我需要使用JavaScript实现AES加密。使用AES / CBC / NoPadding模式并创建了一个方法来完成16个长度块。我已经用Java解决了它。它看起来像:
public static String encrypt(byte[] key, byte[] initVector, String value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector);
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(completeBlocks(value));
return Base64.encodeBase64String(encrypted);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
System.out.println("Error: " + ex);
}
return null;
}
/**
* Completes 16 lenght blocks
*
* @param message
*
*
*/
static byte[] completeBlocks(String message) {
try {
int bytesLenght = message.getBytes("UTF-8").length;
if (bytesLenght % 16 != 0) {
byte[] newArray = new byte[bytesLenght + (16 - (bytesLenght % 16))];
System.arraycopy(message.getBytes(), 0, newArray, 0, bytesLenght);
return newArray;
}
return message.getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
System.out.println("" + ex);
}
return null;
}
public static void main(String[] args) {
String key = "253D3FB468A0E24677C28A624BE0F939";
String strToEncrypt = "My Secret text";
final byte[] initVector = new byte[16];
String resultado = encrypt(new BigInteger(key, 16).toByteArray(), initVector, strToEncrypt.trim());
System.out.println("ENCRYPTED:");
System.out.println(resultado);
}
输入key = 253D3FB468A0E24677C28A624BE0F939
,strToEncrypt = "My Secret text"
和ceros IV。它抛出
7StScX3LnPUly / VNzBes0w ==
我知道这是所需的输出。这是正确的! 我尝试使用JavaScript复制它。我用过CryptoJs库。但我无法生成相同的Java输出。我试过了:
var text = "My Secret text";
var key = CryptoJS.enc.Base64.parse("253D3FB468A0E24677C28A624BE0F939");
var iv = CryptoJS.enc.Base64.parse(" ");
var encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv});
console.log(encrypted.toString());
var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv});
console.log(decrypted.toString(CryptoJS.enc.Utf8));
使用相同的输入,我得到De+CvPVIyiBX2//EE6gXTg==
作为输出。我究竟做错了什么?如何获得相同的Java输出?非常感谢!!
答案 0 :(得分:10)
假设你会修复像空的IV&这是概念证明,您的代码失败是因为:
要在JS中复制输出:
wlan0
有关:
7StScX3LnPUly / VNzBes0w ==
答案 1 :(得分:0)
如果您想使用自定义IV进行加密,则可以像...
\.