public static class EncryptAndDecrypt
{
//Encryption
public static string Encrypt(string input)
{
// Salt value
string saltValue = "saltValue";
// Password value
string pwdValue = "pwdValue";
byte[] data = .UTF8Encoding.(input);
byte[] salt = .UTF8Encoding.(saltValue);
// AesManaged - Advanced Encryption Standard (AES) Management Class of Symmetric Algorithm
aes = new ();
// Rfc2898DeriveBytes - Password-based key derivation function by using a pseudo-random number generator based on HMACSHA1 (PBKDF2 - a password-based key derivation function)
// Derived keys through password and salt
.Rfc2898DeriveBytes rfc = new .Rfc2898DeriveBytes(pwdValue, salt);
/**/
/*
* - Block size of encryption operation (unit: bit)
* - Block size supported by symmetric algorithms (unit: bit)
* - Key size of symmetric algorithm (unit: bit)
* - Key size supported by symmetric algorithm (unit: bit)
* - Key of symmetric algorithm
* - Key size of symmetric algorithm
* (int The number of pseudo-random key bytes that need to be generated) - Generate key
*/
= [0].MaxSize;
= [0].MaxSize;
= ( / 8);
= ( / 8);
// Create a symmetric encryptor object with the current Key attribute and initialization vector IV
encryptTransform = ();
// Encrypted output stream
encryptStream = new ();
// Connect the encrypted target stream (encryptStream) with the encrypted conversion (encryptTransform)
encryptor = new
(encryptStream, encryptTransform, );
// Write a sequence of bytes to the current CryptoStream (complete the encryption process)
(data, 0, );
();
// Convert the encrypted stream into a byte array, and then convert it into a string with Base64 encoding.
string encryptedString = Convert.ToBase64String(());
return encryptedString;
}
#region silverlight password decryption
/**/
/// <summary>
/// Decrypt the data
/// </summary>
/// <param name="input">Encrypted string</param>
/// <returns>String before encryption</returns>
public static string Decrypt(string input)
{
// Salt value (consistent with the value set during encryption)
string saltValue = "saltValue";
// Password value (consistent with the value set during encryption)
string pwdValue = "pwdValue";
byte[] encryptBytes = Convert.FromBase64String(input);
byte[] salt = Encoding.(saltValue);
aes = new ();
.Rfc2898DeriveBytes rfc = new .Rfc2898DeriveBytes(pwdValue, salt);
= [0].MaxSize;
= [0].MaxSize;
= ( / 8);
= ( / 8);
// Create a symmetric decryptor object with the current Key attribute and initialization vector IV
decryptTransform = ();
// Decrypted output stream
MemoryStream decryptStream = new MemoryStream();
// Connect the decrypted target stream (decryptStream) with the decrypted conversion (decryptTransform)
decryptor = new (
decryptStream, decryptTransform, );
// Write a sequence of bytes to the current CryptoStream (complete the decryption process)
(encryptBytes, 0, );
();
// Convert the decrypted stream into a string
byte[] decryptBytes = ();
string decryptedString = UTF8Encoding.(decryptBytes, 0, );
return decryptedString;
}
#endregion
}