C # Simple Encryption class Instance _c# tutorial

Source: Internet
Author: User
Tags rfc static class
Copy Code code 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-Advanced Encryption Standard (AES) symmetric algorithm management class
System.Security.Cryptography.AesManaged AES = new System.Security.Cryptography.AesManaged ();
Rfc2898derivebytes-Cryptography based Key derivation (PBKDF2-a password-based key derivation function) by using a HMACSHA1 pseudo-random number generator
Derive keys by password and salt
System.Security.Cryptography.Rfc2898DeriveBytes RFC = new System.Security.Cryptography.Rfc2898DeriveBytes (pwdvalue , salt);
/**/
/*
* Aesmanaged.blocksize-Block size for cryptographic operations (units: bit)
* aesmanaged.legalblocksizes-Symmetric algorithm supported block size (unit: bit)
* Aesmanaged.keysize-Symmetric algorithm key size (unit: bit)
* aesmanaged.legalkeysizes-the key size supported by the symmetric algorithm (unit: bit)
* Aesmanaged.key-The key of the symmetric algorithm
* AESMANAGED.IV-the key size of the symmetric algorithm
* Rfc2898derivebytes.getbytes (the 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);
Creates a symmetric cryptographic object with the current Key property and initialization vector IV
System.Security.Cryptography.ICryptoTransform encrypttransform = AES. CreateEncryptor ();
The output stream after encryption
System.IO.MemoryStream EncryptStream = new System.IO.MemoryStream ();
Connect the encrypted target stream (EncryptStream) to the Cryptographic transformation (encrypttransform)
System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
(EncryptStream, Encrypttransform, System.Security.Cryptography.CryptoStreamMode.Write);
Writes a sequence of bytes to the current CryptoStream (the process of completing the encryption)
Encryptor. Write (data, 0, data. Length);
Encryptor. Close ();
Converts the resulting stream to an array of bytes, which is then converted to a string using BASE64 encoding
String encryptedstring = Convert.tobase64string (Encryptstream.toarray ());
return encryptedstring;
}


#region Silverlight Password Decryption
/**/
<summary>
Decrypting data
</summary>
<param name= "Input" > Encrypted string </param>
<returns> the string before encryption </returns>
public static string Decrypt (String input)
{
Salt value (consistent with the value set when encrypting)
String saltvalue = "Saltvalue";
Password value (consistent with the value set when encrypting)
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);
Creates a symmetric decryption object with the current Key property and initialization vector IV
System.Security.Cryptography.ICryptoTransform decrypttransform = AES. CreateDecryptor ();
Post-decryption output stream
MemoryStream decryptstream = new MemoryStream ();
Connect the decrypted target stream (Decryptstream) to the decryption Transformation (decrypttransform)
System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream (
Decryptstream, Decrypttransform, System.Security.Cryptography.CryptoStreamMode.Write);
Writes a sequence of bytes to the current CryptoStream (completing the decryption process)
Decryptor. Write (encryptbytes, 0, encryptbytes.length);
Decryptor. Close ();
Converts the resulting stream to a string
byte[] decryptbytes = Decryptstream.toarray ();
String decryptedstring = UTF8Encoding.UTF8.GetString (decryptbytes, 0, decryptbytes.length);
return decryptedstring;
}
#endregion
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.