gpt4 book ai didi

javascript - 将 aes-js CTR 翻译成 Java

转载 作者:行者123 更新时间:2023-12-01 18:17:07 24 4
gpt4 key购买 nike

我使用 aes-js 在 javascript 中进行了给定的加密,并且需要在 Java 中对其进行解密。结果不是预期的文本,而是一些乱码。我怀疑我对初始化 vector 和计数器的理解有误。我尝试了多个iv,例如仅0、仅1、1到16、5到20和5到15然后0到4。

这是我尝试翻译成java的代码。效果很好。

var aesjs = require('aes-js')


test('encrypts text', () => {
// An example 128-bit key (16 bytes * 8 bits/byte = 128 bits)
var key = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

// Convert text to bytes
var text = 'Text may be any length you wish, no padding is required.';
var textBytes = aesjs.utils.utf8.toBytes(text);

// The counter is optional, and if omitted will begin at 1
var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
var encryptedBytes = aesCtr.encrypt(textBytes);

// To print or store the binary data, you may convert it to hex
var encryptedHex = aesjs.utils.hex.fromBytes(encryptedBytes);
console.log(encryptedHex);
// "a338eda3874ed884b6199150d36f49988c90f5c47fe7792b0cf8c7f77eeffd87
// ea145b73e82aefcf2076f881c88879e4e25b1d7b24ba2788"

// When ready to decrypt the hex string, convert it back to bytes
var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);

// The counter mode of operation maintains internal state, so to
// decrypt a new instance must be instantiated.
var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
var decryptedBytes = aesCtr.decrypt(encryptedBytes);

// Convert our bytes back into text
var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
console.log(decryptedText);
// "Text may be any length you wish, no padding is required."
expect(decryptedText).toBe(text);
});

但是我的java代码不起作用

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class SimpleCrypt {

//encryptedHex = "a338eda3874ed884b6199150d36f49988c90f5c47fe7792b0cf8c7f77eeffd87
// ea145b73e82aefcf2076f881c88879e4e25b1d7b24ba2788"
//key =[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
public String simpleDecrypt(byte[] key, String encryptedHex) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
// js: var encryptedBytes = aesjs.utils.hex.toBytes(encryptedHex);
byte[] encryptedBytes = DatatypeConverter.parseHexBinary(encryptedHex);

// js: var aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(5));
final Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
byte[] iv = new byte[]{5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; //aes-js uses a Counter initialized with 5
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), ivSpec);

// js: var decryptedBytes = aesCtr.decrypt(encryptedBytes);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

// js var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);
String decryptedText = new String(decryptedBytes);
return decryptedText;
//#�\t<�0��m�A�RE�ʶ\n� �p�')�+pG/I��5r1��t�R���j��e
}
}

加密的字节数组是相同的(java也是有符号的,而js是无符号的),但解密的字节数组则不同。正确的 iv 是多少还是我做错了什么?

最佳答案

要给该线程一个可接受的答案:托帕科是对的。正确的 iv 是 byte[] iv = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5};

关于javascript - 将 aes-js CTR 翻译成 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60345071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com