Complete example of MD5 encryption and DES encryption and decryption algorithm classes implemented by asp. net

  • 2021-08-12 02:28:46
  • OfStack

This paper describes the MD5 encryption and DES encryption and decryption algorithm classes implemented by asp. net. Share it for your reference, as follows:


#region MD5 Algorithm 
public string md5(string str, int code)
{
if (code == 32) //32 Bit encryption 
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
}
else //16 Bit MD5 Encryption (take 32 Bit encrypted 9~25 Character) 
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
}
}
#endregion


#region DESEncrypt DES Encryption 
// <summary>
///  Go on DES Encryption. 
/// </summary>
/// <param name= " pToEncrypt " > The string to encrypt. </param>
/// <param name= " sKey " > Key, and must be 8 Bit. </param>
/// <returns> With Base64 Gets or sets the encrypted string returned by the. </returns>
public string DESEncrypt(string pToEncrypt, string sKey)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
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


#region DESDecrypt DES Decryption 
/// <summary>
///  Go on DES Decrypt. 
/// </summary>
/// <param name= " pToDecrypt " > To decrypt with Base64</param>
/// <param name= " sKey " > Key, and must be 8 Bit. </param>
/// <returns> The 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: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

On-line detection of password security:
http://tools.ofstack.com/password/my_password_safe

High Strength Password Generator:
http://tools.ofstack.com/password/CreateStrongPassword

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL Encryption/Decryption Tool:
http://tools.ofstack.com/password/urlrethunder

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

More readers interested in asp. net can check the topics of this site: "Summary of asp. net Operation Skills", "Summary of asp. net String Operation Skills", "Summary of XML Operation Skills", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: