Python字节串 => Javascript?

3

我想把一些Python代码移植到Javascript。以下是Python代码:

# Python 
import codecs
from Crypto.Cipher import AES
key = b"\xc3\x99\xff\xff\xc3\x99\xff\xff\xc3\x99\xff\xff\xc3\x99\xff\xff"
...
aes = AES.new(key, AES.MODE_ECB)
token = aes.encrypt("HELLO\x00\x00".encode("utf-8"))
token_hex = codecs.encode(token, "hex").decode("utf-8")

我不确定如何转移我的Python key 变量。应该使用 UInt16Array 还是字符串?

以下是我的Javascript:

// Javascript
const crypto = require('crypto');
const key = '???' // <-- This is one place I am stuck. String? Byte array?
....
const cipher = crypto.createCipher('aes-128-ecb', key);
let tokenHex = cipher.update('HELLO\x00\x00', 'utf8', 'hex');
tokenHex = tokenHex.toString('utf8')

请问如何在Javascript中获取匹配的tokenHex,非常感谢您提供的任何有用信息。

谢谢!


1
Buffer.from([0xc3, 0x99, …, 0xff]) - undefined
1个回答

4
你需要的是代表字节集合的 Buffer。你可能想要像这样实例化密钥变量:
let key = Buffer.from("c399ff...", "hex");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接