AES encrypt in Javascript and decrypt in C#

  • Thread starter Thread starter João Cabaço
  • Start date Start date
J

João Cabaço

Guest
Hello,
I'm having some difficulty decrypt data that was encrypted in javascript.

When I debug in C# I can't see the message decrypted.

I can provide more information if necessary.

Below is the code

Javascript:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>

var key = CryptoJS.enc.Utf8.parse('7061737323313233');
var iv = CryptoJS.enc.Utf8.parse('7061737323313233');
var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("It works"), "Secret Passphrase", key,
{
keySize: 128 / 8,
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});

var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase", key, {
keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });


var options = { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 };
var encrypted = CryptoJS.AES.encrypt("It works", "Secret Passphrase", options);
var seg = encrypted.ciphertext.toString(CryptoJS.enc.Base64);


var enc = encrypted;
var enckey = encrypted.key;
var encsalt = encrypted.salt;
var enciv = encrypted.iv;


alert('Encrypted :' + encrypted);
alert('Key :' + encrypted.key);
alert('Salt :' + encrypted.salt);
alert('iv :' + encrypted.iv);
alert('Decrypted : ' + decrypted);
alert('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8));
alert(seg);

</script>


Results Javascript:

//enc: U2FsdGVkX19jUqTCz/gGd8JqXhyshwRtGnIwIpXN2Zg=
//enckey: 4536538a2f826d756115ceed14569e9eefe97a4c83dadc5b176b5af535a73148
//enciv: 6352a4c2cff80677
//encsalt: c984599f97b3abfe3d127a7d25b40303
//seg: wmpeHKyHBG0acjAilc3ZmA==


C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace bfDemoService
{
public class Codec
{

public static string DecryptStringAES()
{
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
string DataToEncrypt = "It works";


//Settings
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
myRijndael.FeedbackSize = 128;

byte[] keybytes = Convert.FromBase64String("4536538a2f826d756115ceed14569e9eefe97a4c83dadc5b176b5af535a73148");
byte[] iv = Convert.FromBase64String("6352a4c2cff80677");


//DECRYPT FROM CRIPTOJS
byte[] encrypted = Convert.FromBase64String("U2FsdGVkX19jUqTCz/gGd8JqXhyshwRtGnIwIpXN2Zg=");

// Decrypt the bytes to a string.
string roundtrip = DecryptStringFromBytes(encrypted, keybytes, iv);

return roundtrip;
}
}

static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");

// Declare the string used to hold
// the decrypted text.
string plaintext = null;

// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;

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

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

// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}

}

return plaintext;
}

static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.Mode = CipherMode.CBC;
rijAlg.Key = Key;
rijAlg.IV = IV;

// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{

//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}


// Return the encrypted bytes from the memory stream.
return encrypted;

}
}
}

Continue reading...
 
Back
Top