public static class EncryptAndDecrypt
{
// encryption
public static string Encrypt(string input)
{
// Salt value
string saltValue = "saltValue";
// The password value
string pwdValue = "pwdValue";
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);
// AesManaged - Advanced encryption standard (AES) Symmetric algorithm management class
System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
// Rfc2898DeriveBytes - Based on HMACSHA1 The pseudorandom number generator, based on the implementation of the key derived function of the password (PBKDF2 - 1 A key derived function based on a password )
// through password and salt The derived keys
System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
/**/
/*
* AesManaged.BlockSize - Block size of encryption operation (unit: bit )
* AesManaged.LegalBlockSizes - Block size supported by symmetric algorithm (unit: bit )
* AesManaged.KeySize - Key size of symmetric algorithm (unit: bit )
* AesManaged.LegalKeySizes - Key size supported by symmetric algorithm (unit: bit )
* AesManaged.Key - Symmetric algorithm key
* AesManaged.IV - Symmetric algorithm key size
* Rfc2898DeriveBytes.GetBytes(int The number of pseudorandom key bytes that need to be generated ) - Generate the key
*/
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
aes.Key = rfc.GetBytes(aes.KeySize / 8);
aes.IV = rfc.GetBytes(aes.BlockSize / 8);
// With the current Key Properties and initialization vectors IV Create a symmetric encryptor object
System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();
// The encrypted output stream
System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();
// Encrypt the target stream ( encryptStream ) and encryption conversion ( encryptTransform Connected)
System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
(encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
// will 1 A sequence of bytes is written to the current CryptoStream (completing the encryption process)
encryptor.Write(data, 0, data.Length);
encryptor.Close();
// Convert the encrypted stream into a byte array and reuse it Base64 The encoding converts it to a string
string encryptedString = Convert.ToBase64String(encryptStream.ToArray());
return encryptedString;
}
#region silverlight Password decryption
/**/
/// <summary>
/// Decrypt the data
/// </summary>
/// <param name="input"> The encrypted string </param>
/// <returns> The pre-encrypted string </returns>
public static string Decrypt(string input)
{
// Salt value (as set when encrypting 1 To)
string saltValue = "saltValue";
// Password value (as set when encrypting 1 To)
string pwdValue = "pwdValue";
byte[] encryptBytes = Convert.FromBase64String(input);
byte[] salt = Encoding.UTF8.GetBytes(saltValue);
System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
aes.KeySize = aes.LegalKeySizes[0].MaxSize;
aes.Key = rfc.GetBytes(aes.KeySize / 8);
aes.IV = rfc.GetBytes(aes.BlockSize / 8);
// With the current Key Properties and initialization vectors IV Create a symmetric decryptor object
System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();
// Decrypted output stream
MemoryStream decryptStream = new MemoryStream();
// Will decrypt the target stream ( decryptStream ) and decryption conversion ( decryptTransform Connected)
System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
// will 1 A sequence of bytes is written to the current CryptoStream (complete the decryption process)
decryptor.Write(encryptBytes, 0, encryptBytes.Length);
decryptor.Close();
// Converts the decrypted stream into a string
byte[] decryptBytes = decryptStream.ToArray();
string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
return decryptedString;
}
#endregion
}