Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:17:32+00:00 2026-06-03T22:17:32+00:00

I’m trying to encrypt some content in Python and decrypt it in a nodejs

  • 0

I’m trying to encrypt some content in Python and decrypt it in a nodejs application.

I’m struggling to get the two AES implementations to work together though. Here is where I am at.

In node:

var crypto = require('crypto');

var password = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
var input = 'hello world';

var encrypt = function (input, password, callback) {
    var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');

    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');

    // add padding
    while (input.length % 16 !== 0) {
        input += ' ';
    }

    var data = new Buffer(input, 'utf8').toString('binary');

    var cipher = crypto.createCipheriv('aes-256-cbc', key, iv.slice(0,16));
    var encrypted = cipher.update(data, 'binary') + cipher.final('binary');
    var encoded = new Buffer(encrypted, 'binary').toString('base64');

    callback(encoded);
};

var decrypt = function (input, password, callback) {
    // Convert urlsafe base64 to normal base64
    var input = input.replace('-', '+').replace('/', '_');
    // Convert from base64 to binary string
    var edata = new Buffer(input, 'base64').toString('binary')

    // Create key from password
    var m = crypto.createHash('md5');
    m.update(password)
    var key = m.digest('hex');

    // Create iv from password and key
    m = crypto.createHash('md5');
    m.update(password + key)
    var iv = m.digest('hex');

    // Decipher encrypted data
    var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv.slice(0,16));
    var decrypted = decipher.update(edata, 'binary') + decipher.final('binary');
    var plaintext = new Buffer(decrypted, 'binary').toString('utf8');

    callback(plaintext);
};

encrypt(input, password, function (encoded) {
    console.log(encoded);
    decrypt(encoded, password, function (output) {
        console.log(output);
    });
});

This produces the output:

BXSGjDAYKeXlaRXVVJGuREKTPiiXeam8W9e96Nknt3E=
hello world 

In python

from Crypto.Cipher import AES
from hashlib import md5
import base64

password = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
input = 'hello world'

def _encrypt(data, nonce, password):
    m = md5()
    m.update(password)
    key = m.hexdigest()

    m = md5()
    m.update(password + key)
    iv = m.hexdigest()

    # pad to 16 bytes
    data = data + " " * (16 - len(data) % 16)

    aes = AES.new(key, AES.MODE_CBC, iv[:16])

    encrypted = aes.encrypt(data)
    return base64.urlsafe_b64encode(encrypted)

def _decrypt(edata, nonce, password):
    edata = base64.urlsafe_b64decode(edata)

    m = md5()
    m.update(password)
    key = m.hexdigest()

    m = md5()
    m.update(password + key)
    iv = m.hexdigest()

    aes = AES.new(key, AES.MODE_CBC, iv[:16])
    return aes.decrypt(edata)

output = _encrypt(input, "", password) 
print(output)
plaintext = _decrypt(output, "", password)
print(plaintext)

This produces the output

BXSGjDAYKeXlaRXVVJGuRA==
hello world 

Clearly they are very close, but node seems to be padding the output with something. Any ideas how I can get the two to interoperate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:17:33+00:00Added an answer on June 3, 2026 at 10:17 pm

    OK, I’ve figured it out, node uses OpenSSL which uses PKCS5 to do padding. PyCrypto doesn’t handle the padding so I was doing it myself just add ‘ ‘ in both.

    If I add PKCS5 padding in the python code and remove the padding in the node code, it works.

    So updated working code.
    Node:

    var crypto = require('crypto');
    
    var password = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
    var input = 'hello world';
    
    var encrypt = function (input, password, callback) {
        var m = crypto.createHash('md5');
        m.update(password)
        var key = m.digest('hex');
    
        m = crypto.createHash('md5');
        m.update(password + key)
        var iv = m.digest('hex');
    
        var data = new Buffer(input, 'utf8').toString('binary');
    
        var cipher = crypto.createCipheriv('aes-256-cbc', key, iv.slice(0,16));
        
        // UPDATE: crypto changed in v0.10
        // https://github.com/joyent/node/wiki/Api-changes-between-v0.8-and-v0.10 
        var nodev = process.version.match(/^v(\d+)\.(\d+)/);
        var encrypted;
    
        if( nodev[1] === '0' && parseInt(nodev[2]) < 10) {
            encrypted = cipher.update(data, 'binary') + cipher.final('binary');
        } else {
            encrypted = cipher.update(data, 'utf8', 'binary') + cipher.final('binary');
        }
    
        var encoded = new Buffer(encrypted, 'binary').toString('base64');
    
        callback(encoded);
    };
    
    var decrypt = function (input, password, callback) {
        // Convert urlsafe base64 to normal base64
        var input = input.replace(/\-/g, '+').replace(/_/g, '/');
        // Convert from base64 to binary string
        var edata = new Buffer(input, 'base64').toString('binary')
        
        // Create key from password
        var m = crypto.createHash('md5');
        m.update(password)
        var key = m.digest('hex');
    
        // Create iv from password and key
        m = crypto.createHash('md5');
        m.update(password + key)
        var iv = m.digest('hex');
    
        // Decipher encrypted data
        var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv.slice(0,16));
    
        // UPDATE: crypto changed in v0.10
        // https://github.com/joyent/node/wiki/Api-changes-between-v0.8-and-v0.10 
        var nodev = process.version.match(/^v(\d+)\.(\d+)/);
        var decrypted, plaintext;
    
        if( nodev[1] === '0' && parseInt(nodev[2]) < 10) {  
            decrypted = decipher.update(edata, 'binary') + decipher.final('binary');    
            plaintext = new Buffer(decrypted, 'binary').toString('utf8');
        } else {
            plaintext = (decipher.update(edata, 'binary', 'utf8') + decipher.final('utf8'));
        }
    
        callback(plaintext);
    };
    
    encrypt(input, password, function (encoded) {
        console.log(encoded);
        decrypt(encoded, password, function (output) {
            console.log(output);
        });
    });
    

    Python:

    from Crypto.Cipher import AES
    from hashlib import md5
    import base64
    
    password = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    input = 'hello world'
    
    BLOCK_SIZE = 16
    
    def pad (data):
        pad = BLOCK_SIZE - len(data) % BLOCK_SIZE
        return data + pad * chr(pad)
    
    def unpad (padded):
        pad = ord(chr(padded[-1]))
        return padded[:-pad]
    
    def get_key_iv (password):
        m = md5()
        m.update(password.encode('utf-8'))
        key = m.hexdigest()
    
        m = md5()
        m.update((password + key).encode('utf-8'))
        iv = m.hexdigest()
        
        return [key,iv]
    
    def _encrypt(data, password):
    
        key,iv = get_key_iv(password)
        data = pad(data)
    
        aes = AES.new(key, AES.MODE_CBC, iv[:16])
    
        encrypted = aes.encrypt(data)
        return base64.urlsafe_b64encode(encrypted)
    
    def _decrypt(edata, password):
        edata = base64.urlsafe_b64decode(edata)
        key,iv = get_key_iv(password)
    
        aes = AES.new(key, AES.MODE_CBC, iv[:16])
        return unpad(aes.decrypt(edata))
    
    
    output = _encrypt(input, password) 
    print(output)
    plaintext = _decrypt(output, password)
    print(plaintext)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.