python aes padding_使用PKCS7Padding在python和Node.js之间进行AES加密

本文介绍了如何使用Python的PKCS7Padding进行AES加密,并尝试在Node.js中解密。作者遇到了解密错误,提示'bad decrypt',可能是因为在Node.js端的解码过程存在问题。
摘要由CSDN通过智能技术生成

已解决

密码学Python

使用PKCS7Padding在python和Node.js之间进行AES加密10

我试图用Node.js中的pkcs7填充来解密消息没有成功。 此消息已加密并从Python代码发送。 我管理它使它在纯Python中工作,但无法弄清楚如何在Node.js中实现解码(pkcs7填充)功能。 任何人都可以帮我重现代码吗?

Python Encode-Encryptimport base64

import hashlib

from Crypto.Cipher import AES

import pkcs7

text = "data"

pasword = "key"

pw_bytes = pasword.encode('utf-8')

text_bytes = text.encode('utf-8')

m = hashlib.md5()

m.update(pw_bytes)

key = m.hexdigest()

cipher = AES.new(key, AES.MODE_ECB)

pad_text = pkcs7.encode(text_bytes)

msg = cipher.encrypt(pad_text)

EncodeMsg = base64.b64encode(msg)

encryptedstring = EncodeMsg.decode("utf-8")

print(encryptedstring)

# Output: SxQE+SERkAzYcdG/ESAhiQ==

另外,我在Python pkcs7.py中为pkcs7填充添加了自定义代码import binascii

try:

from StringIO import StringIO

except ImportError:

from io import StringIO

def decode(bytestring, k=16):

val = binascii.hexlify(bytestring[-1])

val = int(val, 16)

if val > k:

raise ValueError('Input is not padded or padding is corrupt')

l = len(bytestring) - val

return bytestring[:l]

def encode(bytestring, k=16):

l = len(bytestring)

output = StringIO()

val = k - (l % k)

for _ in range(val):

output.write('%02x' % val)

return bytestring + binascii.unhexlify(output.getvalue())

Node.js解密const crypto = require('crypto');

var decipher = crypto.createDecipher('aes-128-ecb', 'key');

var decrypted = decipher.update('SxQE+SERkAzYcdG/ESAhiQ==', 'base64', 'utf8');

decrypted += decipher.final('utf8');

console.log(decrypted);

这是错误:Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

at Error (native)

at Decipher.Cipher.final (crypto.js:153:26)

at Object.(/root/Documents/Testing-Repo/node.js/testing.js:21:23)

at Module._compile (module.js:413:34)

at Object.Module._extensions..js (module.js:422:10)

at Module.load (module.js:357:32)

at Function.Module._load (module.js:314:12)

at Timeout.Module.runMain [as _onTimeout] (module.js:447:10)

at tryOnTimeout (timers.js:224:11)

at Timer.listOnTimeout (timers.js:198:5)

区块链技术

2019.06.16

7365

收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值