解密,我得到一個解密錯誤的java類,當輸入長度必須是16的倍數:javax.crypto.IllegalBlockSizeException:帶襯墊的密碼
javax.crypto.IllegalBlockSizeException :
Input length must be multiple of 16 when decrypting with padded cipher.
我能做些什麼來解決這個問題?
UPDATE:
我忘了說,它正在一次,當第二次即時試圖再次執行其拋出上述錯誤。
package com.tb.module.service;
import java.security.Key;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
/**
* This class is used for encrypt and decrypt the password field.
*
*/
public class PswdEnc {
private static final String ALGO = "AES";
private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
無論你解密是錯誤的大小。它需要是塊大小的倍數(16),否則它不能被解密。你如何得到你的解密數據? – thegrinner
實際上,我在將密碼保存到sql數據庫之前先對密碼進行加密。然後當檢索我試圖解密它。 – baburao113
您確定要保存的數據和您讀出的數據長度相同嗎? – thegrinner