We need to include aes.js and pbkdf2.js javascript files to use this code. These files can be downloaded from code.google.com/p/crypto-js
function EncryptData() { var PlainText = document.getElementById('TextOriginal').value; var encryptData = document.getElementById('TextEncryptedJS'); try { //Creating the Vector Key var iv = CryptoJS.enc.Hex.parse('a5s8d2e9c1721ae0e84ad660c472y1f3'); //Encoding the Password in from UTF8 to byte array var Pass = CryptoJS.enc.Utf8.parse('Crypto'); //Encoding the Salt in from UTF8 to byte array var Salt = CryptoJS.enc.Utf8.parse("cryptography123example"); //Creating the key in PBKDF2 format to be used during the encryption var key128Bits1000Iterations = CryptoJS.PBKDF2(Pass.toString(CryptoJS.enc.Utf8), Salt, { keySize: 128 / 32, iterations: 1000 }); //Encrypting the string contained in cipherParams using the PBKDF2 key var encrypted = CryptoJS.AES.encrypt(PlainText, key128Bits1000Iterations, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 }); encryptData.value = encrypted.ciphertext.toString(CryptoJS.enc.Base64); } //Malformed UTF Data due to incorrect password catch (err) { return ""; } }
To Decrypt this string in C# we need to add a class file named “ClsCrypto.cs”. Here is the code for ClsCrypto class :
public class ClsCrypto { private RijndaelManaged myRijndael = new RijndaelManaged(); private int iterations; private byte [] salt; public ClsCrypto(string strPassword) { myRijndael.BlockSize = 128; myRijndael.KeySize = 128; myRijndael.IV = HexStringToByteArray("a5s8d2e9c1721ae0e84ad660c472y1f3"); myRijndael.Padding = PaddingMode.PKCS7; myRijndael.Mode = CipherMode.CBC; iterations = 1000; salt = System.Text.Encoding.UTF8.GetBytes("cryptography123example"); myRijndael.Key = GenerateKey(strPassword); } public string Encrypt(string strPlainText) { byte [] strText = new System.Text.UTF8Encoding().GetBytes(strPlainText); ICryptoTransform transform = myRijndael.CreateEncryptor(); byte [] cipherText = transform.TransformFinalBlock(strText, 0, strText.Length); return Convert.ToBase64String(cipherText); } public string Decrypt(string encryptedText) { var encryptedBytes = Convert.FromBase64String(encryptedText); ICryptoTransform transform = myRijndael.CreateDecryptor(); byte[] cipherText = transform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); return System.Text.Encoding.UTF8.GetString(cipherText); } public static byte [] HexStringToByteArray(string strHex) { dynamic r = new byte[strHex.Length / 2]; for (int i = 0; i <= strHex.Length - 1; i += 2) { r[i/2] = Convert.ToByte(Convert.ToInt32(strHex.Substring(i, 2), 16)); } return r; } private byte[] GenerateKey(string strPassword) { Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(System.Text.Encoding.UTF8.GetBytes(strPassword), salt, iterations); return rfc2898.GetBytes(128 / 8); } }
In ClsCrypto method we have a parameter for “strPassword”, it must be the same as we have in javascript code.
ClsCrypto crypto = new ClsCrypto("Crypto"); string DecrypedString = crypto.Decrypt("Encrypted Text");