C # Two ways to MD5 encryption

Source: Internet
Author: User
Tags md5 encryption

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Security.Cryptography;

Namespace XXXXX
{
<summary>
Encrypting files Help Class
</summary>
public class Encryptionhelper
{

<summary>
MD5 (16-bit encryption)
</summary>
<param name= "convertstring" > Strings that need to be encrypted </param>
<RETURNS>MD5 Encrypted String </returns>
public static string Getmd5str (String convertstring)
{
String md5pwd = String. Empty;

Using Cryptographic service Providers
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider ();

Converts the numeric value of each element of the specified byte subarray to its equivalent hexadecimal string representation.
Md5pwd = bitconverter.tostring (Md5.computehash (UTF8Encoding.Default.GetBytes (convertstring)), 4, 8);

Md5pwd = Md5pwd.replace ("-", "" ");

return md5pwd;
}

<summary>
MD5 (32-bit encryption)
</summary>
<param name= "str" > strings that need to be encrypted </param>
<RETURNS>MD5 Encrypted String </returns>
public static string Getmd5hashstr (String str)
{
string pwd = string. Empty;

Instantiate a MD5 pair of images
MD5 MD5 = MD5. Create ();

After the encryption is an array of byte type, here should pay attention to the choice of coding utf8/unicode, etc.
Byte[] s = Md5.computehash (Encoding.UTF8.GetBytes (str));

Converts an array of byte types to a string by using a loop, which is a regular character formatting the resulting
for (int i = 0; i < s.length; i++)
{
The resulting string is formatted using the hexadecimal type. The formatted character is a lowercase letter, and if uppercase (X) is used, the character after the format is uppercase characters
PWD = pwd + s[i]. ToString ("X");
}

return pwd;
}
}
}

C # Two ways to MD5 encryption

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.