﻿function EncryptToBase64(password,s){var bytes=System.Text.Encoding.Unicode.GetBytes(s);var encryptedBytes=Encrypt(password,bytes);var base64String=System.Convert.ToBase64String(encryptedBytes);return base64String;};function Encrypt(password,bytes){var encryptor=GetTransform(password,true);return CipherStreamWrite(encryptor,bytes);};function DecryptFromBase64(password,base64String){var decryptedBytes=System.Convert.FromBase64String(base64String);var bytes=Decrypt(password,decryptedBytes);var s=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.length);return s;};function Decrypt(password,bytes){var decryptor=GetTransform(password,false);return CipherStreamWrite(decryptor,bytes);};function GetTransform(password,encrypt){var cipher=new System.Security.Cryptography.RijndaelManaged();var salt=SaltFromPassword(password);var secretKey=new System.Security.Cryptography.Rfc2898DeriveBytes(password,salt,10);var key=secretKey.GetBytes(32);var iv=secretKey.GetBytes(16);var cryptor=null;if(encrypt){cryptor=cipher.CreateEncryptor(key,iv);}else{cryptor=cipher.CreateDecryptor(key,iv);}return cryptor;};function CipherStreamWrite(cryptor,input){var inputBuffer=new System.Byte(input.length);System.Buffer.BlockCopy(input,0,inputBuffer,0,inputBuffer.length);var stream=new System.IO.MemoryStream();var mode=System.Security.Cryptography.CryptoStreamMode.Write;var cryptoStream=new System.Security.Cryptography.CryptoStream(stream,cryptor,mode);cryptoStream.Write(inputBuffer,0,inputBuffer.length);cryptoStream.FlushFinalBlock();var outputBuffer=stream.ToArray();stream.Close();cryptoStream.Close();return outputBuffer;};function SaltFromPassword(password){var passwordBytes=System.Text.Encoding.UTF8.GetBytes(password);var hmac=new System.Security.Cryptography.HMACSHA1(passwordBytes);var salt=hmac.ComputeHash(passwordBytes);return salt;}