. NET implementation MD5 encryption SHA1 encryption SHA256 encryption sha384 encryption sha512 encrypted DES encryption and decryption

Source: Internet
Author: User
Tags md5 encryption sha1 sha1 encryption

Write the project, the backstage has been used MD5 encryption, a day group of people ask, in addition to MD5 there are other encryption methods? At that time only know there is a sha, but how to realize what is not clear, so when the internet looked for, the several common encryption methods are collated under, with WinForm wrote a program,:

Key code

Using System.Security;
Using System.Security.Cryptography;
Using System.Web;
Using System.IO;

MD5 is case-insensitive
Type, 16-bit or 32-bit, 16-bit is the 8th to 16th bit to take 32 bits
public string Domd5encode (string pwd,string type)
{
Byte[] result = Encoding.Default.GetBytes (PWD);
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider ();
byte[] Output = Md5.computehash (result);
if (type = = "16")
return bitconverter.tostring (Output). Replace ("-", ""). ToLower (). Substring (8,16);
Else
return bitconverter.tostring (Output). Replace ("-", ""). ToLower ();

}

<summary>
SHA1 Encryption of strings
</summary>
<param name= "Strin" > Strings that need to be encrypted </param>
<returns> redaction </returns>
public string Sha1_encrypt (string source_string)
{
byte[] Strres = Encoding.Default.GetBytes (source_string);
HashAlgorithm ISHA = new SHA1CryptoServiceProvider ();
Strres = Isha.computehash (strres);
StringBuilder entext = new StringBuilder ();
foreach (Byte ibyte in strres)
{
Entext.appendformat ("{0:x2}", Ibyte);
}
return entext.tostring ();
}

<summary>
SHA256 encryption, irreversible
</summary>
<param name= "str" >string str: Encrypted string </param>
<returns> returns the encrypted string </returns>
private string Sha256encrypt (String str)
{
System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed ();
Byte[] byte1;
Byte1 = S256.computehash (Encoding.Default.GetBytes (str));
s256. Clear ();
Return convert.tobase64string (BYTE1);
}

<summary>
SHA384 encryption, irreversible
</summary>
<param name= "str" >string str: Encrypted string </param>
<returns> returns the encrypted string </returns>
private string Sha384encrypt (String str)
{
System.Security.Cryptography.SHA384 s384 = new System.Security.Cryptography.SHA384Managed ();
Byte[] byte1;
Byte1 = S384.computehash (Encoding.Default.GetBytes (str));
s384. Clear ();
Return convert.tobase64string (BYTE1);
}


<summary>
SHA512 encryption, irreversible
</summary>
<param name= "str" >string str: Encrypted string </param>
<returns> returns the encrypted string </returns>
private string Sha512encrypt (String str)
{
System.Security.Cryptography.SHA512 s512 = new System.Security.Cryptography.SHA512Managed ();
Byte[] byte1;
Byte1 = S512.computehash (Encoding.Default.GetBytes (str));
s512. Clear ();
Return convert.tobase64string (BYTE1);
}

Default key vector
Private byte[] Keys = {0xEF, 0xAB, 0x56, 0x78, 0x90, 0x34, 0xCD, 0x12};
<summary>
Des encrypted string
</summary>
<param name= "encryptstring" > Strings to be encrypted </param>
<param name= "Encryptkey" > Encryption key, required for 8-bit </param>
<returns> encryption successfully returns the encrypted string, failed to return the source string </returns>
public string Encryptdes (string encryptstring, String encryptkey)
{
Try
{
byte[] RgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));
byte[] Rgbiv = Keys;
byte[] Inputbytearray = Encoding.UTF8.GetBytes (encryptstring);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, Dcsp.createencryptor (RgbKey, Rgbiv), cryptostreammode.write);
Cstream.write (Inputbytearray, 0, inputbytearray.length);
Cstream.flushfinalblock ();
Return convert.tobase64string (Mstream.toarray ());
}
Catch
{
return encryptstring;
}
}

<summary>
Des decryption string
</summary>
<param name= "Decryptstring" > String to Decrypt </param>
<param name= "Decryptkey" > Decryption key, required for 8-bit, same as encryption key </param>
<returns> decryption succeeded in returning the decrypted string, failed to return to the source string </returns>
public string Decryptdes (string decryptstring, String decryptkey)
{
Try
{
byte[] RgbKey = Encoding.UTF8.GetBytes (decryptkey.substring (0, 8));
byte[] Rgbiv = Keys;
byte[] Inputbytearray = convert.frombase64string (decryptstring);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
MemoryStream mstream = new MemoryStream ();
CryptoStream cstream = new CryptoStream (Mstream, DCSP. CreateDecryptor (RgbKey, Rgbiv), cryptostreammode.write);
Cstream.write (Inputbytearray, 0, inputbytearray.length);
Cstream.flushfinalblock ();
Return Encoding.UTF8.GetString (Mstream.toarray ());
}
Catch
{
return decryptstring;
}
}

Go: goody9807. NET implementation MD5 encryption SHA1 encryption SHA256 encryption sha384 encryption sha512 encrypt des encryption decryption

. NET implementation MD5 encryption SHA1 encryption SHA256 encryption sha384 encryption sha512 encrypted DES encryption and decryption

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.