简体   繁体   English

使用AES在Crypto.js和.NET Cryptography中进行不同的加密

[英]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. 我把AES密钥和iv放在用户cookie中。

The problem is that the encrypted string from Crypto.js is G0eNQap/h6u+7566MTOH3w== , and the encrypted string from .NET is F7RemlJeNBhcaZ/FjCK4xw== . 问题是来自Crypto.js的加密字符串是G0eNQap/h6u+7566MTOH3w== ,而来自.NET的加密字符串是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 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);

In your js code you are using CryptoJS.mode.CFB . 在您的js代码中,您使用的是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). 如果您的EncryptStringToBytes_Aes是MSDN示例的精确副本 - 那么它使用CBC AES加密模式(它是AESManaged的默认模式)。

So you have to change either js or C# code for both of them use the same encryption mode. 因此,您必须更改js或C#代码,因为它们都使用相同的加密模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM