Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Decrypting AES256 encrypted data in .NET from node.js - how to obtain IV and Key from passphrase

Decrypting AES256 encrypted data in .NET from node.js - how to obtain IV and Key from passphrase

Problem

I have the following code to encrypt/decrypt data in node.js, which is working:

var cipher = crypto.createCipher('aes256', 'passphrase');  
var encrypted = cipher.update("test", 'utf8', 'base64') + cipher.final('base64');

var decipher = crypto.createDecipher('aes256', 'passphrase');   
var plain = decipher.update(encrypted, 'base64', 'utf8') + decipher.final('utf8');

I would like to be able to do the same in C#/.NET so that I can share data between two seperate systems. However the code that I have seen in .NET requires a Key and IV to entrypt/decrypt. How are these derived from the Passphrase in the node.js Crypto library?

Problem courtesy of: Tom Haigh

Solution

From the node.js source I found this:

 bool CipherInit(char* cipherType, char* key_buf, int key_buf_len) {
cipher = EVP_get_cipherbyname(cipherType);
if(!cipher) {
  fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType);
  return false;
}

unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
int key_len = EVP_BytesToKey(cipher, EVP_md5(), NULL,
  (unsigned char*) key_buf, key_buf_len, 1, key, iv);

I found a c# implementation of EVP_BytesToKey in this question which can be used like this:

byte[] key, iv;
DeriveKeyAndIV(Encoding.ASCII.GetBytes("passphrase"),null, 1, out key, out iv);

                     //this is what node.js gave me as the base64 encrypted data
var encrytedBytes = Convert.FromBase64String("b3rbg+mniw7p9aiPUyGthg==");

The key and IV can then be used in an instance of RijndaelManaged to decrypt encrytedBytes

Solution courtesy of: Tom Haigh

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Decrypting AES256 encrypted data in .NET from node.js - how to obtain IV and Key from passphrase

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×