在我的javascript中,我有以下代码:
var encrypted = CryptoJS.AES.encrypt("Message that I am trying to send !", k, { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv:i });
console.log("Cipher Text");
console.log(encrypted.ciphertext.toString(CryptoJS.enc.Base64));
//prints out: 1e9z0zjI9MGBLW6AdLE+BiVnv9R9lDAS+SmSkX/Ufx7zTHOVanpiwd4IC2XejJaq
console.log("Key: " + k);
//prints out f7710e2bcf419dab
console.log("Iv: " + i);
// prints out 42d39acea111ceb2
这是我在java方面的解密代码:
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import org.apache.commons.codec.binary.BaseNCodec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public static void main(String[] args) {
// TODO Auto-generated method stub
String encrypted = "1e9z0zjI9MGBLW6AdLE+BiVnv9R9lDAS+SmSkX/Ufx7zTHOVanpiwd4IC2XejJaq";
SecretKey key = new SecretKeySpec("f7710e2bcf419dab".getBytes("UTf-8"), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec("42d39acea111ceb2".getBytes("UTf-8"));
byte[] decodeBase64 = Base64.decodeBase64(encrypted);
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
cipher.init(Cipher.DECRYPT_MODE, key, iv);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String s = new String(cipher.doFinal(decodeBase64).toString());
System.out.println(s);
} catch (IllegalBlockSizeException | BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
当我在java端解密时,它会抛出一个execption" javax.crypto.BadPaddingException:给定最终块没有正确填充"马上,我最初的观察是我在加密端的填充是Pkcs7,而我在java上的填充是PKCS5Padding。我一直在互联网上搜索,填充似乎是可以互换的?有人可以解释一下我在这里做错了什么吗?