I am trying to decrypt the string in JavaScript which is encrypted by using AES 256 algorithm in a C# application. The code of encryption and decryption is as below I am able to decrypt the string in a C# application. I used the below code to decrypt the string JavaScript but I am not able to decrypt
public string Encrypt(string content)
{
if (string.IsNullOrEmpty(content))
{
throw new ArgumentNullException("content");
}
byte[] encryptedData = null;
try
{
using (AesCryptoServiceProvider aesMod = new AesCryptoServiceProvider())
{
//Set the key manullay to predefined values
aesMod.Key = m_Key;
aesMod.IV = m_IV;
ICryptoTransform encryptor = aesMod.CreateEncryptor(aesMod.Key, aesMod.IV);
// Create the streams used for encryption.
using (MemoryStream memstreamEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(memstreamEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Writing data to the stream.
swEncrypt.Write(content);
}
encryptedData = memstreamEncrypt.ToArray();
}
}
}
return Convert.ToBase64String(encryptedData);
}
catch (Exception ex)
{
throw new Exception("Exception in Encrypting .", ex);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script>
function decryptMessage(encryptedMessage = '', secretkey = ''){
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(encryptedMessage)
});
var decrypted = CryptoJS.AES.decrypt(cipherParams, secretkey);
var decryptedMessage = decrypted.toString(CryptoJS.enc.Utf8);
return decryptedMessage;
}
</script>