CrazyDog
CrazyDog

Reputation: 321

Different encryption in Crypto.js and .NET Cryptography using AES

I am trying to encrypt a message from client and decrypt it on the server. I put the AES key and iv in users cookies.

The problem is that the encrypted string from Crypto.js is G0eNQap/h6u+7566MTOH3w==, and the encrypted string from .NET is F7RemlJeNBhcaZ/FjCK4xw==. It has the same length, but not the same value.

I gues I am doing something wrong with encoding. Could you point out the mistake? Thanks in advance.

Crypto.js

var communicationKey = CryptoJS.enc.Base64.parse(getCookie("SessionKey"));
    var communicationIV = CryptoJS.enc.Base64.parse(getCookie("IV"));

var encrypted = CryptoJS.AES.encrypt("Message", communicationKey, {
        iv: communicationIV,
        mode: CryptoJS.mode.CFB
    });

console.log("Result: " + CryptoJS.enc.Base64.stringify(encrypted.ciphertext));

.NET:

string key = context.Cookies["SessionKey"].Value;
newUser.UserKey = Convert.FromBase64String(key);

string iv = context.Cookies["IV"].Value;
newUser.InitializationVector = Convert.FromBase64String(iv);

byte[] encryptedMessage = EncryptStringToBytes_Aes("Message", source.UserKey, source.InitializationVector);

Upvotes: 3

Views: 2226

Answers (1)

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

In your js code you are using CryptoJS.mode.CFB.

If your EncryptStringToBytes_Aes is exact copy of MSDN sample - then it uses CBC AES encryption mode (it is default for AESManaged).

So you have to change either js or C# code for both of them use the same encryption mode.

Upvotes: 3

Related Questions