我在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)
根据问题中的新信息更新:最好的办法是Nodejs使用默认的计数器值。在
加密和解密都必须使用相同的计数器值。但加密时没有提供计数器值,解密时使用随机值,因此它永远无法工作。在
使用:
crypto.createCipheriv(algorithm, key, iv)
,其中iv
是随机计数器的初始值。在有必要在加密时创建一个随机计数器值并保存它,以便在解密时可以使用相同的初始计数器值。一种方法是在加密数据前面加上计数器值,它不需要保密。然后在解密时,它可以从加密的数据中分离出来并使用。在
同样,当使用CTR模式时,相同的初始计数器值不得再次与同一按键一起使用。在
见CTR mode
IV是初始计数器值。在
相关问题 更多 >
编程相关推荐