/// <Summary>
/// String encryption component
/// </Summary>
Public class Encrypt
{
# Region "defining encryption string variables"
Private symmetricalgorithm MCSP; // declares symmetricAlgorithmVariable
Private const string CIV = "mi9l/+ 7zujhy12se6yjy111a"; // initialization Vector
Private const string ckey = "jkhuiy9d/9i ="; // key (constant)
# Endregion
/// <Summary>
/// Instantiate
/// </Summary>
Public encrypt ()
{
MCSP = new descryptoserviceprovider (); // The data encryption service that defines the Access Data Encryption Standard (DES) algorithm providesProgram(CSP) version of the packaging object, which is a derived class of symmetricalgorithm
}
/// <Summary>
/// Encrypted string
/// </Summary>
/// <Param name = "value"> string to be encrypted </param>
/// <Returns> </returns>
Public String encryptstring (string value)
{
Icryptotransform CT; // defines the basic encryption and conversion operations
Memorystream MS; // defines the memory stream
Cryptostream Cs; // defines the link of the memory stream to the encrypted conversion stream.
Byte [] BYT;
// Createencryptor creates (symmetric data) encryption object
Ct = MCSP. createencryptor (convert. frombase64string (ckey), convert. frombase64string (CIV); // create symmetric data encryption standards with the specified key and initialization Vector
Byt = encoding. utf8.getbytes (value); // converts the value character to the byte sequence encoded by the UTF-8
MS = new memorystream (); // creates a memory stream
Cs = new cryptostream (MS, CT, cryptostreammode. Write); // link the memory stream to the encrypted converted stream
CS. Write (BYT, 0, byt. Length); // write the memory stream
CS. flushfinalblock (); // write data in the buffer to the memory stream and clear the buffer.
CS. Close (); // release the memory stream
Return convert. tobase64string (Ms. toarray (); // write the memory flow to the byte array and convert it to a string character.
}
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "value"> string to be decrypted </param>
/// <Returns> string </returns>
Public String decryptstring (string value)
{
Icryptotransform CT; // defines the basic encryption and conversion operations
Memorystream MS; // defines the memory stream
Cryptostream Cs; // defines the stream that links the data stream to the encrypted conversion stream.
Byte [] BYT;
Ct = MCSP. createdecryptor (convert. frombase64string (ckey), convert. frombase64string (CIV); // create symmetric data decryption standards with the specified key and initialization Vector
Byt = convert. frombase64string (value); // convert the value (Base 64) character to a byte array
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
Return encoding. utf8.getstring (Ms. toarray (); // decodes All characters in the byte array into a string
}
}
/*
The following namespace must be referenced:
System. Security. cryptography;
System. IO;
*/
From http://www.cnblogs.com/nclly/archive/2008/11/27/1342087.html