SHA1加签名
一、計算文件
///?<summary>?? ///?SHA1?加密,返回大寫字符串?? ///?</summary>?? ///?<param?name="content">需要加密字符串</param>?? ///?<returns>返回40位UTF8?大寫</returns>?? public?static?string?SHA1(string?content)?? {?? ????return?SHA1(content,?Encoding.UTF8);?? }?? ///?<summary>?? ///?SHA1?加密,返回大寫字符串?? ///?</summary>?? ///?<param?name="content">需要加密字符串</param>?? ///?<param?name="encode">指定加密編碼</param>?? ///?<returns>返回40位大寫字符串</returns>?? public?static?string?SHA1(string?content,?Encoding?encode)?? {?? ????try?? ????{?? ????????SHA1?sha1?=?new?SHA1CryptoServiceProvider();?? ????????byte[]?bytes_in?=?encode.GetBytes(content);?? ????????byte[]?bytes_out?=?sha1.ComputeHash(bytes_in);?? ????????sha1.Dispose();?? ????????string?result?=?BitConverter.ToString(bytes_out);?? ????????result?=?result.Replace("-",?"");?? ????????return?result;?? ????}?? ????catch?(Exception?ex)?? ????{?? ????????throw?new?Exception("SHA1加密出錯:"?+?ex.Message);?? ????}?? }??
| public?static?string?HashCode(string?str)? |
| {? |
| ????string?rethash =?"";? |
| ????try? |
| ????{? |
| ? |
| ??????????System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();? |
| ???????????System.Text.ASCIIEncoding encoder =?new?System.Text.ASCIIEncoding();? |
| ???????????byte[] combined = encoder.GetBytes(str);? |
| ???????????hash.ComputeHash(combined);? |
| ???????????rethash = Convert.ToBase64String(hash.Hash);? |
| ????}? |
| ????catch?(Exception ex)? |
| ????{? |
| ???????????string?strerr =?"Error in HashCode : "?+ ex.Message;? |
| ????}? |
| ????return?rethash;? |
| } |
| ? |
| using?System; |
| namespace?myMethod |
| { |
| ????class?computeMD5andSHA1 |
| ????{ |
| ????????/// <summary> |
| ????????/// 計算文件的 MD5 值 |
| ????????/// </summary> |
| ????????/// <param name="fileName">要計算 MD5 值的文件名和路徑</param> |
| ????????/// <returns>MD5 值16進制字符串</returns> |
| ????????public?string?MD5File(string?fileName) |
| ????????{ |
| ????????????return?HashFile(fileName ,?"md5"); |
| ????????} |
| ? |
| ????????/// <summary> |
| ????????/// 計算文件的 sha1 值 |
| ????????/// </summary> |
| ????????/// <param name="fileName">要計算 sha1 值的文件名和路徑</param> |
| ????????/// <returns>sha1 值16進制字符串</returns> |
| ????????public?string?SHA1File(string?fileName) |
| ????????{ |
| ????????????return?HashFile(fileName ,?"sha1"); |
| ????????} |
| ? |
| ????????/// <summary> |
| ????????/// 計算文件的哈希值 |
| ????????/// </summary> |
| ????????/// <param name="fileName">要計算哈希值的文件名和路徑</param> |
| ????????/// <param name="algName">算法:sha1,md5</param> |
| ????????/// <returns>哈希值16進制字符串</returns> |
| ????????private?string?HashFile(string?fileName ,?string?algName) |
| ????????{ |
| ????????????if?( !System.IO.File.Exists(fileName) ) |
| ????????????????return?string.Empty; |
| ? |
| ????????????System.IO.FileStream fs =?new?System.IO.FileStream(fileName , System.IO.FileMode.Open , System.IO.FileAccess.Read); |
| ????????????byte[] hashBytes = HashData(fs , algName); |
| ????????????fs.Close(); |
| ????????????return?ByteArrayToHexString(hashBytes); |
| ????????} |
| ? |
| ????????/// <summary> |
| ????????/// 計算哈希值 |
| ????????/// </summary> |
| ????????/// <param name="stream">要計算哈希值的 Stream</param> |
| ????????/// <param name="algName">算法:sha1,md5</param> |
| ????????/// <returns>哈希值字節數組</returns> |
| ????????private?byte[] HashData(System.IO.Stream stream ,?string?algName) |
| ????????{ |
| ????????????System.Security.Cryptography.HashAlgorithm algorithm; |
| ????????????if?( algName ==?null?) |
| ????????????{ |
| ????????????????throw?new?ArgumentNullException("algName 不能為 null"); |
| ????????????} |
| ????????????if?(?string.Compare(algName ,?"sha1"?,?true) == 0 ) |
| ????????????{ |
| ????????????????algorithm = System.Security.Cryptography.SHA1.Create(); |
| ????????????} |
| ????????????else |
| ????????????{ |
| ????????????????if?(?string.Compare(algName ,?"md5"?,?true) != 0 ) |
| ????????????????{ |
| ????????????????????throw?new?Exception("algName 只能使用 sha1 或 md5"); |
| ????????????????} |
| ????????????????algorithm = System.Security.Cryptography.MD5.Create(); |
| ????????????} |
| ????????????return?algorithm.ComputeHash(stream); |
| ????????} |
| ? |
| ????????/// <summary> |
| ????????/// 字節數組轉換為16進制表示的字符串 |
| ????????/// </summary> |
| ????????private?string?ByteArrayToHexString(byte[] buf) |
| ????????{ |
| ????????????return?BitConverter.ToString(buf).Replace("-"?,?""); |
| ????????} |
| ????} |
| } |
二、計算文本
三、加密文本
| 123456789101112131415161718192021222324 | class?A{??static?string?GetPwd(string?Pwd)??{????byte[]?data?=?System.Text.Encoding.Default.GetBytes(Pwd);//以字節方式存儲????System.Security.Cryptography.SHA1?sha1?=?new?System.Security.Cryptography.SHA1CryptoServiceProvider();????byte[]?result?=?sha1.ComputeHash(data);//得到哈希值????return?System.BitConverter.ToString(result).Replace("-",?"");?//轉換成為字符串的顯示??}??static?void?Main()??{????string?input?=?"ABCD";????string?Pwd1??=?System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input,?"SHA1");????System.Console.WriteLine(Pwd1);????string?Pwd2?=?GetPwd(input);????System.Console.WriteLine(Pwd2);??}}/*?程序輸出:FB2F85C88567F3C8CE9B799C7C54642D0C7B41F6FB2F85C88567F3C8CE9B799C7C54642D0C7B41F6*/ |
總結
- 上一篇: 网络攻击的一般步骤
- 下一篇: 渗透thinksns官网