I am trying to create a multi-platform encryption-decryption mechanism, so far I have been able to encrypt in python and decrypt in C and vice versa, now I am trying to do the same using the python script and a node js script. I am able to encrypt a string in node js and decrypt it in python but decryption in Node using the encrypted message of python is not happening Here is the python code :
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))
Here is the Nodejs code:
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)
I took the node portion from :
AES - Encryption with Crypto (node-js) / decryption with Pycrypto (python)
I am getting the error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
Any help would be much appreciated, Thank you!
And if there is any better method for Node js implementation please tell.