C # encrypt and decrypt strings

Source: Internet
Author: User

/// <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

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.