[Reprint] MD5 encryption and decryption

Source: Internet
Author: User

MD5 encryption and decryption

Using system;
Using system. text;
Using system. IO;
Using system. Security. cryptography;

Class class1
{
Static void main ()
{
Console. writeline ("encrypt string ...");
Txtkey = "tkggrmbervc ="; // sets the encryption key.
Btnkeygen ();
Console. writeline ("Encrypt Key: {0}", txtkey );
Txtiv = "kl7zgtm1dvq ="; // sets encryption IV
Btnivgen ();
Console. writeline ("encrypt IV: {0}", txtiv );
Console. writeline ();

String txtencrypted = encryptstring ("test ");
Console. writeline ("encrypt string: {0}", txtencrypted );

String txtoriginal = decryptstring (txtencrypted );
Console. writeline ("decrypt string: {0}", txtoriginal );
}

Private Static metrics ricalgorithm MCSP;
Private Static string txtkey;
Private Static string txtiv;

Private Static void btnkeygen ()
{
MCSP = setenc ();

Byte [] byt2 = convert. frombase64string (txtkey );

MCSP. Key = byt2;
}

Private Static void btnivgen ()
{
Byte [] byt2 = convert. frombase64string (txtiv );

MCSP. IV = byt2;
}

Private Static string encryptstring (string value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;

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

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

Private Static string decryptstring (string value)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;

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

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

Private Static metrics ricalgorithm setenc ()
{
Return new descryptoserviceprovider ();
}
}

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.