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.