从Nodejs加密时,在Python中解密AES256CTR有效载荷

2024-10-06 07:15:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Nodejs中编写了一个应用程序,它使用AES-256-CTR加密用户密码:

const crypto = require('crypto')
const masterkey = 'azertyuiopazertyuiopazertyuiopaz'
const cipher = crypto.createCipher('aes-256-ctr', masterkey)
console.log(cipher.update('antoine', 'utf8', 'hex') + cipher.final('hex')) //=> 6415bc70ad76c6

然后它被持久化到数据库中,现在我尝试使用PyCrypto从Python脚本中对其进行解密,如下所示:

^{pr2}$

但这给了我完全错误的结果。在

你知道我错过了什么吗?在

编辑

多亏了zaph,我的Javascript代码加密数据的方式似乎不安全。我仍然需要弄清楚Node在内部使用了什么IV。我试过很多次都没有成功

masterkey[0:16].encode()
bytes(16)

Tags: 用户应用程序密码nodejsrequirecryptoaescipher
1条回答
网友
1楼 · 发布于 2024-10-06 07:15:50

根据问题中的新信息更新:最好的办法是Nodejs使用默认的计数器值。在

加密和解密都必须使用相同的计数器值。但加密时没有提供计数器值,解密时使用随机值,因此它永远无法工作。在

使用:crypto.createCipheriv(algorithm, key, iv),其中iv是随机计数器的初始值。在

有必要在加密时创建一个随机计数器值并保存它,以便在解密时可以使用相同的初始计数器值。一种方法是在加密数据前面加上计数器值,它不需要保密。然后在解密时,它可以从加密的数据中分离出来并使用。在

同样,当使用CTR模式时,相同的初始计数器值不得再次与同一按键一起使用。在

CTR mode

PyCrypto documentation CTR mode:

MODE_CBC
Cipher-Block Chaining (CBC). Each of the ciphertext blocks depends on the current and all previous plaintext blocks. An Initialization Vector (IV) is required.

The IV is a data block to be transmitted to the receiver. The IV can be made public, but it must be authenticated by the receiver and it should be picked randomly.)

IV是初始计数器值。在

[Nodejs dociumewnrtation: Class: Cipher:

crypto.createCipheriv(algorithm, key, iv)
    algorithm <string>  
    key <string> | <Buffer> | <TypedArray> | <DataView>
    iv <string> | <Buffer> | <TypedArray> | <DataView>

Creates and returns a Cipher object, with the given algorithm, key and initialization vector (iv).

相关问题 更多 >