I have an existing encryption and decryption login inplemented in C# using RSC2-cbc algorithm using Key and IV. Now I am going to implement the same in node.js. So I have written following code to encryprt and decrypt. The problem I am facing is the node.js encrypted string (chiper) or decrypted string are not matching with C# encryptrd strings.
Existing C# code
byte[] arrbIV = Encoding.ASCII.GetBytes("dleftaba");
byte[] arrbKey = Encoding.ASCII.GetBytes(Key);
byte[] arrbData = Encoding.ASCII.GetBytes(sData); //Text to be encryptrd
RC2 oEncryptor = new RC2CryptoServiceProvider();
oEncryptor.Mode = CipherMode.CBC;
oEncryptor.Key = arrbKey;
oEncryptor.IV = arrbIV;
// Create memory stream to store encrypted string
MemoryStream oMemoryStream = new MemoryStream();
CryptoStream oCryptoStream = new CryptoStream(oMemoryStream, oEncryptor.CreateEncryptor(),
CryptoStreamMode.Write);
// Peform the encryption
oCryptoStream.Write(arrbData, 0, arrbData.Length);
// We have written all the data in the stream and now we can apply padding
oCryptoStream.Close();
string sRetVal = Convert.ToBase64String(oMemoryStream.ToArray());
Equivalent/ Translated node.js code
var crypto = require('crypto')
var SECRET_KEY = "435353553"
var IV = "dleftaba"
var ENCODING = 'base64'
var text = "My Text"
Encryption
var cipher = crypto.createCipheriv('rc2-cbc',key, iv)
var cryptedPassword = cipher.update(text, 'utf-8', 'base64')
cryptedPassword+= cipher.final('base64')
Decryption
var decipher = crypto.createDecipheriv('rc2-cbc', SECRET_KEY, IV)
var decryptedPassword = decipher.update(cryptedPassword, 'base64','utf-8')
decryptedPassword += decipher.final('utf-8')
Please suggest what is going wrong over here. Why node. js is not resulting into the identical chiper like C#.
Though not the same algorithm you are using... here's my reference implementation in C# ... I've put comments on what the node implementation should be... and it matches up...
My goal at the time was to keep the .Net side compatible with the node side's defaults...
Related:
Node.js
My .Net code references HashUtility which isn't strictly needed, and I don't have my full node.js implementation with scrypt available... It was mainly for testing against.