Node.js 使用python3和node js进行加密和解密

Node.js 使用python3和node js进行加密和解密,node.js,python-3.x,encryption,pycryptodome,Node.js,Python 3.x,Encryption,Pycryptodome,我正在尝试创建一个多平台的加密-解密机制,到目前为止,我已经能够用python进行加密,用C进行解密,反之亦然,现在我正在尝试使用python脚本和node js脚本进行同样的操作。我能够在node js中加密字符串,并在python中解密它,但在node中使用python的加密消息进行解密是不可能的 以下是python代码: from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from Crypto

我正在尝试创建一个多平台的加密-解密机制,到目前为止,我已经能够用python进行加密,用C进行解密,反之亦然,现在我正在尝试使用python脚本和node js脚本进行同样的操作。我能够在node 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)
我从以下位置获取节点部分: 我收到错误信息:
06065064:数字信封例程:EVP\u DecryptFinal\u ex:bad decrypt
任何帮助都将不胜感激,谢谢!
如果有更好的方法实现Node js,请告诉我。

在Python代码中,明文在加密之前是Base64编码的,而在NodeJS代码中则不是。此外,Python代码应用自定义的零填充,NodeJS代码应用默认的PKCS7填充

为了使NodeJS代码提供与Python代码相同的密文,必须在加密之前对明文进行Base64编码。此外,必须禁用默认的PKCS7填充,并且必须应用Python代码的零填充变量。由于PyCryptodome不支持零填充,因此需要自定义实现

一种可能的NodeJS实现可以是:

const crypto = require('crypto') 
const buffertrim = require('buffertrim') 

function toB64padded(plaintext, blocksize){
    var bufPlaintext = Buffer.from(plaintext, 'utf8')
    var bufPlaintextB64 = Buffer.from(bufPlaintext.toString('base64'), 'utf8')      // Base64 encoding
    var bufPadding = Buffer.alloc(blocksize - bufPlaintextB64.length % blocksize)
    return Buffer.concat([bufPlaintextB64, bufPadding])                             // Zero padding
}

var iv = Buffer.from('aaaaaaaaaaaaaaaa')                                            // Static IV only for testing purposes
var key =  Buffer.from('aaaaaaaaaaaaaaaa')

// Encryption
var plaintext = "The quick brown fox jumps over the lazy dog"
var bufPlaintextB64padded = toB64padded(plaintext, 16)                              // Base64 encoding and Zero padding
var cipher = crypto.createCipheriv('aes-128-cbc', key, iv)
cipher.setAutoPadding(false)                                                        // Disable PKCS7 padding

var ciphertextB64 = cipher.update(bufPlaintextB64padded, '', 'base64')              // Encryption, Base64 encoding of ciphertext 
ciphertextB64 += cipher.final('base64')
console.log("ciphertext is:", ciphertextB64)  

// Decryption
var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv)
decipher.setAutoPadding(false)                                                      // Disable PKCS7 (un-) padding

var bufPlaintextB64padded = Buffer.concat([                                         // Base64 decoding of ciphertext, decryption
    decipher.update(ciphertextB64, 'base64'), 
    decipher.final()
]);                                                                                 
var bufPlaintextB64 = buffertrim.trimEnd(bufPlaintextB64padded)                     // Unpadding (unreliable)
var bufPlaintext = Buffer.from(bufPlaintextB64.toString('utf8'), 'base64')          // Base64 decoding
console.log("plaintext is:", bufPlaintext.toString('utf8'))
例如,用上面的Python代码和NodeJS代码对快速棕色狐狸跳过懒狗的明文进行加密。这两种代码使用已发布的密钥和IV生成相同的密文,即:

IETFUbiTGsKFZWLgjjP5RrKPX+GeVon1Kuy38bPdKXwqUJWUGWMJ9MOL9gEAsF+1U/N0Juwzu24Dju4UMwdZaA== 
这意味着可以用一种代码加密,用另一种代码解密


请注意,加密前不必对明文进行Base64编码。此外,零填充是一种不可靠的填充,最好用PKCS7填充代替。PyCryptodome支持模块中的PKCS7填充,因此填充不需要自定义实现。如前所述,NodeJS的加密模块默认使用PKCS7填充


此外,出于安全原因,每次加密都必须使用随机IV。IV不是秘密的,通常与密文连接。在Python代码中实现了IV的随机生成(但注释掉了,可能是出于测试目的)。但是,缺少连接部分。在NodeJS代码中,为了与Python代码兼容,这两个代码都被省略了。

这太棒了,非常感谢:)