I need to convert an Rijndael encryption function from C# to Node. But I cannot match the result, even with the same Key, IV, Mode and Block Size. What am I doing wrong?
C# MRE:
using System.Security.Cryptography;
byte[] encrypted;
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = new byte[] { 63, 115, 38, 206, 45, 102, 229, 13,
161, 196, 250, 133, 149, 70, 153, 33,
218, 32, 135, 149, 150, 21, 143, 11,
210, 115, 185, 163, 24, 70, 145, 141 };
rijAlg.IV = new byte[] { 151, 121, 168, 83, 221, 99, 206, 230, 74, 190, 106, 212, 232, 117, 192, 37 };
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write("1234");
}
encrypted = msEncrypt.ToArray();
}
}
Console.WriteLine(rijAlg.BlockSize); //128
Console.WriteLine(rijAlg.Mode); //CBC
Console.WriteLine(rijAlg.Padding); //PKCS7
Console.WriteLine(Convert.ToBase64String(encrypted)); //6d9SB6t9dktDJ+siSlFtOQ==
}
Node / Javascript MRE:
import Rijndael from "rijndael-js";
const key = Buffer.from([63, 115, 38, 206, 45, 102, 229, 13, 161, 196, 250, 133, 149, 70, 153, 33, 218, 32, 135, 149, 150, 21, 143, 11, 210, 115, 185, 163, 24, 70, 145, 141]);
const IV = Buffer.from([151, 121, 168, 83, 221, 99, 206, 230, 74, 190, 106, 212, 232, 117, 192, 37]);
const cipher = new Rijndael(key, "cbc");
const buffer = cipher.encrypt("1234", "128", IV);
console.log(Buffer.from(buffer).toString("base64")); //6ewzPyaxUgFX8IXW9iLJfw==
The result of encrypting "1234" in C# is "6d9SB6t9dktDJ+siSlFtOQ==" while in Node is "6ewzPyaxUgFX8IXW9iLJfw=="
This can be a bit tricky due to differences in how each environment handles encryption, especially with things like padding and block sizes.
In your C# code, you're using Rijndael with a block size of 128 bits (as indicated by rijAlg.BlockSize output), and the padding mode is PKCS7. This is pretty standard for Rijndael/AES encryption.
However, in your Node.js code, when you use rijndael-js and call cipher.encrypt, you're specifying "256" as the block size. This might be causing the mismatch. Rijndael supports various block sizes, but AES, which is a subset of Rijndael, is fixed at a block size of 128 bits.
So, a couple of things you might want to check:
Try aligning these parameters and see if that resolves the discrepancy. Encryption can be a bit finicky with these details, so it's all about making sure everything lines up perfectly.
Hope this helps and best of luck with your project!