简体   繁体   English

使用AES,Crypto.js和.NET的Websockets聊天消息加密

[英]Websockets chat message encryption with AES, Crypto.js and .NET

So, I've developed a user chat using Websockets, with ASP.MVC on the server. 所以,我使用Websockets开发了一个用户聊天,服务器上有ASP.MVC。

I wanted to encrypt all messages (using AES ) sent and received from websockets. 我想加密从websockets发送和接收的所有消息(使用AES )。 To do so, I tried to encrypt the user message before sending ( using Crypto.js ) and decrpyt it on the server (using Security.Cryptography .net ). 为此,我尝试在发送( 使用Crypto.js )之前加密用户消息并在服务器上解密(使用Security.Cryptography .net )。

The problem is that the encrypted message on the client is different from the encrypted message on the server (with message,key and initialization vector being the same on the client and the user). 问题是客户端上的加密消息与服务器上的加密消息不同(消息,密钥和初始化向量在客户端和用户上是相同的)。

Is this a good way of doing the websockets message encrypting? 这是做websockets消息加密的好方法吗? What other solutions would you recommend me? 你会推荐我什么其他解决方案?

CryptoJS: CryptoJS:

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

.NET Cryptography: .NET加密:

 byte[] encryptedMessage = EncryptStringToBytes_Aes(decryptedMessage, keyToDecrypt, ivToDecrypt);
 return Convert.ToBase64String(encryptedMessage);

The Crypto.js encrypted string is: Crypto.js加密字符串是:

U2FsdGVkX18wnoGfYzHo2Ms/6CKsRC+cE1fj8ylSPlI=

And the .NET`s Security.Cryptography is: 而.NET的Security.Cryptography是:

kLApirWt1VcVu3tTuAizgA==

I`m using the same key and initalization vector on both sides. 我在两侧使用相同的键和初始化矢量。 What could be the problem? 可能是什么问题呢?

I assume that you want to use CFB mode, because you reference this in your JavaScript code and EncryptStringToBytes_Aes already does this. 我假设您要使用CFB模式,因为您在JavaScript代码中引用了它,而EncryptStringToBytes_Aes已经这样做了。

Put the mode into the first config object. 将模式放入第一个配置对象。 There is no second one: 没有第二个:

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

Also, CryptoJS formats the output using an OpenSSLFormatter. 此外,CryptoJS使用OpenSSLFormatter格式化输出。 It includes a salt in there if you use a password based encryption, but it seems that you don't have one. 如果您使用基于密码的加密,它包含一个盐,但似乎您没有。 If you want to make sure that only the ciphertext is exchanged, encode it as CryptoJS.enc.Base64.stringify(encrypted.ciphertext) into Base64 instead of using encrypted.toString() . 如果要确保只交换密文,请将其编码为CryptoJS.enc.Base64.stringify(encrypted.ciphertext)到Base64,而不是使用encrypted.toString()

Don't forget to include mode-cfb.js in your page if you're using the aes rollup, because only CBC is included in there. 如果您正在使用aes汇总,请不要忘记在您的页面中包含mode-cfb.js,因为那里只包含CBC。

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

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