[英]Java AES giving BadPaddingException
I am trying to code a simple encryption and decryption method for my program in Java.我正在尝试在 Java 中为我的程序编写一个简单的加密和解密方法。 It should encrypt and decrypt a string with another string as key.它应该使用另一个字符串作为密钥来加密和解密一个字符串。
While testing, a BadPaddingException
occured.在测试时,发生了BadPaddingException
。
My current method to encrypt:我目前的加密方法:
public String encrypt(String strToEncrypt, String secretKey) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] salt = new Hash().hashString(secretKey).getBytes();
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
byte[] ciphertext = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
System.out.println("Finished encryption");
return new Base64().encodeToString(ciphertext);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
My current method to decrypt:我目前的解密方法:
public String decrypt(String strToDecrypt, String secretKey) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] salt = new Hash().hashString(secretKey).getBytes();
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
byte[] decodedPlaintext = new Base64().decode(strToDecrypt);
byte[] plaintext = cipher.doFinal(decodedPlaintext);
System.out.println("Finished decryption");
return new Base64().encodeToString(plaintext);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
I am getting the followed exception:我收到以下异常:
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:975)
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1056)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853)
at java.base/com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2202)
With the kind help of Michael and President James K. Polk I could get the en- and decryption working and improved the generation of the salt.在 Michael 和总裁 James K. Polk 的帮助下,我可以进行加密和解密工作,并改进了盐的生成。 My mistake was, that I wasn't using the same IvParameterSpec
for encryption and decryption.我的错误是,我没有使用相同的IvParameterSpec
进行加密和解密。
My encryption method:我的加密方法:
public static String encrypt(String strToEncrypt, String secretKey) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] salt = new byte[16];
SecureRandom.getInstanceStrong().nextBytes(salt);
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secret, ivspec);
byte[] ivBytes = ivspec.getIV();
byte[] ciphertext = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
System.out.println("Finished encryption");
byte[] outputBytes = concat(concat(salt, ivBytes), ciphertext);
return new Base64().encodeToString(outputBytes);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
My decryption method:我的解密方法:
public static String decrypt(String strToDecrypt, String secretKey) {
try {
byte[] decodedCiphertext = new Base64().decode(strToDecrypt);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] salt = Arrays.copyOfRange(decodedCiphertext, 0, 16);
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = Arrays.copyOfRange(decodedCiphertext, 16, 32);
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secret, ivspec);
byte[] plaintext = cipher.doFinal(Arrays.copyOfRange(decodedCiphertext, 32, decodedCiphertext.length));
System.out.println("Finished decryption");
return new String(plaintext, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.