String encryption and decryption: An Example

Source: Internet
Author: User

 

. NET provides a variety of encryption and decryption functions, but only one is used here.

Public class stringdeprovider
{
# Region internal fields

Private ‑ricalgorithm MCSP;

/// <Summary>
/// Key | Note: The length must be 12
/// </Summary>
Private const string CIV = "moliqingcha = ";
/// <Summary>
/// Initialization vector | Note: The length must be 12
/// </Summary>
Private const string ckey = "xingmai0216 = ";

# Endregion

# Region instance Constructor

Public stringdeprovider ()
{
MCSP = new descryptoserviceprovider ();
}

# Endregion

# Region instance method

///


// encrypted string
///
///
//
Public String encryptstring (string value)
{< br> icryptotransform CT;
memorystream MS;
cryptostream Cs;
byte [] BYT;

Ct = MCSP. createencryptor (convert. frombase64string (ckey), convert. frombase64string (CIV ));

Byt = encoding. utf8.getbytes (value );

MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();

CS. Close ();

Return convert. tobase64string (Ms. toarray ());
}

///


// decrypt the string
///
///
//
Public String decryptstring (string value)
{< br> icryptotransform CT;
memorystream MS;
cryptostream Cs;
byte [] BYT;

Ct = MCSP. createdecryptor (convert. frombase64string (ckey), convert. frombase64string (CIV ));

Byt = convert. frombase64string (value );

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 ());
}

# Endregion
}

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.