public static bool DecryptBase64(string cliperText, string key, out string originText) { byte[] inputData = Convert.FromBase64String(cliperText); var aes = new AesManaged() { Padding = _padding, Mode = _cipherMode, IV = ORIGIN_IV, Key = Encoding.UTF8.GetBytes(key.PadRight(KEY_LENGTH, PADDING_CHAR).Substring(0, KEY_LENGTH)) }; var decryptor = aes.CreateDecryptor(); bool decryptSuccess = true; try { byte[] decryptedData = decryptor.TransformFinalBlock(inputData, 0, inputData.Length); originText = Encoding.UTF8.GetString(decryptedData); } catch { decryptSuccess = false; originText = string.Empty; } finally { decryptor.Dispose(); if (aes != null) { aes.Clear(); } } return decryptSuccess; }
/// <summary> /// Creates a symmetric key. See this link for more information behind the numbers /// http://blogs.msdn.com/b/shawnfa/archive/2006/10/09/the-differences-between-rijndael-and-aes.aspx /// </summary> /// <returns></returns> private SymmetricKey CreateNewAESSymmetricKeyset() { if (cert == null) { throw new InvalidOperationException("Unable to create new AES keyset; Certificate not loaded."); } byte[] symmKey, iv; using (AesManaged aes = new AesManaged()) { aes.GenerateIV(); aes.GenerateKey(); symmKey = aes.Key; iv = aes.IV; aes.Clear(); } // Encrypt the Symmetric Key for storage byte[] encryptedKey = EncryptRSA(symmKey, cert); SymmetricKey symmKeySet = new SymmetricKey() { iv = iv, Key = encryptedKey, CertificateThumbprint = cert.Thumbprint }; return symmKeySet; }
///<summary>Encrypts signature text and returns a base 64 string so that it can go directly into the database.</summary> public static string Encrypt(string str,byte[] key){ //No need to check RemotingRole; no call to db. if(str==""){ return ""; } byte[] ecryptBytes=Encoding.UTF8.GetBytes(str); MemoryStream ms=new MemoryStream(); CryptoStream cs=null; Aes aes=new AesManaged(); aes.Key=key; aes.IV=new byte[16]; ICryptoTransform encryptor=aes.CreateEncryptor(aes.Key,aes.IV); cs=new CryptoStream(ms,encryptor,CryptoStreamMode.Write); cs.Write(ecryptBytes,0,ecryptBytes.Length); cs.FlushFinalBlock(); byte[] encryptedBytes=new byte[ms.Length]; ms.Position=0; ms.Read(encryptedBytes,0,(int)ms.Length); cs.Dispose(); ms.Dispose(); if(aes!=null) { aes.Clear(); } return Convert.ToBase64String(encryptedBytes); }
public String EncryptIt(String s) { Encoding byteEncoder = Encoding.UTF8; byte[] rijnKey = byteEncoder.GetBytes("h504lXb1erd4ilw7"); //this is key for encrypt data byte[] rijnIV = byteEncoder.GetBytes("4hx7e4bwM15d0CrL"); //this is iv for encrypt data String result; AesManaged rijn = new AesManaged(); //Using AES-128 rijn.KeySize = 128; rijn.BlockSize = 128; using (MemoryStream msEncrypt = new MemoryStream()) { using (ICryptoTransform encryptor = rijn.CreateEncryptor(rijnKey, rijnIV)) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(s); } } } result = Convert.ToBase64String(msEncrypt.ToArray()); } rijn.Clear(); return result; }
public static string Decrypt(string str,byte[] key) { //No need to check RemotingRole; no call to db. if(str==""){ return ""; } try { byte[] encrypted=Convert.FromBase64String(str); MemoryStream ms=null; CryptoStream cs=null; StreamReader sr=null; Aes aes=new AesManaged(); aes.Key=key; aes.IV=new byte[16]; ICryptoTransform decryptor=aes.CreateDecryptor(aes.Key,aes.IV); ms=new MemoryStream(encrypted); cs=new CryptoStream(ms,decryptor,CryptoStreamMode.Read); sr=new StreamReader(cs); string decrypted=sr.ReadToEnd(); ms.Dispose(); cs.Dispose(); sr.Dispose(); if(aes!=null) { aes.Clear(); } return decrypted; } catch { //MessageBox.Show("Text entered was not valid encrypted text."); return""; } }
public string Decryption() { try { byte[] encrypted=Convert.FromBase64String(textInput.Text); MemoryStream ms=null; CryptoStream cs=null; StreamReader sr=null; Aes aes=new AesManaged(); aes.Key=key; aes.IV=new byte[16]; ICryptoTransform decryptor=aes.CreateDecryptor(aes.Key,aes.IV); ms=new MemoryStream(encrypted); cs=new CryptoStream(ms,decryptor,CryptoStreamMode.Read); sr=new StreamReader(cs); string decrypted=sr.ReadToEnd(); ms.Dispose(); cs.Dispose(); sr.Dispose(); if(aes!=null) { aes.Clear(); } return decrypted; } catch { MessageBox.Show("Text entered was not valid encrypted text."); return""; } }
public static string Decrypt(byte[] encryptedData, string password, byte[] salt = null) { if (encryptedData == null) throw new ArgumentNullException("encryptedData"); if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password"); if (salt == null) salt = DefaultSalt; var aes = new AesManaged(); string text; try { aes.SetKey(password, salt); using (var ms = new MemoryStream(encryptedData)) { aes.IV = ms.GetIV(); var decryptor = aes.CreateDecryptor(aes.Key, aes.IV); using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) using (var sr = new StreamReader(cs)) text = sr.ReadToEnd(); } } finally { aes.Clear(); } return text; }
public int dbEncrypt(String partition, int size, String data, out String dataOut) { AesManaged aes = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { aes = new AesManaged(); aes.Key = readKeyFromFile(partition); memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write); byte[] buf = Encoding.UTF8.GetBytes(data); cryptoStream.Write(buf, 0, buf.Length); cryptoStream.FlushFinalBlock(); dataOut = Convert.ToBase64String(memoryStream.ToArray()); } finally { if (cryptoStream != null) cryptoStream.Close(); if (memoryStream != null) memoryStream.Close(); if (aes != null) aes.Clear(); } return getErrorCode() == 0 ? 1 : 0; }
public static byte[] Encrypt(string text, string password, byte[] salt = null) { if (string.IsNullOrEmpty(text)) throw new ArgumentNullException("text"); if (string.IsNullOrEmpty(password)) throw new ArgumentNullException("password"); if (salt == null) salt = DefaultSalt; var aes = new AesManaged(); byte[] encryptedData; try { aes.SetKey(password, salt); var encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (var ms = new MemoryStream()) { ms.Write(BitConverter.GetBytes(aes.IV.Length), 0, sizeof(int)); ms.Write(aes.IV, 0, aes.IV.Length); using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) using (var sw = new StreamWriter(cs)) sw.Write(text); encryptedData = ms.ToArray(); } } finally { aes.Clear(); } return encryptedData; }
public byte[] Decrypt(byte[] cipher, byte[] key, byte[] IV) { System.Collections.Generic.List<byte> bytes = new List<byte>(); using (AesManaged _aesAlgorithm = new AesManaged()) { _aesAlgorithm.Key = key; _aesAlgorithm.IV = IV; using (ICryptoTransform decryptor = _aesAlgorithm.CreateDecryptor(_aesAlgorithm.Key, _aesAlgorithm.IV)) { using (System.IO.MemoryStream str = new System.IO.MemoryStream(cipher)) { using (CryptoStream crypto = new CryptoStream(str, decryptor, CryptoStreamMode.Read)) { int b = crypto.ReadByte(); while (b > -1) { b = crypto.ReadByte(); bytes.Add((byte)b); } } } _aesAlgorithm.Clear(); } } return bytes.ToArray(); }
public static byte[] DecryptCBC(byte[] cipherTextBodyBytes, byte[] secretBytes, byte[] saltBytes, int keySize) { byte[] key = null; byte[] iv = null; if (keySize == 128) { key = GetIteratedMD5(secretBytes, saltBytes, 1); iv = GetIteratedMD5(secretBytes, saltBytes, 2); } else if (keySize == 256) { byte[] keyA = GetIteratedMD5(secretBytes, saltBytes, 1); byte[] keyB = GetIteratedMD5(secretBytes, saltBytes, 2); key = new byte[keyA.Length + keyB.Length]; Buffer.BlockCopy(keyA, 0, key, 0, keyA.Length); // merge keyA and keyB into key Buffer.BlockCopy(keyB, 0, key, keyA.Length, keyB.Length); iv = GetIteratedMD5(secretBytes, saltBytes, 3); } else throw new Exception("Key size must be 128 or 256"); AesManaged aes = new AesManaged(); aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.KeySize = keySize; aes.BlockSize = 128; aes.Key = key; aes.IV = iv; byte[] clearTextBody = null; using (ICryptoTransform Decryptor = aes.CreateDecryptor()) { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream CryptoStream = new CryptoStream(mStream, Decryptor, CryptoStreamMode.Write)) { CryptoStream.Write(cipherTextBodyBytes, 0, cipherTextBodyBytes.Length); CryptoStream.FlushFinalBlock(); clearTextBody = mStream.ToArray(); CryptoStream.Close(); } mStream.Close(); } } aes.Clear(); return clearTextBody; }
/// <summary> /// Decrypt the given string. Assumes the string was encrypted using /// EncryptStringAES(), using an identical sharedSecret. /// </summary> /// <param name="cipherText">The text to decrypt.</param> /// <param name="sharedSecret">A password used to generate a key for decryption.</param> public static string DecryptStringAES(string cipherText, string sharedSecret) { if (string.IsNullOrEmpty(cipherText)) throw new ArgumentNullException("cipherText"); if (string.IsNullOrEmpty(sharedSecret)) throw new ArgumentNullException("sharedSecret"); // Declare the RijndaelManaged object // used to decrypt the data. AesManaged aesAlg = null; // Declare the string used to hold // the decrypted text. string plaintext = null; try { // generate the key from the shared secret and the salt Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt); // Create a RijndaelManaged object // with the specified key and IV. aesAlg = new AesManaged(); aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8); // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. byte[] bytes = Convert.FromBase64String(cipherText); using (MemoryStream msDecrypt = new MemoryStream(bytes)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } finally { // Clear the RijndaelManaged object. if (aesAlg != null) aesAlg.Clear(); } return plaintext; }
public static string Decrypt(string dataToDecrypt, string salt) { AesManaged aes = null; MemoryStream memoryStream = null; string password = "******"; try { //Generate a Key based on a Password and HMACSHA1 pseudo-random number generator //Salt must be at least 8 bytes long //Use an iteration count of at least 1000 Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000); //Create AES algorithm aes = new AesManaged(); //Key derived from byte array with 32 pseudo-random key bytes aes.Key = rfc2898.GetBytes(32); //IV derived from byte array with 16 pseudo-random key bytes aes.IV = rfc2898.GetBytes(16); //Create Memory and Crypto Streams memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write); //Decrypt Data byte[] data = Convert.FromBase64String(dataToDecrypt); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); //Return Decrypted String byte[] decryptBytes = memoryStream.ToArray(); //Dispose if (cryptoStream != null) cryptoStream.Dispose(); //Retval return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length); } finally { if (memoryStream != null) memoryStream.Dispose(); if (aes != null) aes.Clear(); } }
public string Decode(string data) { AesManaged aes = new AesManaged(); byte[] buf = CharUtil.DecodeString(data, mask); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(keys, salt), CryptoStreamMode.Write)) { cStream.Write(buf, 0, buf.Length); cStream.FlushFinalBlock(); buf = mStream.ToArray(); } } aes.Clear(); return Encoding.UTF8.GetString(buf, 0, buf.Length); }
private const string _salt = "salt"; // Not for UI. Do not change. #endregion Fields #region Methods public static string Decrypt(string dataToDecrypt, string password = "", string salt = "") { AesManaged aes = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { if (password.CompareTo("") == 0) password = _password; if (salt.CompareTo("") == 0) salt = _salt; //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt)); //Create AES algorithm with 256 bit key and 128-bit block size aes = new AesManaged(); aes.Key = rfc2898.GetBytes(aes.KeySize / 8); aes.IV = rfc2898.GetBytes(aes.BlockSize / 8); //Create Memory and Crypto Streams memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write); //Decrypt Data byte[] data = Convert.FromBase64String(dataToDecrypt); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); //Return Decrypted String byte[] decryptBytes = memoryStream.ToArray(); return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length); } finally { if (cryptoStream != null) cryptoStream.Dispose(); if (memoryStream != null) memoryStream.Dispose(); if (aes != null) aes.Clear(); } }
public static bool Decrypt(string cliperText, string key, out string originText) { int cliperTextLength = cliperText.Length; if (cliperTextLength % 2 != 0) { originText = String.Empty; return false; } byte[] inputData = new byte[cliperTextLength / 2]; for (int i = 0; i < inputData.Length; i++) { inputData[i] = Convert.ToByte(cliperText.Substring(i * 2, 2), 16); } var aes = new AesManaged() { Padding = _padding, Mode = _cipherMode, IV = ORIGIN_IV, Key = Encoding.UTF8.GetBytes(key.PadRight(KEY_LENGTH, PADDING_CHAR).Substring(0, KEY_LENGTH)) }; var decryptor = aes.CreateDecryptor(); bool decryptSuccess = true; try { byte[] decryptedData = decryptor.TransformFinalBlock(inputData, 0, inputData.Length); originText = Encoding.UTF8.GetString(decryptedData); } catch { originText = String.Empty; } finally { decryptor.Dispose(); if (aes != null) { aes.Clear(); } } return decryptSuccess; }
public static string DecryptAES(string decryptString, string decryptKey, string salt) { AesManaged aes = null; MemoryStream ms = null; CryptoStream cs = null; string str = null; try { Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(decryptKey, Encoding.UTF8.GetBytes(salt)); aes = new AesManaged(); aes.Key = rfc2898.GetBytes(aes.KeySize / 8); aes.IV = rfc2898.GetBytes(aes.BlockSize / 8); ms = new MemoryStream(); cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write); byte[] data = Convert.FromBase64String(decryptString); cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); str = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length); } catch { str = decryptString; } finally { if (cs != null) cs.Close(); if (ms != null) ms.Close(); if (aes != null) aes.Clear(); } return str; }
// Internal helper function to do the encryption/decryption protected static byte[] Crypt(byte[] bValue, string sPassword, string sSalt, bool bEncrypt) { byte[] bReturnVal = null; AesManaged aesTransform = null; MemoryStream msStream = null; CryptoStream csCryptoStream = null; try { // Rfc2898DeriveBytes (PBKDF2) is similar to using PasswordDeriveBytes (PBKDF1) but the algorithm is stronger apparently Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(sPassword, Encoding.Unicode.GetBytes(sSalt), 1000); // Create our AES Key and Initialization Vector (IV) values aesTransform = new AesManaged(); aesTransform.Key = pwdGen.GetBytes(aesTransform.KeySize / 8); aesTransform.IV = pwdGen.GetBytes(aesTransform.BlockSize / 8); aesTransform.Padding = PaddingMode.ISO10126;// Pad with random characters rather than zeros // Depending on if we're encrypting or decrypting create the proper transform object ICryptoTransform ctTransform = (bEncrypt ? aesTransform.CreateEncryptor() : aesTransform.CreateDecryptor()); // Create our memory stream and encryption/decryption stream object msStream = new MemoryStream(); csCryptoStream = new CryptoStream(msStream, ctTransform, CryptoStreamMode.Write); // Encrypt/Decrypt the value csCryptoStream.Write(bValue, 0, bValue.Length); csCryptoStream.FlushFinalBlock(); // Turn our encrypted/decrypted memory stream value into a byte array bReturnVal = msStream.ToArray(); } finally { if (csCryptoStream != null) { csCryptoStream.Clear(); csCryptoStream.Close(); } if (msStream != null) { msStream.Close(); } if (aesTransform != null) { aesTransform.Clear(); } } // Return the encrypted/decrypted value return bReturnVal; }
public static string Encrypt(string dataToEncrypt, string password=PASSWORD, string salt=SALT) { AesManaged aes = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { //Generate a Key based on a Password and HMACSHA1 pseudo-random number generator //Salt must be at least 8 bytes long //Use an iteration count of at least 1000 Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000); //Create AES algorithm aes = new AesManaged(); //Key derived from byte array with 32 pseudo-random key bytes aes.Key = rfc2898.GetBytes(32); //IV derived from byte array with 16 pseudo-random key bytes aes.IV = rfc2898.GetBytes(16); //Create Memory and Crypto Streams memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write); //Encrypt Data byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); //Return Base 64 String return Convert.ToBase64String(memoryStream.ToArray()).Replace("=","$"); } finally { if (cryptoStream != null) cryptoStream.Close(); if (memoryStream != null) memoryStream.Close(); if (aes != null) aes.Clear(); } }
//---------------------------------------------------------------------- #region --- Methods --- //---------------------------------------------------------------------- /// <summary> /// Encrypts the specified data using the password and salt. /// </summary> /// <param name="dataToEncrypt">The data to encrypt.</param> /// <param name="password">The password.</param> /// <param name="salt">The salt.</param> /// <returns></returns> public static string Encrypt(string dataToEncrypt, string password, string salt) { AesManaged aes = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { var rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000); aes = new AesManaged(); aes.Key = rfc2898.GetBytes(32); aes.IV = rfc2898.GetBytes(16); memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write); var data = Encoding.UTF8.GetBytes(dataToEncrypt); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); return Convert.ToBase64String(memoryStream.ToArray()); } catch (Exception ex) { return String.Empty; } finally { if (cryptoStream != null) { cryptoStream.Close(); } if (memoryStream != null) { memoryStream.Close(); } if (aes != null) { aes.Clear(); } } }
///<summary>Encrypts signature text and returns a base 64 string so that it can go directly into the database.</summary> public string Encryption(string encrypt){ byte[] ecryptBytes=Encoding.UTF8.GetBytes(encrypt); MemoryStream ms=new MemoryStream(); CryptoStream cs=null; Aes aes=new AesManaged(); aes.Key=key; aes.IV=new byte[16]; ICryptoTransform encryptor=aes.CreateEncryptor(aes.Key,aes.IV); cs=new CryptoStream(ms,encryptor,CryptoStreamMode.Write); cs.Write(ecryptBytes,0,ecryptBytes.Length); cs.FlushFinalBlock(); byte[] encryptedBytes=new byte[ms.Length]; ms.Position=0; ms.Read(encryptedBytes,0,(int)ms.Length); cs.Dispose(); ms.Dispose(); if(aes!=null) { aes.Clear(); } return Convert.ToBase64String(encryptedBytes); }
public static string Encrypt(string dataToEncrypt, string key) { AesManaged aes = null; MemoryStream memoryStream = null; CryptoStream cryptoStream = null; try { //Create AES algorithm // aes = new AesManaged(); aes = new AesManaged(); //Key derived from byte array with 32 pseudo-random key bytes aes.Key = Encoding.UTF8.GetBytes(key); //IV derived from byte array with 16 pseudo-random key bytes aes.IV = new byte[16]; //Create Memory and Crypto Streams memoryStream = new MemoryStream(); cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write); //Encrypt Data byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt); cryptoStream.Write(data, 0, data.Length); cryptoStream.FlushFinalBlock(); //Return Base 64 String return Convert.ToBase64String(memoryStream.ToArray()); } finally { if (cryptoStream != null) cryptoStream.Close(); if (memoryStream != null) memoryStream.Close(); if (aes != null) aes.Clear(); } }
public static byte[] Encrypt(byte[] plaintext, AesEncryptionInfo info) { if (info == null) throw new ArgumentNullException("info"); if (plaintext == null || plaintext.Length <= 0) throw new ArgumentNullException("plaintext"); if (info.Key == null || info.Key.Length <= 0) throw new ArgumentNullException("key"); if (info.IV == null || info.IV.Length <= 0) throw new ArgumentNullException("iv"); MemoryStream memoryStream; AesManaged aesAlg = null; try { // Create the encryption algorithm object with the specified key and IV. aesAlg = new AesManaged(); aesAlg.Key = info.Key; aesAlg.IV = info.IV; // Create an encryptor to perform the stream transform. var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. memoryStream = new MemoryStream(); using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plaintext, 0, plaintext.Length); } } finally { if (aesAlg != null) aesAlg.Clear(); } // Return the encrypted bytes from the memory stream. return memoryStream.ToArray(); }
public static System.Version LatestVersion=new Version("15.2.1.0");//This value must be changed when a new conversion is to be triggered. #region Helper Functions ///<summary>Encrypts signature text and returns a base 64 string so that it can go directly into the database. ///Copied from MiscUtils.Encrypt() so that the data conversion will never change historically.</summary> public static string Encrypt(string encrypt) { UTF8Encoding enc=new UTF8Encoding(); byte[] arrayEncryptBytes=Encoding.UTF8.GetBytes(encrypt); MemoryStream ms=new MemoryStream(); CryptoStream cs=null; Aes aes=new AesManaged(); aes.Key=enc.GetBytes("AKQjlLUjlcABVbqp"); aes.IV=new byte[16]; ICryptoTransform encryptor=aes.CreateEncryptor(aes.Key,aes.IV); cs=new CryptoStream(ms,encryptor,CryptoStreamMode.Write); cs.Write(arrayEncryptBytes,0,arrayEncryptBytes.Length); cs.FlushFinalBlock(); byte[] retval=new byte[ms.Length]; ms.Position=0; ms.Read(retval,0,(int)ms.Length); cs.Dispose(); ms.Dispose(); if(aes!=null) { aes.Clear(); } return Convert.ToBase64String(retval); }
/// <summary> /// Encrypt string with AES. /// </summary> /// <param name="dataToEncrypt">Unsafe data</param> /// <param name="password">Password used for encription</param> /// <param name="stream">Destination stream like memory or file</param> public static void Encrypt(byte[] dataToEncrypt, byte[] password, Stream stream) { AesManaged aes = null; CryptoStream cryptoStream = null; try { // Generate a Key based on a Password and HMACSHA1 pseudo-random number generator // Salt must be at least 8 bytes long // Use an iteration count of at least 1000 //Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10); // Create AES algorithm aes = new AesManaged(); // Key derived from byte array with 32 pseudo-random key bytes aes.Key = password; // IV derived from byte array with 16 pseudo-random key bytes aes.IV = new byte[16]; // Create Crypto Streams cryptoStream = new CryptoStream(stream, aes.CreateEncryptor(), CryptoStreamMode.Write); // Encrypt Data cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length); cryptoStream.FlushFinalBlock(); } finally { if (cryptoStream != null) cryptoStream.Close(); if (aes != null) aes.Clear(); } }
public static byte[] Decrypt(byte[] ciphertext, AesEncryptionInfo info) { if (info == null) throw new ArgumentNullException("info"); if (ciphertext == null || ciphertext.Length <= 0) throw new ArgumentNullException("plainText"); if (info.Key == null || info.Key.Length <= 0) throw new ArgumentNullException("key"); if (info.IV == null || info.IV.Length <= 0) throw new ArgumentNullException("iv"); AesManaged aesAlg = null; try { // Create a the encryption algorithm object with the specified key and IV. aesAlg = new AesManaged(); aesAlg.Key = info.Key; aesAlg.IV = info.IV; // Create a decrytor to perform the stream transform. var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for decryption. using (var memoryStream = new MemoryStream(ciphertext)) using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) { int len = cryptoStream.Read(ciphertext, 0, ciphertext.Length); return ciphertext.RangeSubset(0, len); } } finally { if (aesAlg != null) aesAlg.Clear(); } }
public static string EncryptBase64(string plainText, string key) { byte[] inputData = Encoding.UTF8.GetBytes(plainText); var aes = new AesManaged() { Padding = _padding, Mode = _cipherMode, IV = ORIGIN_IV, Key = Encoding.UTF8.GetBytes(key.PadRight(KEY_LENGTH, PADDING_CHAR).Substring(0, KEY_LENGTH)) }; var cryptor = aes.CreateEncryptor(); var encryptedData = cryptor.TransformFinalBlock(inputData, 0, inputData.Length); cryptor.Dispose(); aes.Clear(); return Convert.ToBase64String(encryptedData); }
public static string Encrypt(string plainText, string key) { byte[] inputData = Encoding.UTF8.GetBytes(plainText); var aes = new AesManaged() { Padding = _padding, Mode = _cipherMode, IV = ORIGIN_IV, Key = Encoding.UTF8.GetBytes(key.PadRight(KEY_LENGTH, PADDING_CHAR).Substring(0, KEY_LENGTH)) }; var cryptor = aes.CreateEncryptor(); var encryptedData = cryptor.TransformFinalBlock(inputData, 0, inputData.Length); cryptor.Dispose(); aes.Clear(); StringBuilder sb = new StringBuilder(); foreach (byte b in encryptedData) { sb.AppendFormat("{0:x2}", b); } return sb.ToString(); }
private bool Decrypt(string name, string code, string pass, string main) { #region 口令散列 // 口令 byte[] k = GenK(name, code, pass); // 向量 byte[] v = GenV(name, code, pass); // 数据 byte[] t = Convert.FromBase64String(main); pass = null; #endregion #region AES 解密 AesManaged aes = new AesManaged(); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(k, v), CryptoStreamMode.Write)) { cStream.Write(t, 0, t.Length); cStream.FlushFinalBlock(); t = mStream.ToArray(); } } aes.Clear(); #endregion if (t.Length != 72) { return false; } this.Code = Encoding.UTF8.GetString(t, 0, 8); int i = 8; _Salt = new byte[16]; Array.Copy(t, i, _Salt, 0, _Salt.Length); i += _Salt.Length; _Keys = new byte[32]; Array.Copy(t, i, _Keys, 0, _Keys.Length); i += _Keys.Length; _Mask = Encoding.UTF8.GetChars(t, i, 16); return true; }
public string EncodeKey(string data) { AesManaged aes = new AesManaged(); byte[] buf = Encoding.UTF8.GetBytes(data); using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(_Keys, _Salt), CryptoStreamMode.Write)) { cStream.Write(buf, 0, buf.Length); cStream.FlushFinalBlock(); buf = mStream.ToArray(); } } aes.Clear(); return CharUtil.EncodeString(buf, _Mask); }