积极答复者
MD5已过时:请问用什么方法替代?

问题
答案
-
MD5没过时,此方法从字面翻译过来是“生成密码的 HASH 值,用于存储在配置文件中”,所以是HashPasswordForStoringInConfigFile 方法不推荐使用了,而不是 MD5 不推荐使用了;并且,此方法并不只是采用 MD5 的算法,通过方法的第二个参数 passwordFormat ,你还可以指定 SHA1 算法。
HashAlgorithm hashAlgorithm = CryptoAlgorithms.CreateSHA1() 或者 CryptoAlgorithms.CreateMD5()
byte[] data = result =hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(password)); // 请注意选择你的 Encoding
- 已建议为答案 Happy Chen - MSFTModerator 2013年9月17日 1:32
- 已标记为答案 佛根 2014年4月16日 6:03
全部回复
-
using System; using System.Security.Cryptography; using System.Text; MD5 md5Hash = MD5.Create(); string source = “str”; string hash = GetMd5Hash(md5Hash, source); static string GetMd5Hash(MD5 md5Hash, string input) { // Convert the input string to a byte array and compute the hash. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); }
e-mail:ist_te@hotmail.com
- 已建议为答案 算神 2013年11月6日 9:31
-
MD5没过时,此方法从字面翻译过来是“生成密码的 HASH 值,用于存储在配置文件中”,所以是HashPasswordForStoringInConfigFile 方法不推荐使用了,而不是 MD5 不推荐使用了;并且,此方法并不只是采用 MD5 的算法,通过方法的第二个参数 passwordFormat ,你还可以指定 SHA1 算法。
HashAlgorithm hashAlgorithm = CryptoAlgorithms.CreateSHA1() 或者 CryptoAlgorithms.CreateMD5()
byte[] data = result =hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(password)); // 请注意选择你的 Encoding
- 已建议为答案 Happy Chen - MSFTModerator 2013年9月17日 1:32
- 已标记为答案 佛根 2014年4月16日 6:03