如果我使用错误的键或错误的盐进行解密BadPaddingException被抛出。 我期望一个不正确的字符串被返回。 的doFinal()导致异常的解密法AES BadPaddingException
消息:This is just an example
Unfug:'ΩÙΩ„SåF?V®ßs.k˚·ºç€èÀHfif∫ÙÉÕ
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 casino.AES.decryptString(AES.java:130)
at casino.AES.main(AES.java:172)
public static void main(String[] args) throws Exception {
//Encryption
AES encr = new AES();
encr.setKey("KEY");
encr.setSalt("SALT");
encr.setup();
String message = "This is just an example";
System.out.println("Message : " + message);
byte[] code = encr.encrypt(message);
System.out.println("Encrypted Strinng : "+ new String(code, "UTF-8"));
//Decryption
AES dec = new AES();
dec.setKey("INCORRECT"); //<--- incorrect
dec.setSalt("SALT");
dec.setup();
System.out.println(dec.decryptString(code));
}
public synchronized void setKey(String key) throws UnsupportedEncodingException {
this.key = key.getBytes("UTF-8");
isPasswordAlreadySet = true;
}
public synchronized void setSalt(String salt) throws UnsupportedEncodingException {
this.salt = salt.getBytes("UTF-8");
}
public synchronized void setup() throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(key);
digest.update(salt);
byte[] raw = digest.digest();
skeySpec = new SecretKeySpec(raw, "AES");
cipher = Cipher.getInstance("AES");
}
public synchronized byte[] encrypt(byte[] klartext) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(klartext);
return encrypted;
}
public synchronized byte[] encrypt(String klartext) throws Exception{
return encrypt(klartext.getBytes("UTF-8"));
}
public synchronized byte[] decrypt(byte[] code) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(code);
return original;
}
public synchronized double decryptDouble(byte[] code) throws Exception {
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original = cipher.doFinal(code);
return doubleFromBytes(original);
}
谢谢! 弗雷德里克
我已经改变了密码的initline在一个提供者无关的方式的密钥:密码= Cipher.getInstance( “AES/CBC/PKCS5Padding”); 但是抛出同样的异常。 为了获得正确的长度,我使用了SHA-Hash - 我认为这应该足够了。 此外,我第一次使用setup()方法时不会引发异常 – Frederik 2011-04-08 15:28:21
@Frederik:您必须了解填充的工作方式:填充模式将在加密之前将一些填充内容附加到纯文本数据。解密后,该填充再次被剥离。但要知道有多少个字节需要去除,填充的内容很重要,如果我们使用错误的密钥解密,最后一些字节不是将被填充算法添加的格式之一。如上所述,使用'NoPadding',但是您必须确保您的数据长度是块长度的倍数。 – 2011-04-08 15:49:48
对于'PKCS5Padding',它与DES CBC的[RFC 1423](http://tools.ietf.org/html/rfc1423#section-1.1)中描述的相同。 – 2011-04-08 15:57:51