我正在使用 AES 进行加密/解密。我正在尝试加密文本,然后稍后单独解密。
我的 AESEncrypt.java 是:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESEncrypt {
private static final byte[] SALT = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03};
private static final int ITERATION_COUNT = 65536;
private static final int KEY_LENGTH = 256;
private Cipher eCipher;
private Cipher dCipher;
AESEncrypt(String passPhrase) throws Exception {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");
eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
eCipher.init(Cipher.ENCRYPT_MODE, secretKey);
dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = eCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}
public String encrypt(String encrypt) throws Exception {
byte[] bytes = encrypt.getBytes("UTF8");
byte[] encrypted = encrypt(bytes);
return new String(Base64.encodeBase64(encrypted));
}
public byte[] encrypt(byte[] plain) throws Exception {
return eCipher.doFinal(plain);
}
public static void main(String[] args) throws Exception {
String passphrase = "PASSWORDPASSPHRASE";
String password = "password123";
AESEncrypt aesEncrypt = new AESEncrypt(passphrase);
String encryptedPassword = aesEncrypt.encrypt(password);
System.out.println("encryptedPassword = " + encryptedPassword);
}
}
运行上面的AESEncrypt代码后,结果是:
encryptedPassword = aUkbhjFebZ9VjJ44yptlBA==
所以,现在在我的 AESDecrypt.java 中,我使用该 encryptedPassword 字符串进行解密:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESDecrypt {
private static final byte[] SALT = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03};
private static final int ITERATION_COUNT = 65536;
private static final int KEY_LENGTH = 256;
private Cipher eCipher;
private Cipher dCipher;
AESDecrypt(String passPhrase) throws Exception {
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");
eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
eCipher.init(Cipher.ENCRYPT_MODE, secretKey);
dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = eCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}
public String decrypt(String encrypt) throws Exception {
byte[] bytes = Base64.decodeBase64(encrypt);
byte[] decrypted = decrypt(bytes);
return new String(decrypted, "UTF8");
}
public byte[] decrypt(byte[] encrypt) throws Exception {
return dCipher.doFinal(encrypt);
}
public static void main(String[] args) throws Exception {
String passphrase = "PASSWORDPASSPHRASE";
String encryptedPassword = "aUkbhjFebZ9VjJ44yptlBA==";
AESDecrypt aesDecrypt = new AESDecrypt(passphrase);
String decryptedPassword = aesDecrypt.decrypt(encryptedPassword);
System.out.println("decryptedPasswpord = " + decryptedPassword);
}
}
运行 AESDecrypt 代码时,返回错误:
Exception in thread "main" 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*..)
at AESDecrypt.decrypt(AESDecrypt.java:41)
at AESDecrypt.decrypt(AESDecrypt.java:35)
at AESDecrypt.main(AESDecrypt.java:50)
为什么我会收到错误消息?有人可以帮忙和建议吗?非常感谢!