AES Encryption/Decryption:给定的最终块未正确填充
AES Encryption/Decryption : Given final block not properly padded
我正在尝试编写加密或解密字符串(主要是数字)的方法。它适用于某些文本(例如-“1010000011”、“1010000012”、“1010000013”),但对其他文本(例如-“1010000014”、“1010000018”)给出以下错误:
javax.crypto.BadPaddingException: Given final block not properly
padded
这是我的代码:
public static SecretKey secKey;
private static IvParameterSpec ivspec;
static {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec("i15646dont6321wanna".toCharArray(),
"ahhalkdjfslk3205jlk3m4ljdfa85l".getBytes("UTF-8"), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
secKey = new SecretKeySpec(tmp.getEncoded(), "AES");
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ivspec = new IvParameterSpec(iv);
} catch (NoSuchAlgorithmException | InvalidKeySpecException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encryptData(String textToEncrypt) {
byte[] encryptedBytes = null;
String encryptedText = "";
try {
byte[] byteToEncrypt = textToEncrypt.getBytes(Charset.defaultCharset());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secKey, ivspec);
encryptedBytes = cipher.doFinal(byteToEncrypt);
encryptedText = new String(encryptedBytes);
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException
| InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return encryptedText;
}
public static String decryptData(String textToDecrypt) {
byte[] decryptedBytes = null;
String decryptedText = "";
try {
byte[] byteToDecrypt = textToDecrypt.getBytes(Charset.defaultCharset());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secKey, ivspec);
decryptedBytes = cipher.doFinal(byteToDecrypt);
decryptedText = new String(decryptedBytes);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| IllegalBlockSizeException | BadPaddingException
| InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return decryptedText;
}
要加密的字符串从文件中读取,加密后写入其他文件。稍后必须解密此加密文本。我按以下方式调用这些方法:
String[] lineArray = line.split(" | "); //line is read from a file.
String encryptedText = AESEncryption.encryptData(lineArray[0]);
String decryptedText = AESEncryption.decryptData(encryptedText);
System.out.println("Original Text: " + lineArray[0] + " | Encrypted text: "
+ encryptedText + " | Decrypted again: " + decryptedText);
您正在尝试将加密数据作为字符串传递。这是致命的。加密数据是字节,而不是字符串。使用 Base64 转换实用程序将加密字节转换为 Base64 字符串。
加密:明文 -> 加密字节 -> Base64 文本。
解密:Base64 文本 -> 加密字节 -> 解密文本。
我正在尝试编写加密或解密字符串(主要是数字)的方法。它适用于某些文本(例如-“1010000011”、“1010000012”、“1010000013”),但对其他文本(例如-“1010000014”、“1010000018”)给出以下错误:
javax.crypto.BadPaddingException: Given final block not properly padded
这是我的代码:
public static SecretKey secKey;
private static IvParameterSpec ivspec;
static {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec("i15646dont6321wanna".toCharArray(),
"ahhalkdjfslk3205jlk3m4ljdfa85l".getBytes("UTF-8"), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
secKey = new SecretKeySpec(tmp.getEncoded(), "AES");
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ivspec = new IvParameterSpec(iv);
} catch (NoSuchAlgorithmException | InvalidKeySpecException | UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encryptData(String textToEncrypt) {
byte[] encryptedBytes = null;
String encryptedText = "";
try {
byte[] byteToEncrypt = textToEncrypt.getBytes(Charset.defaultCharset());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secKey, ivspec);
encryptedBytes = cipher.doFinal(byteToEncrypt);
encryptedText = new String(encryptedBytes);
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException
| InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return encryptedText;
}
public static String decryptData(String textToDecrypt) {
byte[] decryptedBytes = null;
String decryptedText = "";
try {
byte[] byteToDecrypt = textToDecrypt.getBytes(Charset.defaultCharset());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secKey, ivspec);
decryptedBytes = cipher.doFinal(byteToDecrypt);
decryptedText = new String(decryptedBytes);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| IllegalBlockSizeException | BadPaddingException
| InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return decryptedText;
}
要加密的字符串从文件中读取,加密后写入其他文件。稍后必须解密此加密文本。我按以下方式调用这些方法:
String[] lineArray = line.split(" | "); //line is read from a file.
String encryptedText = AESEncryption.encryptData(lineArray[0]);
String decryptedText = AESEncryption.decryptData(encryptedText);
System.out.println("Original Text: " + lineArray[0] + " | Encrypted text: "
+ encryptedText + " | Decrypted again: " + decryptedText);
您正在尝试将加密数据作为字符串传递。这是致命的。加密数据是字节,而不是字符串。使用 Base64 转换实用程序将加密字节转换为 Base64 字符串。
加密:明文 -> 加密字节 -> Base64 文本。
解密:Base64 文本 -> 加密字节 -> 解密文本。