Background: ASP. NET user authentication method and encryption processing
I searched the internet and found many similar articles, which are cited below:
public static string MD5(string Sourcein) { MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider(); byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein); byte[] MD5Out = MD5CSP.ComputeHash(MD5Source); return Convert.ToBase64String(MD5Out); }
In this case, convert. tobase64string is a string containing letters, numbers, and matching strings, for example, afw1fw3j =-
This is different from the encrypted strings stored in the database and cannot be compared. The database stores strings such as baba327d241746ee0829e7e88117d4d5.
Google hasn't found out how to implement the above output in ASP. NET for a long time. So I did it myself, checked msdn, tried it for countless times, and finally found a method. I used the bitconverter class in. net. I didn't talk much about it, so I posted the code,
private void btnMd5_Click( object sender, EventArgs e ) {
if (string.IsNullOrEmpty(txtSrc.Text)) {
txtSrc.Text = "Please Input your string to Encrype";
return;
}
MD5CryptoServiceProvider md5Encrypter = new MD5CryptoServiceProvider();
byte[] theSrc = Encoding.UTF8.GetBytes( txtSrc.Text );
byte[] theResBytes = md5Encrypter.ComputeHash( theSrc );
txtResult.Text = BitConverter.ToString( theResBytes ).Replace( "-", "" );
}
private void btnHash_Click( object sender, EventArgs e ) {
if (string.IsNullOrEmpty( txtSrc.Text )) {
txtSrc.Text = "Please Input your string to Encrype";
return;
}
HashAlgorithm hashEncrypter = new SHA1Managed();
byte[] theSrc = Encoding.UTF8.GetBytes( txtSrc.Text );
byte[] theResBytes = hashEncrypter.ComputeHash( theSrc );
txtResult.Text = BitConverter.ToString( theResBytes ).Replace( "-", "" );
}
Bitconverter. tostring is similar to the results of the BA-BA-32-7D-24-17-46-EE, put the mouse in bitconverter. the summary: converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.
There should be many ways to separate '-' from the string. After testing, I feel that the above method is relatively simple. As for the execution efficiency, there is no comparison of O (operator _ operator) O
If you need this tool, you can download it from favorites: MD5 and hash encryption tools (right-click to save)
It can be seen that Chinese characters can also be encrypted without garbled characters.
Patience to see the last blessing, in fact, in the system. web. security. in formsauthentication, there is a method specifically used to encrypt passwords. It is hashpasswordforstoringinconfigfile. This method requires two parameters. Let's use msdn. Unfortunately, this method only implements MD5 and sha1 encryption methods.
We recommend an online encryption tool, which has many practical tools. Http://www.aspnetresources.com/tools/pwdhash.aspx
If you have any questions, please let us know ~
Appendix: I have just read the internal implementation of hashpasswordforstoringinconfigfile and pasted the code ~
Code
public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat)
{
HashAlgorithm algorithm;
if (password == null)
{
throw new ArgumentNullException("password");
}
if (passwordFormat == null)
{
throw new ArgumentNullException("passwordFormat");
}
if (StringUtil.EqualsIgnoreCase(passwordFormat, "sha1"))
{
algorithm = SHA1.Create();
}
else
{
if (!StringUtil.EqualsIgnoreCase(passwordFormat, "md5"))
{
throw new ArgumentException(SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" }));
}
algorithm = MD5.Create();
}
return MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0);
}