Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Encrypt (AES) in Javascript and decrypt in .NET

knrao_1knrao_1 Member Posts: 1
Hi All,
With the help of following link,
I encrypted(AES) data with Javascript . I want to decrypt this in c#. While decrypting in c3 I am getting error "length of the data to be decrypt is not valid" Can any one provide some example?
Thanks for your help...

For encryption in Javascript I am using following code,


var encByteData = byteArrayToHex(rijndaelEncrypt(strFileContent, hex2s(randomnumber), "CBC"));

definition for the rijndaelEncrypt() can be found in env-test.js of following link,

For the decryption in c# I am using following code,

Calling function,

DecryptData(byteArraySignData, "91860F52E5C3A09BA3B827F28070E08D",

ref byteArrayDecryptedData);

public static bool EncryptData(byte[] clearData, string Password, ref byte[] byteArrayEncryptedData)


PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,

new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,

0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

EncryptData(clearData, pdb.GetBytes(32), pdb.GetBytes(16),

ref byteArrayEncryptedData);

return true;




bool DecryptData(byte[] byteArrayEncryptedData, byte[] Key, byte[] IV, ref byte[] byteArrayDecryptKey)


bool bRetVal = false;

// Check arguments.

if (byteArrayEncryptedData == null || byteArrayEncryptedData.Length <= 0)

throw new ArgumentNullException("ByteArray");

if (Key == null || Key.Length <= 0)

throw new ArgumentNullException("Key");

if (IV == null || IV.Length <= 0)

throw new ArgumentNullException("IV");


MemoryStream ms = new MemoryStream();

Rijndael alg = Rijndael.Create();

alg.KeySize = 128;

alg.Mode =


alg.BlockSize = 128;

alg.Key = Key;

alg.IV = IV;

CryptoStream cs = new CryptoStream(ms,



// Write the data and make it do the decryption

cs.Write(byteArrayEncryptedData, 0, byteArrayEncryptedData.Length);

// Close the crypto stream (or do FlushFinalBlock).


byteArrayDecryptKey = ms.ToArray();

bRetVal =



catch (Exception ex)



return bRetVal;


Sign In or Register to comment.