0

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>
  • 1
    are you getting any error in Javascript code? Can you share the error here please? – Chetan May 19 '20 at 09:05
  • Are you sure you are using same Key in both C# and Javascript code? – Chetan May 19 '20 at 09:08
  • I am using the same key in C# and as well in javascript application. – Chiranjeevi Rudregowda May 19 '20 at 09:09
  • I am getting the error as "Uncaught Error: Malformed UTF-8 data" – Chiranjeevi Rudregowda May 19 '20 at 09:11
  • Which line in the code gives the error? Did you look at https://stackoverflow.com/questions/58111929/why-i-get-malformed-utf-8-data-error-on-crypto-js? – Chetan May 19 '20 at 09:12
  • 1
    Note that you haven't set the IV on the javascript side, and there seems to be no consideration given to mdoe and padding. – ProgrammingLlama May 19 '20 at 09:13
  • This is the line where I am getting error var decryptedMessage = decrypted.toString(CryptoJS.enc.Utf8); and I am not always getting error because sometimes decryptedMessage itself will be empty.(I have written decrypt logic on load and as this static page when I refresh sometimes I don’t see error] – Chiranjeevi Rudregowda May 19 '20 at 09:18
  • In C# we are passing IV as empty byte array as below `byte[] iv = new byte[];` As this empty array, I am not using in javascript. Do you want still use Iv in Javascript? – Chiranjeevi Rudregowda May 19 '20 at 09:21
  • yeah went through link @ChetanRanpariya. It does not work for me. I am using the only Javascript – Chiranjeevi Rudregowda May 19 '20 at 09:24
  • @John .Thank you for the input. I have tried with iv and mode and padding `var decrypted = CryptoJS.AES.decrypt(cipherParams, secretkey, {iv:bytesiv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });`. But still i am getting the same error – Chiranjeevi Rudregowda May 19 '20 at 10:10

1 Answers1

0

The problem could be that the strings in C# are encoded in UTF-16 Try to change the encoding in the JavaScript code, if possible.

  • Thank you for input. I did the code change as below ` var decryptedMessage = decrypted.toString(CryptoJS.enc.Utf16);` now i am not getting error but not getting the proper output on decrypted message . I am getting some random values – Chiranjeevi Rudregowda May 19 '20 at 10:26