gpt4 book ai didi

java - 如何在此方法上使用 Cipher 来解密字符串?

转载 作者:行者123 更新时间:2023-12-01 22:48:56 27 4
gpt4 key购买 nike

你好,我构建了这两种方法,加密工作正常,但解密出现错误,因为密码想要一个字节,我想从字符串加密

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

关于java - 如何在此方法上使用 Cipher 来解密字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24968466/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com