c#md5加密

Posted on 2008-05-07 22:23  少林  阅读(...)  评论(...编辑  收藏

1. 首先创建MD5的哈希算法。
    ((HashAlgorithm)System.Security.Cryptogrophy.CryptoConfig.CreateFromName("MD5")).ComputeHash(System.Text.UTF8.GetBytes(input))
2.计算哈希值
    使用方法:ComputeHash(byte[] value);
3.转化成字符串.
    

 1protected virtual string HashMD5(string input)
 2        {
 3            byte[] result = ((HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(System.Text.Encoding.UTF8.GetBytes(input));
 4            StringBuilder output = new StringBuilder(16);            
 5                                    
 6            for (int i = 0; i < result.Length; i++
 7            {                            
 8                // convert from hexa-decimal to character
 9                output.Append((result[i]).ToString( "x2", System.Globalization.CultureInfo.InvariantCulture));                
10            }

11            return output.ToString();
12        }

13

posts - 70, comments - 306, trackbacks - 0, articles - 4

Copyright © 2020 少林
Powered by .NET Core 3.1.0 on Linux