Copy codeThe Code is as follows: public static class EncryptAndDecrypt
{
// Encryption
Public static string Encrypt (string input)
{
// Salt value
String saltValue = "saltValue ";
// Password value
String pwdValue = "pwdValue ";
Byte [] data = System. Text. UTF8Encoding. UTF8.GetBytes (input );
Byte [] salt = System. Text. UTF8Encoding. UTF8.GetBytes (saltValue );
// AesManaged-Management of Advanced Encryption Standard (AES) symmetric algorithms
System. Security. Cryptography. AesManaged aes = new System. Security. Cryptography. AesManaged ();
// Rfc2898DeriveBytes-use the HMACSHA1-based pseudo-random number generator to implement the password-based key derivation function (PBKDF2-a password-based key derivation function)
// Derived from the password and salt
System. Security. Cryptography. Rfc2898DeriveBytes rfc = new System. Security. Cryptography. Rfc2898DeriveBytes (pwdValue, salt );
/**/
/*
* AesManaged. BlockSize-block size of the encryption operation (unit: bit)
* AesManaged. LegalBlockSizes-block size supported by symmetric algorithms (unit: bit)
* AesManaged. KeySize-key size of the symmetric algorithm (unit: bit)
* AesManaged. LegalKeySizes-key size supported by symmetric algorithms (unit: bit)
* AesManaged. Key-Key of the symmetric algorithm
* AesManaged. IV-key size of symmetric algorithms
* Rfc2898DeriveBytes. GetBytes (number of pseudo-random key bytes to be generated by int)-generate 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 );
// Use the current Key attribute and the initialization vector IV to create a symmetric encryptor object
System. Security. Cryptography. ICryptoTransform encryptTransform = aes. CreateEncryptor ();
// Encrypted output stream
System. IO. MemoryStream encryptStream = new System. IO. MemoryStream ();
// Connect the encrypted Target stream (encryptStream) with the encrypted conversion (encryptTransform)
System. Security. Cryptography. CryptoStream encryptor = new System. Security. Cryptography. CryptoStream
(EncryptStream, encryptTransform, System. Security. Cryptography. CryptoStreamMode. Write );
// Write a byte sequence to the current CryptoStream (the encryption process is completed)
Encryptor. Write (data, 0, data. Length );
Encryptor. Close ();
// Convert the encrypted streams into byte arrays and convert them into strings using Base64 encoding.
String encryptedString = Convert. ToBase64String (encryptStream. ToArray ());
Return encryptedString;
}
# Region silverlight password decryption
/**/
/// <Summary>
/// Decrypt 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. 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 );
// Use the current Key attribute and the initialization vector IV to create a symmetric encryptor object
System. Security. Cryptography. ICryptoTransform decryptTransform = aes. CreateDecryptor ();
// Decrypted output stream
MemoryStream decryptStream = new MemoryStream ();
// Connect the decrypted destination stream (decryptStream) with the decrypted and converted form
System. Security. Cryptography. CryptoStream decryptor = new System. Security. Cryptography. CryptoStream (
DecryptStream, decryptTransform, System. Security. Cryptography. CryptoStreamMode. Write );
// Write a byte sequence to the current CryptoStream (the decryption process is completed)
Decryptor. Write (encryptBytes, 0, encryptBytes. Length );
Decryptor. Close ();
// Convert the flow obtained after decryption into a string
Byte [] decryptBytes = decryptStream. ToArray ();
String decryptedString = UTF8Encoding. UTF8.GetString (decryptBytes, 0, decryptBytes. Length );
Return decryptedString;
}
# Endregion
}