Howdy, Stranger!

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

Categories

Encrypt (AES) in Javascript and decrypt in .NET

knrao_1knrao_1 Posts: 1Member
Hi All,
With the help of following link,
http://www.hanewin.net/encrypt/aes/aes.htm
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...
Code:

For encryption in Javascript I am using following code,

var



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

definition for the rijndaelEncrypt() can be found in env-test.js of following link,
http://www.hanewin.net/encrypt/aes/aes.htm

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;

}

-----------------------------------------------------------------------------------------


static




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");




try



MemoryStream ms = new MemoryStream();



Rijndael alg = Rijndael.Create();

alg.KeySize = 128;

alg.Mode =


CipherMode.CBC;




alg.BlockSize = 128;

alg.Key = Key;

alg.IV = IV;



CryptoStream cs = new CryptoStream(ms,

alg.CreateDecryptor(),


CryptoStreamMode.Write);




// Write the data and make it do the decryption

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



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

cs.Close();

byteArrayDecryptKey = ms.ToArray();

bRetVal =

true;

}




catch (Exception ex)

{

}




return bRetVal;

}



Narasimha
Sign In or Register to comment.