C#,asp. NET simple MD5 encryption, decryption

Source: Internet
Author: User
Tags md5 encryption

Simple MD5 Encryption

The first thing to have a decryption rule is key.

The code is as follows

Create key
public string GenerateKey ()
{
DESCryptoServiceProvider descrypto = (DESCryptoServiceProvider) descryptoserviceprovider.create ();
Return ASCIIEncoding.ASCII.GetString (Descrypto.key);
}

Then it's encryption.

The parameters passed in are the strings you want to encrypt, and then the encryption rules;

MD5 encryption
public string Md5encrypt (string ptoencrypt, String SKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = Encoding.Default.GetBytes (Ptoencrypt);
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
StringBuilder ret = new StringBuilder ();

Write your encrypted content according to the rules
foreach (Byte b in Ms. ToArray ())
{
Ret. AppendFormat ("{0:x2}", b);
}
Ret. ToString ();
return ret. ToString ();
}

Decryption to correspond to after encryption:

The arguments passed are: strings encrypted with the rules above, then rules


MD5 decryption
public string Md5decrypt (string ptodecrypt, String SKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
byte[] Inputbytearray = new BYTE[PTODECRYPT.LENGTH/2];

Interpret byte according to rules
for (int x = 0; x < PTODECRYPT.LENGTH/2; × x + +)
{
int i = (Convert.ToInt32 (ptodecrypt.substring (x * 2, 2), 16));
INPUTBYTEARRAY[X] = (byte) i;
}
Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
MemoryStream ms = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();

StringBuilder ret = new StringBuilder ();

Return System.Text.Encoding.Default.GetString (Ms. ToArray ());
}

Simple MD5 encryption, decryption

Can be written as a public class in any place called

Good bye!!!!

C#,asp. NET simple MD5 encryption, decryption

Related Article

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.