doFinal()javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整

时间:2014-06-28 22:32:20

标签: encryption aes

很抱歉,如果已经回答了这个问题。我看到几个相似但不是我认为是我的问题。

我收到以下错误:     javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整     在org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(未知来源)     在javax.crypto.Cipher.doFinal

public static String encryptAES(String key, String source) {
String encrypted = "";
try {  
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); 
    byte[] bXml = source.getBytes("UTF8");
    byte[] enc = cipher.doFinal(bXml);
    encrypted = new sun.misc.BASE64Encoder().encode(enc); 
} catch (Exception e) {  /** removed **/ }
return encrypted;
}
public static String decryptAES(String key, String source) {
String decrypted = "";
try {  
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");     
    IvParameterSpec iv = new IvParameterSpec(new byte[16]);
    cipher.init(Cipher.DECRYPT_MODE, keySpec, iv); 
    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(source); 
    byte[] bDecrypted = cipher.doFinal(dec);  /** Error occurs here **/
    decrypted = new String(bDecrypted, "UTF8");     
} catch (Exception e) {  /** removed **/ }  
return decrypted;
}

0 个答案:

没有答案