I have two system which are sharing a user database, so authentication needs to be the same.
The passwords are currently encrypted using C#'s Cryptography.Rijndael
(N.B. not RijndaelManaged). Using a custom key and iv (initialisation vector). (CBC mode and Pkcs7 padding)
The C# encryption is as follows:
Rijndael alg = Rijndael.Create();
alg.Key = key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encryptedData = ms.ToArray();
key
is 256 bits (32 bytes) and iv
(initialisation vector) is 128 bits (16 bytes). The block size is 128 bits (16 bytes).
key and iv are a byte arrays from a base64 strings via:
byte[] key = Convert.FromBase64String(base64Key);
byte[] iv = Convert.FromBase64String(base64IV);
N.B. I have no control on the C# code (legacy system).
On the javascript based system I have to encrypt the passwords exactly the same way. I've tried using node crypto-js
to no avail.
my code looks something like this:
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(password), keyCodeWords, {
iv: ivCodeWords,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
keyCodeWords and ivCodeWords are CryptoJS code words from the same base64 key and iv as follows:
var keyCodeWords = CryptoJS.enc.Base64.parse(base64Key);
var ivCodeWords = CryptoJS.enc.Base64.parse(base64IV);
The encryption does work (in the sense that I can encrypt and then decrypt to get the same value). However, the issue is that the encrypted value (encrypted.ciphertext
) is not the same as the C# one, so I'm unable to authenticate.
The comparison is done base comparing the base64 encrypted output strings.
How do I make crypto-js (or any other javascript encryption library) consistent with C# Rijndael?
Rijndael alg = Rijndael.Create(); alg.Key = key; alg.IV = IV; CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(clearData, 0, clearData.Length); cs.Close(); byte[] encryptedData = ms.ToArray();
Thekey
is 256 bits, the `iv' is 128 bits. Block size is 128. Unfortunately, I have no control on the C# implementation (legacy system) - I also would have just used hashing. - Kholofelo