Java 如何在此方法上使用密码来解密字符串?
您好,我构建了这两种方法加密工作正常,但解密出错,因为 cipher需要一个字节,我需要从字符串加密Java 如何在此方法上使用密码来解密字符串?,java,encryption,aes,secret-key,Java,Encryption,Aes,Secret Key,您好,我构建了这两种方法加密工作正常,但解密出错,因为 cipher需要一个字节,我需要从字符串加密 import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; public class Test { private byte[] encrypted; private String encryptedtext; private String decrypted; public
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Test {
private byte[] encrypted;
private String encryptedtext;
private String decrypted;
public String Encrypt (String pInput) {
try {
String Input = pInput;
String key = "Bar12345Bar12345Bar12345Bar12345";
// Erstelle key and cipher
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// Verschlüsselung
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(Input.getBytes());
encryptedtext = new String(encrypted);
System.err.println("encrypted:" + encryptedtext);
}catch(Exception e) {
e.printStackTrace();
}
return encrypted;
}
public String Decrypt (String pInput) {
try {
String Input = pInput;
String key = "Bar12345Bar12345Bar12345Bar12345";
// Erstelle key and cipher
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// Entschlüsselung
cipher.init(Cipher.DECRYPT_MODE, aesKey);
decrypted = new String(cipher.doFinal(encryptedtext)); // HERE IS THE PROBLEM IT WANT BYTE BUT I WANT TO ENCRYPT FROM A STRING
System.err.println("decrypted: " + decrypted);
}catch(Exception e) {
e.printStackTrace();
}
return pInput;
}
}
字节数组不能直接转换为字符串,反向也不能
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class stackoverflow_test {
private byte[] encrypted;
private String encryptedtext;
private String decrypted;
public String Encrypt(String pInput) {
try {
String Input = pInput;
String key = "Bar12345Bar12345Bar12345Bar12345";
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(Input.getBytes());
//encryptedtext = new String(encrypted);
encryptedtext = DatatypeConverter.printBase64Binary(encrypted);
System.err.println("encrypted:" + encryptedtext);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedtext;
}
public String Decrypt(String pInput) {
try {
String Input = pInput;
String key = "Bar12345Bar12345Bar12345Bar12345";
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
encrypted = DatatypeConverter.parseBase64Binary(encryptedtext);
decrypted = new String(cipher.doFinal(encrypted));
System.err.println("decrypted: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
return pInput;
}
public static void main(String[] ag){
stackoverflow_test test = new stackoverflow_test();
String a = test.Encrypt("Byte cannot directly convert to string");
String b = test.Decrypt(a);
}
}
结果
encrypted:UmH+3eUagjrRDblxSStArnaktoxTLX+7qvPdwiTO7VggYmYtuXu/Ygww8ZG5SrDz
decrypted: Byte cannot directly convert to string
字节数组不能直接转换为字符串,反向也不能
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class stackoverflow_test {
private byte[] encrypted;
private String encryptedtext;
private String decrypted;
public String Encrypt(String pInput) {
try {
String Input = pInput;
String key = "Bar12345Bar12345Bar12345Bar12345";
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(Input.getBytes());
//encryptedtext = new String(encrypted);
encryptedtext = DatatypeConverter.printBase64Binary(encrypted);
System.err.println("encrypted:" + encryptedtext);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedtext;
}
public String Decrypt(String pInput) {
try {
String Input = pInput;
String key = "Bar12345Bar12345Bar12345Bar12345";
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
encrypted = DatatypeConverter.parseBase64Binary(encryptedtext);
decrypted = new String(cipher.doFinal(encrypted));
System.err.println("decrypted: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
return pInput;
}
public static void main(String[] ag){
stackoverflow_test test = new stackoverflow_test();
String a = test.Encrypt("Byte cannot directly convert to string");
String b = test.Decrypt(a);
}
}
结果
encrypted:UmH+3eUagjrRDblxSStArnaktoxTLX+7qvPdwiTO7VggYmYtuXu/Ygww8ZG5SrDz
decrypted: Byte cannot directly convert to string
您可以使用
密码
对字符串
进行加密和解密
公共类cryptoutil{
私有静态最终字符串算法=“Blowfish”;
私有静态最终字符串模式=“Blowfish/CBC/pkcs5pAdd”;
私有静态最终字符串IV=“abcdefgh”;
publicstaticstringencrypt(stringsecretkey,stringvalue)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidGorthmParameterException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException{
SecretKeySpec SecretKeySpec=新的SecretKeySpec(secretKey.getBytes(),算法);
Cipher Cipher=Cipher.getInstance(模式);
cipher.init(cipher.ENCRYPT_模式,secretKeySpec,新的IvParameterSpec(IV.getBytes());
byte[]values=cipher.doFinal(value.getBytes());
返回Base64.encodeToString(值为Base64.DEFAULT);
}
公共静态字符串解密(字符串secretKey,字符串值)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidGorthmParameterException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException{
字节[]值=Base64.decode(值,Base64.DEFAULT);
SecretKeySpec SecretKeySpec=新的SecretKeySpec(secretKey.getBytes(),算法);
Cipher Cipher=Cipher.getInstance(模式);
cipher.init(cipher.DECRYPT_模式,secretKeySpec,新的IvParameterSpec(IV.getBytes());
返回新字符串(cipher.doFinal(值));
}
}
您可以使用密码
对字符串
进行加密和解密
公共类cryptoutil{
私有静态最终字符串算法=“Blowfish”;
私有静态最终字符串模式=“Blowfish/CBC/pkcs5pAdd”;
私有静态最终字符串IV=“abcdefgh”;
publicstaticstringencrypt(stringsecretkey,stringvalue)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidGorthmParameterException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException{
SecretKeySpec SecretKeySpec=新的SecretKeySpec(secretKey.getBytes(),算法);
Cipher Cipher=Cipher.getInstance(模式);
cipher.init(cipher.ENCRYPT_模式,secretKeySpec,新的IvParameterSpec(IV.getBytes());
byte[]values=cipher.doFinal(value.getBytes());
返回Base64.encodeToString(值为Base64.DEFAULT);
}
公共静态字符串解密(字符串secretKey,字符串值)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidGorthmParameterException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException{
字节[]值=Base64.decode(值,Base64.DEFAULT);
SecretKeySpec SecretKeySpec=新的SecretKeySpec(secretKey.getBytes(),算法);
Cipher Cipher=Cipher.getInstance(模式);
cipher.init(cipher.DECRYPT_模式,secretKeySpec,新的IvParameterSpec(IV.getBytes());
返回新字符串(cipher.doFinal(值));
}
}
加密也使用字节。那么你是如何解决加密问题的呢?是的,我知道,但是字节被转换成一个字符串,这样我就可以使用它并将其打印出来,但问题是当我使用解密时,它需要加密中的字节会话。但是我想给这个方法一个字符串,它将decrypet文本存储到一个字符串中,以显示decrypted值。我想使用这个独立于这个版本的方法,我必须加密和解密,这将是可行的,但我想使用解密而不加密的东西之前,密码加密字节和返回字节。但是您正在加密字符串。那么,在加密字符串之前,您是如何将其转换为字节的呢?顺便说一下,将加密的字节转换为字符串是错误的,可能会损坏数据。如果您继续这样做,您最终将加密某些内容,并且无法再次解密。请编辑我的代码并使其成为我希望看到的代码示例:)加密也使用字节。那么你是如何解决加密问题的呢?是的,我知道,但是字节被转换成一个字符串,这样我就可以使用它并将其打印出来,但问题是当我使用解密时,它需要加密中的字节会话。但是我想给这个方法一个字符串,它将decrypet文本存储到一个字符串中,以显示decrypted值。我想使用这个独立于这个版本的方法,我必须加密和解密,这将是可行的,但我想使用解密而不加密的东西之前,密码加密字节和返回字节。但是您正在加密字符串。那么,在加密字符串之前,您是如何将其转换为字节的呢?顺便说一下,将加密的字节转换为字符串是错误的,可能会损坏数据。如果您继续这样做,您最终将加密某些内容,而无法再次解密。请编辑我的代码并使其生效。我希望看到一个代码示例:)谢谢:)但是否可以在此代码中不使用Base64,因为Android在Base64方面遇到了一些问题:/这可以帮助您,将其转换为十六进制字符串,那么您必须以错误的方式进行设计。您将创建什么字符串作为decrypt函数的参数?您可以从文件、网络套接字中读取字节[],不需要是字符串,除非某个外国人可以在命令提示符下输入xff。XDNo:/看,如果你做了加密,然后解密,一切都可以,但如果你只是想使用“仅”解密,它不会继续,它会给我错误…@tom87416:这是一个很好的答案,但你应该改变一件事,使其可移植。将Input.getBytes()
更改为Input.getBytes(“UTF-8”)加密和更改新字符串(cipher.d)中的code>