Not having the same result between AES of javascript cryptojs and AES C# RijndaelManaged

276 views Asked by At

i have a mobile app developed on ionic and i have this portion of code that get a base64 string and encrypt it (the same thing for decryption) here is the code

     globalEncrypt(input: string): string {
    return crypto.AES.encrypt(input, '****************').toString();
  }
  
  globalDecrypt(input: string):string {
    return crypto.AES.decrypt(input, '****************', {
      iv: '****************',
      mode: crypto.mode.CBC,
      padding: crypto.pad.Pkcs7
    }).toString(crypto.enc.Utf8);
  }

NB: '****************' are strings of 16 length but note the same (key <> Iv).

this works fine.

the problème is that i tried to use the same AES in C# with the same configuration but i don't get the same result.

        public static string DecryptStringFromBytes(String TextBase64)
    {
            
            byte[] cipherText = Encoding.UTF8.GetBytes(TextBase64);  
            string plaintext = null;

            // Create an RijndaelManaged object  
            // with the specified key and IV.  
            using (var rijAlg = new RijndaelManaged())
            {
                //Settings  
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.PKCS7;
                rijAlg.FeedbackSize = 128;

                rijAlg.Key = Encoding.UTF8.GetBytes("****************");
                rijAlg.IV = Encoding.UTF8.GetBytes("****************"); ;

                // Create a decrytor to perform the stream transform.  
                var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                try
                {
                    // Create the streams used for decryption.  
                    using (var msDecrypt = new MemoryStream(cipherText))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {

                            using (var srDecrypt = new StreamReader(csDecrypt))
                            {
                                // Read the decrypted bytes from the decrypting stream  
                                // and place them in a string.  
                                plaintext = srDecrypt.ReadToEnd();

                            }

                        }
                    }
                }
                catch
                {
                    plaintext = "keyError";
                }
            }

            return plaintext;

    }

Any help will appreciated. Thank you

0

There are 0 answers