【发布时间】:2020-11-04 10:56:03
【问题描述】:
我正在尝试创建一个多平台加密解密机制,到目前为止,我已经能够在 python 中加密并在 C 中解密,反之亦然,现在我正在尝试使用 python 脚本和一个节点来做同样的事情js脚本。我能够在节点 js 中加密一个字符串并在 python 中对其进行解密,但是在 Node 中使用 python 的加密消息进行解密并没有发生 这是python代码:
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto import Random
from base64 import b64decode
from base64 import b64encode
import json
import random
#iv= get_random_bytes(16)
key=b"aaaaaaaaaaaaaaaa"
iv= b"aaaaaaaaaaaaaaaa"
value = "Hello World"
strValue= str.encode(value)
data =strValue
#Encryption
data = b64encode(data)
pad =data + b"\0" * (AES.block_size - len(data) % AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext= cipher.encrypt(pad)
print (type(ciphertext))
print(b64encode(ciphertext).decode("utf-8"))
# Decryption
cipher = AES.new(key, AES.MODE_CBC, iv)
data = cipher.decrypt(ciphertext)
print(b64decode(data))
这是 Nodejs 代码:
const crypto = require('crypto');
var iv = Buffer.from('aaaaaaaaaaaaaaaa')
var key = Buffer.from('aaaaaaaaaaaaaaaa')
var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
let enc= cipher.update( "Hello World");
console.log(typeof (enc))
enc += cipher.final('base64');
console.log("enc is :",enc)
var decipher = crypto.createDecipheriv('aes-128-cbc', key,iv);
let decrypted = decipher.update(enc, 'base64');
decrypted += decipher.final('utf8');
console.log("plain text is :",decrypted)
我从以下位置获取节点部分:
AES - Encryption with Crypto (node-js) / decryption with Pycrypto (python)
我收到错误消息:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
任何帮助将不胜感激,谢谢!
如果有更好的Node js实现方法请告诉。
【问题讨论】:
标签: node.js python-3.x encryption pycryptodome