为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

C#计算字符串哈希值(MD5、SHA)的方法小结

2017-12-01 10页 doc 34KB 50阅读

用户头像

is_769254

暂无简介

举报
C#计算字符串哈希值(MD5、SHA)的方法小结C#计算字符串哈希值(MD5、SHA)的方法小结 C#计算字符串哈希值(MD5、SHA)的方法小结 这篇文章主要介绍了C#计算字符串哈希值(MD5、SHA)的方法,以实例形式较为详细的分析总结了C#计算字符串哈希值的各种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了C#计算字符串哈希值(MD5、SHA)的方法。分享给大家供大家参考。具体如下: 一、关于本文 本文中是一个类库,包括下面几个函数: ? 计算32位MD5码(大小写):Hash_MD5_32 ? 计算16位MD5码(大小写):Has...
C#计算字符串哈希值(MD5、SHA)的方法小结
C#计算字符串哈希值(MD5、SHA)的方法小结 C#计算字符串哈希值(MD5、SHA)的方法小结 这篇文章主要介绍了C#计算字符串哈希值(MD5、SHA)的方法,以实例形式较为详细的总结了C#计算字符串哈希值的各种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了C#计算字符串哈希值(MD5、SHA)的方法。分享给大家供大家参考。具体如下: 一、关于本文 本文中是一个类库,包括下面几个函数: ? 计算32位MD5码(大小写):Hash_MD5_32 ? 计算16位MD5码(大小写):Hash_MD5_16 ? 计算32位2重MD5码(大小写):Hash_2_MD5_32 ? 计算16位2重MD5码(大小写):Hash_2_MD5_16 ? 计算SHA-1码(大小写):Hash_SHA_1 ? 计算SHA-256码(大小写):Hash_SHA_256 ? 计算SHA-384码(大小写):Hash_SHA_384 ? 计算SHA-512码(大小写):Hash_SHA_512 编译后被打包成文件HashTools.dll,其他程序可以在添加引用后对这些函数进行调用 二、类库中各函数代码 1. 类库结构 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace HashTools{ public class HashHelper { //各个函数 }} 2. 计算32位MD5码(大小写):Hash_MD5_32 /// /// 计算32位MD5码/// /// 字符串/// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_MD5_32(string word, bool toUpper = true){ try { System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = MD5CSP.ComputeHash(bytValue); MD5CSP.Clear(); //根据计算得到的Hash码翻译为MD5码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 3. 计算16位MD5码(大小写):Hash_MD5_16 /// /// 计算16位MD5码/// /// 字符串 /// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_MD5_16(string word, bool toUpper = true){ try { string sHash = Hash_MD5_32(word).Substring(8, 16); return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 4. 计算32位2重MD5码(大小写):Hash_2_MD5_32 /// /// 计算32位2重MD5码/// /// 字符串 /// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_2_MD5_32(string word, bool toUpper = true){ try { System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = MD5CSP.ComputeHash(bytValue); //根据计算得到的Hash码翻译为MD5码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } bytValue = System.Text.Encoding.UTF8.GetBytes(sHash); bytHash = MD5CSP.ComputeHash(bytValue); MD5CSP.Clear(); sHash = ""; //根据计算得到的Hash码 翻译为MD5码 for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 5. 计算16位2重MD5码(大小写):Hash_2_MD5_16 /// /// 计算16位2重MD5码/// /// 字符串 /// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_2_MD5_16(string word, bool toUpper = true){ try { System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = MD5CSP.ComputeHash(bytValue); //根据计算得到的Hash码翻译为MD5码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } sHash = sHash.Substring(8, 16); bytValue = System.Text.Encoding.UTF8.GetBytes(sHash); bytHash = MD5CSP.ComputeHash(bytValue); MD5CSP.Clear(); sHash = ""; //根据计算得到的Hash码翻译为MD5码 for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } sHash = sHash.Substring(8, 16); //根据大小写规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 6. 计算SHA-1码(大小写):Hash_SHA_1 /// /// 计算SHA-1码/// /// 字符串/// 返回哈希值格式 true:英文大写,false:英文小写/// public static string Hash_SHA_1(string word, bool toUpper = true){ try { System.Security.Cryptography.SHA1CryptoServiceProvider SHA1CSP = new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA1CSP.ComputeHash(bytValue); SHA1CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 7. 计算SHA-256码(大小写):Hash_SHA_256 /// /// 计算SHA-256码/// /// 字符串 /// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_SHA_256(string word, bool toUpper = true){ try { System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP = new System.Security.Cryptography.SHA256CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA256CSP.ComputeHash(bytValue); SHA256CSP.Clear(); //根据计算得到的Hash码翻译为 SHA-1码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写 规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 8. 计算SHA-384码(大小写):Hash_SHA_384 /// /// 计算SHA-384码/// /// 字符串 /// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_SHA_384(string word, bool toUpper = true){ try { System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP = new System.Security.Cryptography.SHA384CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA384CSP.ComputeHash(bytwww.hunanwang.netValue); SHA384CSP.Clear(); //根据计算得 到的Hash码翻译为SHA-1码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写 规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 9. 计算SHA-512码(大小写):Hash_SHA_512 /// /// 计算SHA-512码/// /// 字符串 /// 返回哈希值格式 true:英文大写,false:英文小写 /// public static string Hash_SHA_512(string word, bool toUpper = true){ try { System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP = new System.Security.Cryptography.SHA512CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word); byte[] bytHash = SHA512CSP.ComputeHash(bytValue); SHA512CSP.Clear(); //根据计算得到的Hash码翻译为 SHA-1码 string sHash = "", sTemp = ""; for (int counter = 0; counter < bytHash.Count(); counter++) { long i = bytHash[counter] / 16; if (i > 9) { sTemp = ((char)(i - 10 + 0x41)).ToString(); } else { sTemp = ((char)(i + 0x30)).ToString(); } i = bytHash[counter] % 16; if (i > 9) { sTemp += ((char)(i - 10 + 0x41)).ToString(); } else { sTemp += ((char)(i + 0x30)).ToString(); } sHash += sTemp; } //根据大小写 规则决定返回的字符串 return toUpper ? sHash : sHash.ToLower(); } catch (Exception ex) { throw new Exception(ex.Message); }} 三、函数调用 建立项目ComputeHash,添加对HashToolswww.visa158.com.dll库的引用。并添加代码: 复制代码 代码如下: using HashTools; 然后在Main函数中添加下列代码: static void Main(string[] args){ Console.WriteLine("MD5 of \"abc\""); Console.WriteLine("MD5_32(Upper): {0}", HashHelper.Hash_MD5_32("abc")); Console.WriteLine("MD5_32(Lower): {0}", HashHelper.Hash_MD5_32("abc", false)); Console.WriteLine("MD5_16(Upper): {0}", HashHelper.Hash_MD5_16("abc")); Console.WriteLine("MD5_16(Lower): {0}", HashHelper.Hash_MD5_16("abc", false)); Console.WriteLine("2_MD5_32(Upper): {0}", HashHelper.Hash_2_MD5_32("abc")); Console.WriteLine("2_MD5_32(Lower): {0}", HashHelper.Hash_2_MD5_32("abc", false)); Console.WriteLine("2_MD5_32(Upper): {0}", HashHelper.Hash_2_MD5_16("abc")); Console.WriteLine("2_MD5_32(Lower): {0}", HashHelper.Hash_2_MD5_16("abc", false)); Console.WriteLine("SHA of \"abc\""); Console.WriteLine("SHA-1(Upper): {0}", HashHelper.Hash_SHA_1("abc")); Console.WriteLine("SHA-1(Lower): {0}", HashHelper.Hash_SHA_1("abc", false)); Console.WriteLine("SHA-256(Upper): {0}", HashHelper.Hash_SHA_256("abc")); Console.WriteLine("SHA-256(Lower): {0}", HashHelper.Hash_SHA_256("abc", false)); Console.WriteLine("SHA-384(Upper): {0}", HashHelper.Hash_SHA_384("abc")); Console.WriteLine("SHA-384(Lower): {0}", HashHelper.Hash_SHA_384("abc", false)); Console.WriteLine("SHA-512(Upper): {0}", HashHelper.Hash_SHA_512("abc")); Console.WriteLine("SHA-512(Lower): {0}", HashHelper.Hash_SHA_512("abc", false)); Console.ReadLine();}
/
本文档为【C#计算字符串哈希值(MD5、SHA)的方法小结】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索