• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Question AES Encrypt In Javascript And Decrypt In C#

joaocabaco

New member
Joined
Sep 5, 2013
Messages
1
Programming Experience
1-3
Hello,

I'm having a hard time decrypting in C# data that was encrypted on client-side using CryptoJS.

Javascript Code:

Code:
[FONT=arial]<script src="[URL]http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js[/URL]"></script>[/FONT]
[FONT=arial]<script>[/FONT]
[FONT=arial]
[/FONT]
[FONT=arial]    var key = CryptoJS.enc.Utf8.parse('AMINHAKEYTEM32NYTES1234567891234');[/FONT]
[FONT=arial]    var iv = CryptoJS.enc.Utf8.parse('7061737323313233');[/FONT]
[FONT=arial]    var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("It works"), "Secret Passphrase", key, [/FONT]
[FONT=arial] { [/FONT]
[FONT=arial]     keySize: 128,[/FONT]
[FONT=arial]     iv: iv, [/FONT]
[FONT=arial]     mode: CryptoJS.mode.CBC,[/FONT]
[FONT=arial]     padding: CryptoJS.pad.Pkcs7 [/FONT]
[FONT=arial] }); [/FONT]
[FONT=arial]
[/FONT]
[FONT=arial]    </script>[/FONT]

Results from javascript:

//////////////DATA FROM CRYPTOJS
//enc:U2FsdGVkX19cNNeEUTEspBKuiUus3EFrkTElHDyZd54=
//key:48390e5076d5f67516b2fd776d1f799b4a61972b2fcaf27d362802c00a00c9c7
//salt:5c34d78451312ca4
//iv:c3ed8b19df64a88048bd30e7f82db106
//

Code C#:

Code:
            //////////////DATA FROM CRYPTOJS
            //enc:U2FsdGVkX19cNNeEUTEspBKuiUus3EFrkTElHDyZd54=
            //key:48390e5076d5f67516b2fd776d1f799b4a61972b2fcaf27d362802c00a00c9c7
            //salt:5c34d78451312ca4
            //iv:c3ed8b19df64a88048bd30e7f82db106
            //


            string encrypted = "U2FsdGVkX19cNNeEUTEspBKuiUus3EFrkTElHDyZd54=";


            string decrypted = Decrypt<AesManaged>(encrypted, "Secret Passphrase", "5c34d78451312ca4");


public static string Decrypt<T>(string text, string password, string salt)
           where T : SymmetricAlgorithm, new()
        {
            DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));


            SymmetricAlgorithm algorithm = new T();


            algorithm.Key = Encoding.UTF8.GetBytes("AMINHAKEYTEM32NYTES1234567891234");
            algorithm.IV = Encoding.UTF8.GetBytes("7061737323313233");


            byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
            byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);


            ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);


            using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
            {
                using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }
Error Printscreen:
Imageshack - y1ch.png
 
Top Bottom