ÿþ<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>System.Security.Cryptography.AES</title> <link type="text/css" rel="stylesheet" href="../System.Web.UI.Interface.debug.css" /> <script type="text/javascript" language="JavaScript" src="../System.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.IO.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.Text.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.Convert.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.BitConverter.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.Security.Cryptography.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.Security.Cryptography.SHA1.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.Security.Cryptography.HMACSHA1.debug.js"></script> <script type="text/javascript" language="JavaScript" src="../System.Security.Cryptography.RijndaelManaged.debug.js"></script> <link rel="stylesheet" type="text/css" href="Styles/Default.css" /> <script type="text/javascript" language="javascript"> //#region Init Example function Window_Load() { Trace.IsEnabled = true; WriteLog("Start Demo"); $("TestAesButton").onclick = TestAesButton_Click; } window.onload = Window_Load; //#endregion //#region Log Function function WriteLog(text) { Trace.Write("<code>" + text + "</code>"); } function WriteEnc(input, output) { WriteLog("Transform: " + System.BitConverter.ToString(input, '') + " -> " + System.BitConverter.ToString(output, '')); } //#endregion //#region Shared Functions function SaltFromPassword(password) { /// <summary> /// Generate salt from password. /// </summary> /// <param name="password">Password string.</param> /// <returns>Salt bytes.</returns> var passwordBytes = System.Text.Encoding.UTF8.GetBytes(password); var hmac = new System.Security.Cryptography.HMACSHA1(passwordBytes); var salt = hmac.ComputeHash(passwordBytes); return salt; } function GetTransform(password, encrypt) { // Create an instance of the Rihndael class. var cipher = new System.Security.Cryptography.RijndaelManaged(); // Calculate salt to make it harder to guess key by using a dictionary attack. var salt = SaltFromPassword(password); WriteLog("// salt = " + System.BitConverter.ToString(salt, '')); // Generate Secret Key from the password and salt. // Note: Set number of iterations to 10 in order for JavaScript example to work faster. var secretKey = new System.Security.Cryptography.Rfc2898DeriveBytes(password, salt, 10); // Create a encryptor from the existing SecretKey bytes by using // 32 bytes (256 bits) for the secret key and // 16 bytes (128 bits) for the initialization vector (IV). var key = secretKey.GetBytes(32); var iv = secretKey.GetBytes(16); WriteLog("// key = " + System.BitConverter.ToString(key, '')); WriteLog("// iv = " + System.BitConverter.ToString(iv, '')); var cryptor = null; if (encrypt) { cryptor = cipher.CreateEncryptor(key, iv); } else { cryptor = cipher.CreateDecryptor(key, iv); } return cryptor; } function CipherStreamWrite(cryptor, input) { /// <summary> /// Encrypt/Decrypt with Write method. /// </summary> /// <param name="cryptor"></param> /// <param name="input"></param> /// <returns></returns> var inputBuffer = new System.Byte(input.length); // Copy data bytes to input buffer. System.Buffer.BlockCopy(input, 0, inputBuffer, 0, inputBuffer.length); // Create a MemoryStream to hold the output bytes. var stream = new System.IO.MemoryStream(); // Create a CryptoStream through which we are going to be processing our data. var mode = System.Security.Cryptography.CryptoStreamMode.Write; var cryptoStream = new System.Security.Cryptography.CryptoStream(stream, cryptor, mode); // Start the crypting process. cryptoStream.Write(inputBuffer, 0, inputBuffer.length); // Finish crypting. cryptoStream.FlushFinalBlock(); // Convert data from a memoryStream into a byte array. var outputBuffer = stream.ToArray(); // Close both streams. stream.Close(); cryptoStream.Close(); return outputBuffer; } //#endregion //#region AES-256 Encryption function EncryptToBase64(password, s) { /// <summary> /// Encrypt string with AES-256 by using password. /// </summary> /// <param name="password">Password string.</param> /// <param name="s">String to encrypt.</param> /// <returns>Encrypted Base64 string.</returns> // Turn input strings into a byte array. var bytes = System.Text.Encoding.Unicode.GetBytes(s); // Get encrypted bytes. var encryptedBytes = Encrypt(password, bytes); // Convert encrypted data into a base64-encoded string. var base64String = System.Convert.ToBase64String(encryptedBytes); // Return encrypted string. return base64String; } function Encrypt(password, bytes) { /// <summary> /// Encrypt string with AES-256 by using password. /// </summary> /// <param name="password">String password.</param> /// <param name="bytes">Bytes to encrypt.</param> /// <returns>Encrypted bytes.</returns> // Create an instance of the Rihndael class. // Create a encryptor. var encryptor = GetTransform(password, true); // Return encrypted bytes. return CipherStreamWrite(encryptor, bytes); } //#endregion //#region AES-256 Decryption function DecryptFromBase64(password, base64String) { /// <summary> /// Decrypt string with AES-256 by using password key. /// </summary> /// <param name="password">String password.</param> /// <param name="base64String">Encrypted Base64 string.</param> /// <returns>Decrypted string.</returns> // Convert Base64 string into a byte array. var encryptedBytes = System.Convert.FromBase64String(base64String); var bytes = Decrypt(password, encryptedBytes); // Convert decrypted data into a string. var s = System.Text.Encoding.Unicode.GetString(bytes, 0, bytes.length); // Return decrypted string. return s; } function Decrypt(password, bytes) { /// <summary> /// Decrypt string with AES-256 by using password key. /// </summary> /// <param name="password">String password.</param> /// <param name="encryptedBytes">Encrypted bytes.</param> /// <returns>Decrypted bytes.</returns> // Create an instance of the Rihndael class. // Create a encryptor. var decryptor = GetTransform(password, false); // Return encrypted bytes. return CipherStreamWrite(decryptor, bytes); } //#endregion function TestAesButton_Click(sender, e) { var password = $("PasswordTextBox").value; var data = $("DataTextBox").value; var encrypted = EncryptToBase64(password, data); WriteLog("Encrypded = " + encrypted); var decrypted = DecryptFromBase64(password, encrypted); WriteLog("Decrypded = " + decrypted); } </script> </head> <body> <table border="0" cellpadding="0" cellspacing="4"> <tbody> <tr> <td> Data: </td> <td> <input name="DataTextBox" value="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" id="DataTextBox" style="width: 500px;" type="text" /> </td> </tr> <tr> <td> Password: </td> <td> <input name="PasswordTextBox" value="password" id="PasswordTextBox" type="text"> </td> </tr> </tbody> </table> <input name="TestAesButton" value="Test AES" id="TestAesButton" type="button" /><br /> <iframe id="TraceLog" style="width: 100%; height: 200px;"></iframe> </body> </html>