Symmetric eds encryption and decryption method

Source: Internet
Author: User
Tags pkcs7

1. Import two namespaces:

Using system. Security. cryptography;

Using system. Text. regularexpressions;

 

2. encryption and decryption class:

Private Static readonly string skey = "qjzgeh6heszdvjecnfpguxzaib7nlqm3"; // key

Private Static readonly string SIV = "qcdy6x + aplw ="; // vector, which can be empty

Private Static algorithm ricalgorithm MCSP = new tripledescryptoserviceprovider (); // construct a symmetric algorithm

 

3. encryption method:

Public static string encryptdes (string source)

{

Icryptotransform CT;

Memorystream MS;

Cryptostream Cs;

Byte [] BYT;

 

String STR = NULL;

MCSP. Key = convert. frombase64string (skey );

MCSP. IV = convert. frombase64string (SIV );

MCSP. mode = system. Security. cryptography. ciphermode. ECB;

MCSP. Padding = system. Security. cryptography. paddingmode. pkcs7;

 

Ct = MCSP. createencryptor (MCSP. Key, MCSP. IV );

 

Byt = encoding. utf8.getbytes (source );

 

MS = new memorystream ();

Cs = new cryptostream (MS, CT, cryptostreammode. Write );

CS. Write (BYT, 0, byt. Length );

CS. flushfinalblock ();

 

CS. Close ();

 

STR = convert. tobase64string (Ms. toarray ());

 

Return STR;

}

 

4. Confidential methods:

Public static string decryptdes (string source)

{

Icryptotransform CT;

Memorystream MS;

Cryptostream Cs;

Byte [] BYT;

 

String STR = NULL;

 

MCSP. Key = convert. frombase64string (skey );

MCSP. IV = convert. frombase64string (SIV );

MCSP. mode = system. Security. cryptography. ciphermode. ECB;

MCSP. Padding = system. Security. cryptography. paddingmode. pkcs7;

 

Ct = MCSP. createdecryptor (MCSP. Key, MCSP. IV );

 

Byt = convert. frombase64string (source );

 

MS = new memorystream ();

Cs = new cryptostream (MS, CT, cryptostreammode. Write );

CS. Write (BYT, 0, byt. Length );

CS. flushfinalblock ();

 

CS. Close ();

 

STR = encoding. utf8.getstring (Ms. toarray ());

 

Return STR;

}

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.