将 aes-js CTR 翻译成 Java

马文·舍恩

我在 javascript 中使用 aes-js 进行了给定的加密,需要在 Java 中对其进行解密。结果不是预期的文本,而是一些胡言乱语。我怀疑我对初始化向量和计数器的理解有误。我尝试了多个 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 或者我做错了什么?

马文·舍恩

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章