Recently, I have implemented a user chat system using Websockets and ASP.MVC on the server.
My goal was to ensure all messages sent and received through Websockets are encrypted (using AES). To achieve this, I decided to encrypt user messages before sending them (using Crypto.js) and decrypt them on the server side (using Security.Cryptography .net).
However, I encountered an issue where the encrypted message on the client does not match the encrypted message on the server, despite using the same message, key, and initialization vector. This has led me to question whether this method of encrypting Websockets messages is effective. Are there any alternative solutions you would recommend?
Here is the encryption process using CryptoJS:
var encrypted = CryptoJS.AES.encrypt("Message", communicationKey, { iv : communicationIV}, { mode: CryptoJS.mode.CFB });
And here is the encryption process in .NET Cryptography:
byte[] encryptedMessage = EncryptStringToBytes_Aes(decryptedMessage, keyToDecrypt, ivToDecrypt);
return Convert.ToBase64String(encryptedMessage);
The result of the CryptoJS encryption is:
U2FsdGVkX18wnoGfYzHo2Ms/6CKsRC+cE1fj8ylSPlI=
While the result of the .NET's Security.Cryptography encryption is:
kLApirWt1VcVu3tTuAizgA==
I am certain that I am using the same key and initialization vector on both ends. What could be causing this discrepancy in the encrypted messages?