我正在开发一个项目,我需要向用户发送验证链接。所以使用AES加密来加密他的用户名。我的代码工作正常,即加密和解密工作正常,但只在程序中,当我测试它。我加密了一个字符串,然后解密它。它在当地很好地工作。AES中的差异填充异常
的问题是,当我发送电子邮件激活链接,点击它,它给我的错误:
javax.crypto.BadPaddingException: Given final block not properly padded
我的代码如下所示:
public class AES {
private static final String algo="AES";
private static final byte[] keyValue=
new byte[]{somekey};
private static Key generateKey() throws Exception{
Key key= new SecretKeySpec(keyValue, algo);
return key;
}
public static String encrypt(String email) throws Exception{
Key key=generateKey();
Cipher c=Cipher.getInstance(algo);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal=c.doFinal(email.getBytes());
String encryptedEmail= new BASE64Encoder().encode(encVal);
return encryptedEmail;
}
public static String decrypt(String encryptedEmail) throws Exception{
Key key=generateKey();
Cipher c=Cipher.getInstance(algo);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decodeEmail= new BASE64Decoder().decodeBuffer(encryptedEmail);
byte[] decodedEmail=c.doFinal(decodeEmail);
String decryptedEmail= new String(decodedEmail);
return decryptedEmail;
}
}
有没有人会帮助我?请紧急! – kunal18 2012-07-21 09:37:02
无论何时出现任何错误的算法输入(在您的案例中为密钥或密文),都会抛出BadPaddingException。您是否比较了基础64编码/解码的输出?您不应该使用内部Sun类,而应尝试使用Bouncy Castle或Apache库。 – 2012-07-21 10:31:00
你解决了这个问题吗?如果是这样,你能指出做了什么并且接受或发布了答案吗? – 2012-07-30 00:11:15