asp.net implementation of MD5 encryption and DES encryption and decryption algorithm class complete example _ practical skills

Source: Internet
Author: User
Tags base64 md5 md5 encryption

The example of this paper describes the MD5 encryption and DES encryption and decryption algorithm classes implemented by ASP.net. Share to everyone for your reference, specific as follows:

#region MD5 Algorithm Public
string MD5 (string str, int code)
{
if (code = = 32)//32-bit encryption
{return
SYSTEM.WEB.S Ecurity. FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower ();
}
else//16-bit MD5 encryption (32-bit encrypted 9~25 character)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (str, "MD5"). ToLower (). Substring (8);
}
#endregion

 des encryption #region desencrypt des encryption//<summary>///. </summary>///<param name= "Ptoencrypt" > String to encrypt. </param>///<param name= "SKey" > key, and must be 8 bits. </param>///<returns> Encrypted string returned in BASE64 format. </returns> public String Desencrypt (string ptoencrypt, String SKey) {using (DESCryptoServiceProvider des = new DESC Ryptoserviceprovider ()) {byte[] Inputbytearray = Encoding.UTF8.GetBytes (Ptoencrypt); Des.
Key = ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream (); using (CryptoStream cs = new CryptoStream (MS, Des.) CreateEncryptor (), cryptostreammode.write)) {cs.
Write (Inputbytearray, 0, inputbytearray.length); Cs.
FlushFinalBlock (); Cs.
Close (); String str = convert.tobase64string (MS.
ToArray ()); Ms.
Close ();
return str; #endregion 
Des decryption #region desdecrypt des
///<summary>
///.
///</summary>
///<param name= "Ptodecrypt" > to be decrypted with base64</param>///<param
"Name=" SKey "> key, and must be 8 bits. </param>
///<returns> decrypted string. </returns> public
String Desdecrypt (String ptodecrypt, String SKey)
{
byte[] Inputbytearray = Convert.frombase64string (ptodecrypt);
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider ())
{
des. Key = ASCIIEncoding.ASCII.GetBytes (SKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes (SKey);
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
using (CryptoStream cs = new CryptoStream (MS, Des.) CreateDecryptor (), cryptostreammode.write))
{
cs. Write (Inputbytearray, 0, inputbytearray.length);
Cs. FlushFinalBlock ();
Cs. Close ();
}
String str = Encoding.UTF8.GetString (MS. ToArray ());
Ms. Close ();
return str;
}
}
#endregion

PS: About encryption and decryption interested friends can also refer to the site online tools:

Password Security online detection:
Http://tools.jb51.net/password/my_password_safe

High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword

MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL encryption/decryption tool:
Http://tools.jb51.net/password/urlrethunder

Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

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.