从JavaScript到Python的对应AES加密/解密

2024-10-01 09:20:49 发布

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

最近我想把最初的Javascript AES256加密代码移植到Python上,我看过很多帖子(例如*1*2),但这不是我想要的。在

我想要一个没有额外设置的简单函数,只需输入有效载荷密钥,输出加密/解密有效载荷,对应于以下两个函数。在

function encrypt (payload, key) {
  var cipher = crypto.createCipher('aes256', key)
  var encryptedPayload = cipher.update(payload, 'utf8', 'hex')
  encryptedPayload += cipher.final('hex')
  return encryptedPayload
}

function decrypt (payload, key) {
  const decipher = crypto.createDecipher('aes256', key)
  let decryptedPayload = decipher.update(payload, 'hex', 'utf8')
  decryptedPayload += decipher.final('utf8')
  return decryptedPayload
}

我找不到任何解决办法。这是否意味着这个问题很难解决,或者Javascript和Python库之间的规范不匹配?在

或者,如果我想让它成为现实,我能为PyCrypto或一些开源项目做贡献吗?在

谢谢!在


Tags: key函数varfunctionutf8javascriptcryptoaes256