. NET two kinds of encryption methods MD5 and Dec

Source: Internet
Author: User
Tags decrypt md5 encryption

MD5 encryption

<summary>
MD5 encryption
</summary>
<param name= "Tocrystring" > string to encrypt </param>
<returns></returns>
public static string Getmd5str (String tocrystring)
{
MD5CryptoServiceProvider hashmd5;
HASHMD5 = new MD5CryptoServiceProvider ();
Return bitconverter.tostring (Hashmd5.computehash (Encoding.Default.GetBytes (tocrystring)). Replace ("-", ""). ToLower ();//asp is lowercase, lowercase all characters
}

DES encryption and decryption

///
DEC Encryption Process
///
public static string Encrypt (String ptoencrypt, String SKey)
{
Descryptoserviceproviderdes = new DESCryptoServiceProvider (); Put the string in a byte array

byte[] Inputbytearray = Encoding.Default.GetBytes (Ptoencrypt);
Byte[] Inputbytearray=encoding.unicode.getbytes (ptoencrypt);

Des. Key = ASCIIEncoding.ASCII.GetBytes (SKey); Establish keys and offsets for encrypted objects
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey); GetBytes method of using Asciiencoding.ascii method in original text
MemoryStream ms = new MemoryStream (); Make the input password must be entered in English text
CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);

Cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();

StringBuilder ret = new StringBuilder ();
foreach (Byte b in Ms. ToArray ())
{
Ret. AppendFormat ("{0:x2}", b);
}
Ret. ToString ();
return ret. ToString ();
}

///
DEC decryption Process
///
public static string Decrypt (String ptodecrypt, String SKey)
{
Descryptoserviceproviderdes = new DESCryptoServiceProvider ();

byte[] Inputbytearray = new BYTE[PTODECRYPT.LENGTH/2];
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); Establishes the key and offset of the encrypted object, which is important and cannot be modified
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 (); Create Stringbuild object, Createdecrypt use Stream object, must turn decrypted text into stream object

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

The skey is very important, defined as a string and then assigned equal to eight letters or numbers, note that must be 8
This is also very useful, for example, you want to enter the article page, the parameters passed in the aid=10000
Then encrypt the 10000.
Then decrypt it when accepted. This will effectively prevent SQL injection attacks!

. NET two kinds of encryption methods MD5 and Dec

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.