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

  • Home
  • SEARCH
  • 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 8837623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:46:26+00:00 2026-06-14T09:46:26+00:00

So in node.js I have an encryption scheme using aes128 and without passing IV.

  • 0

So in node.js I have an encryption scheme using aes128 and without passing IV. An example being the following

 var cipher = require('crypto').createCipher('aes128','password');
 cipher.update('test','utf8','base64')+cipher.final('base64');

which outputs CjZ3R/tW8jiyLvg+/TE6bA==. The issue is that I can’t reproduce this in PHP even though I’ve tried a bunch of implementations and variations.

When I then try running this through openssl, I run the following:

 echo "test" | openssl enc -e -aes128 -a -k password

which outputs U2FsdGVkX19Ccfv3SWvuzuZWeov9GDuwx1RMK2HWa/s= which also doesn’t match either. I have also tried using the -md with all possible options and that didn’t match either (they aren’t even the same block size). When running in PHP I’ve tried hashing the key/not hashing, using padding, using the computed padding on the input where the padded character is the number of padded characters needed (saw something online saying this is what nodejs uses). And I have tried setting the IV to the password and also to null bytes but still I can’t get a match.

Any suggestions/ideas here?

edit:

So I just discovered the function openssl_encrypt in php and tried the same test through that and once again got a completely different output (this one now takes exactly the same args as node.js and supposedly they both use openssl):

 openssl_encrypt ( "test" , 'aes128' , "password")

which outputs (already in base64) JleA91MvYHdEdnj3KYHmog== which is at least now matching in block count but still not the same ciphertext.

I should also mention that yes passing an IV to nodejs is an option and might resolve the discrepancy but this scheme in php will be replacing the old one in nodejs which is already live so it must be able to decrypt already created ciphertexts

  • 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-14T09:46:28+00:00Added an answer on June 14, 2026 at 9:46 am

    So I figured out the solution. After looking through the c++ source code of node.js I found that the key and iv are generated using the openssl function EVP_BytesToKey. After searching for an implementation of this function I found this stack overflow post Encrypting data in Cocoa, decoding in PHP (and vice versa) which contains a version of this function. Modifying that and adding the fact that the openssl version pads the data with the ascii character equal to the number of bytes needed for padding I came up with the following function which fully matched the nodejs encryption scheme:

    function aes128Encrypt($key, $data) {
        $padding = 16 - (strlen($data) % 16);
        $data .= str_repeat(chr($padding), $padding);
    
        $keySize   = 16;
        $ivSize    = 16;
    
        $rawKey = $key;
        $genKeyData = '';
        do
        {
            $genKeyData = $genKeyData.md5( $genKeyData.$rawKey, true );
        } while( strlen( $genKeyData ) < ($keySize + $ivSize) );
    
        $generatedKey = substr( $genKeyData, 0, $keySize );
        $generatedIV  = substr( $genKeyData, $keySize, $ivSize );
    
        print($generatedIV);
        print($generatedKey);
    
        return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $generatedKey, $data, MCRYPT_MODE_CBC, $generatedIV);
    }
    

    which matches exactly with the node.js function:

     function aes128Encrypt(key,data) {
          var cipher = require('crypto').createCipher('aes128',key);
          return cipher.update(data,'utf8','binary')+cipher.final('binary');
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Edited to greatly simplify) On node I have the following server.js file. var Backbone
Let me explain best with an example. Say you have node class that can
I have to do something like this with Node.js (crypto module). http://www.devco.net/archives/2006/02/13/public_-_private_key_encryption_using_openssl.php Is this
I have node.js REST api, which returns following object [ { word: word },
I have the following xml: <root> <node> <tag1/> <tag2/> <tag3/> </node> <node> <tag1/> <tag2/>
I have node.js modules in several directories because I am following MVC pattern. I
I have the following situation: For ech tree node I have a different type
I have a binary tree where each node can have a value. I want
i have this problem to find a particular xml node l have post this
I want to have a tree in memory, where each node can have multiple

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.