Asp.net implements MD5 Encryption

Source: Internet
Author: User
Very simple, just a piece of code. Response. Write (FormsAuthentication. HashPasswordForStoringInConfigFile ("string to be encrypted", "MD5 "));

To make the blog post longer and more valuable, add the frequently-used encryption and decryption code:

Code

/* Usage
Protected void Page_Load (object sender, EventArgs e)
{
// Encryption
This. Title = CEncrypt. DesEncrypt ("pwd", CEncrypt. Key );
This. Title + = CEncrypt. DesDecrypt (this. Title, CEncrypt. Key );
Response. Write (CEncrypt. DesDecrypt ("gAYyhdLQunc =", CEncrypt. Key ));
}
*/
Using System;
Using System. IO;
Using System. Text;
Using System. Security. Cryptography;
Using System. Web;

Namespace YD. Common
{
/// <Summary>
/// Add a password
/// </Summary>
Public class CEncrypt
{
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "inputString"> </param>
/// <Returns> </returns>
Public static string DesEncrypt (string inputString)
{
Return DesEncrypt (inputString, Key );
}
/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "inputString"> </param>
/// <Returns> </returns>
Public static string DesDecrypt (string inputString)
{
Return DesDecrypt (inputString, Key );
}
/// <Summary>
/// Key
/// </Summary>
Private static string Key
{
Get
{
Return "hongye10 ";
}
}
/// <Summary>
/// Encrypted string
/// Note: The key must be 8 bits.
/// </Summary>
/// <Param name = "strText"> string </param>
/// <Param name = "encryptKey"> key </param>
/// <Param name = "encryptKey"> return the encrypted string </param>
Public static string DesEncrypt (string inputString, string encryptKey)
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (encryptKey. Substring (0, 8 ));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
Byte [] inputByteArray = Encoding. UTF8.GetBytes (inputString );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateEncryptor (byKey, IV), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
Return Convert. ToBase64String (ms. ToArray ());
}
Catch (System. Exception error)
{
// Return error. Message;
Return null;
}
}
/// <Summary>
/// Decrypt the string
/// </Summary>
/// <Param name = "this. inputString"> encrypted string </param>
/// <Param name = "decryptKey"> key </param>
/// <Param name = "decryptKey"> return the decrypted string </param>
Public static string DesDecrypt (string inputString, string decryptKey)
{
Byte [] byKey = null;
Byte [] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Byte [] inputByteArray = new Byte [inputString. Length];
Try
{
ByKey = System. Text. Encoding. UTF8.GetBytes (decryptKey. Substring (0, 8 ));
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
InputByteArray = Convert. FromBase64String (inputString );
MemoryStream MS = new MemoryStream ();
CryptoStream cs = new CryptoStream (MS, des. CreateDecryptor (byKey, IV), CryptoStreamMode. Write );
Cs. Write (inputByteArray, 0, inputByteArray. Length );
Cs. FlushFinalBlock ();
System. Text. Encoding encoding = new System. Text. UTF8Encoding ();
Return encoding. GetString (ms. ToArray ());
}
Catch (System. Exception error)
{
// Return error. Message;
Return null;
}
}
}
}

 

 

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.