Encryption class
#region ========Encryption========
/// <summary>
/// Encryption
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
Public static string Encrypt(string Text)
{
Return Encrypt(Text, "cong");
}
/// <summary>
/// Encrypt data
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
Public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
Foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
Return ret.ToString();
}
#endregion
#region ========Decrypt========
/// <summary>
/// decrypt
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
Public static string Decrypt(string Text)
{
Return Decrypt(Text, "cong");
}
/// <summary>
/// Decrypt data
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
Public static string Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Int len;
Len = Text.Length / 2;
Byte[] inputByteArray = new byte[len];
Int x, i;
For (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Return Encoding.Default.GetString(ms.ToArray());
}
#endregion
In the. NET 4.5 version, when you use System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile for MD5 encryption, it appears obsolete, such as:
MD5 encrypted outdated display. png
We can replace it with the following methods:
namespaces: System.Web.Security Assembly:system.web (in System.Web.dll)
/// <summary>
/// 32-bit MD5 encryption
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
Private static string Md5Hash(string input)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
Byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
For (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
Return sBuilder.ToString();
}
Originally, I thought the MD5 in System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile was the same as usual. Can be a test today, the results are very different, such as Test,hashpasswordforstoringinconfigfile encoded into C8059e2ec7419f590e79d7f1b774bfe6 And it should be 098f6bcd4621d373cade4e832627b4f6 . and different machines have different results, some of the results are correct A look at the MSDN explanation that turned out to be Given a password and a string identifying the hash type, this routine produces a hash password suitable for storing in A configuration file. In order to be compatible with the previous code and platform compatibility, had to re-write the MD5 algorithm, the use of System.Security.Cryptography.MD5CryptoServiceProvider The code is as follows, everybody executes a bit to know, I will not say more. <script language= "C #" runat= "Server" > String qswhMD5 (String str) { /************qiushuiwuhen (2002-9-27) ***************/ Byte[] B=system.text.encoding.default.getbytes (str); B=new System.Security.Cryptography.MD5CryptoServiceProvider (). ComputeHash (b); String ret= ""; for (int i=0;i<b.length;i++) Ret+=b[i]. ToString ("X"). PadLeft (2, ' 0 '); return ret; } public void encryptstring (Object sender, EventArgs e) { MYMD5.TEXT=QSWHMD5 (Txtclear.text); MD5. Text =system.web.security.formsauthentication.hashpasswordforstoringinconfigfile (TxtClear.Text, "MD5"); } </script> <body Onload=document.all.txtclear.select ();> <form runat= "Server" > PlainText: <asp:textbox id= "txtclear" runat= "Server"/> <asp:button runat= "Server" text= "Md5 Summary" onclick= "encryptstring" id= "Button1"/> Common MD5 for <br/>: <br/><asp:label id= "myMD5" runat= "Server"/> <br/> <br/>hashpasswordforstoringinconfigfile in the MD5: <br/><asp:label id= "MD5" runat= "Server"/> </form> |
MD5 Encryption decryption Class (ASP) & Obsolete processing using MD5