gpt4 book ai didi

javax.crypto.BadPaddingException

转载 作者:IT老高 更新时间:2023-10-28 21:01:43 25 4
gpt4 key购买 nike

我正在研究 AES 算法,但我遇到了无法解决的异常。

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)

异常发生在解密部分。我在与解密算法不同的地方初始化 key

KeyGenerator kgen = KeyGenerator.getInstance("AES");//key generation for AES
kgen.init(128); // 192 and 256 bits may not be available

然后我将它与我从文件中读取的密文一起传递给以下方法

 public String decrypt(String message, SecretKey skey) {

byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
// Instantiate the cipher
Cipher cipher;

byte[] original = null;
try {
cipher = Cipher.getInstance("AES");

cipher.init(Cipher.DECRYPT_MODE, skeySpec);
System.out.println("Original string: "
+ message);
original = cipher.doFinal(message.trim().getBytes()); //here where I got the exception
String originalString = new String(original);
}
//catches

编辑这是加密方法。

public String encrypt(String message, SecretKey skey) {
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");

// Instantiate the cipher

Cipher cipher;
byte[] encrypted = null;
try {
cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

encrypted = cipher.doFinal(message.getBytes());
System.out.println("raw is " + encrypted);

} catches
return asHex(encrypted);
}

这里是 asHex 方法

  public static String asHex(byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;

for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}

strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}

return strbuf.toString();
}

这是我从文件中读取密文的地方

static public String readFile(String filePath) {
StringBuilder file = new StringBuilder();
String line = null;
try {
FileReader reader = new FileReader(filePath);
BufferedReader br = new BufferedReader(reader);
if (br != null) {
line = br.readLine();
while (line != null) {
file.append(line);
// System.out.println("line is " + line);
line = br.readLine();

}
}
br.close();
reader.close();
} catch (IOException ex) {
Logger.getLogger(FileManagement.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("line is " + file.toString());
return String.valueOf(file);

}

有人可以帮忙吗?

最佳答案

好的,所以问题是您将加密的字节转换为十六进制字符串(使用 asHex 方法),但没有将十六进制字符串正确转换回字节数组以进行解密。你不能使用 getBytes

您可以使用以下 method将十六进制字符串转换为字节数组:

public static byte[] fromHexString(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}

然后更改要使用的解密方法:

original = cipher.doFinal(fromHexString(message));

关于javax.crypto.BadPaddingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4580982/

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