在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本文是利用一个简单的小例子,简述C#中和加密术有关的内容,仅供学习参考用。 概述随着信息技术的发展,计算机网络为信息的获取、传输、处理、利用与共享提供了一个高效、快捷、安全的通信环境和传输通道,网络信息安全也变得越来越重要。信息安全主要包括两个方面的内容:信息存储安全和信息传输安全。保证网络中信息安全的主要技术是数据的加密与解密。如下图示,说明了加密与解密的过程。
公式算法表示如下: 加密公式:c=Eke(m) (11.1) 解密公式:m=Dkd(c) (11.2)其中m代表明文,c代表密文,E是加密算法,D是解密算法,参数ke称为加密密钥,参数kd称为解密密钥 涉及知识点
散列算法散列算法主要有MD5【Message Digest Algorithm】,RIPEMD160【RACE Integrity Primitives Evaluation Message Digest】和SHA【Secure Hash Algorithm】,SHA又分为SHA1,SHA256,SHA384,SHA512,【散列算法不可逆,是单向操作】如下如所示:
关于散列算法,效果图如下:
HASH算法核心代码如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Security.Cryptography; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DemoCrtpto 9 { 10 /// <summary> 11 /// 信息摘要帮助类 12 /// </summary> 13 public class HashHelper 14 { 15 #region 信息摘要 16 17 /// <summary> 18 /// MD5信息摘要 19 /// </summary> 20 /// <param name="source"></param> 21 /// <returns></returns> 22 public static string GetInfoByMd5(string source) 23 { 24 HashAlgorithm hash = getHashAlgorithm(HashType.MD5); 25 return Encrypto(hash, source); 26 } 27 28 public static string GetInfoBySHA1(string source) 29 { 30 HashAlgorithm hash = getHashAlgorithm(HashType.SHA1); 31 return Encrypto(hash, source); 32 } 33 34 public static string GetInfoBySHA256(string source) 35 { 36 HashAlgorithm hash = getHashAlgorithm(HashType.SHA256); 37 return Encrypto(hash, source); 38 } 39 40 public static string GetInfoBySHA384(string source) 41 { 42 HashAlgorithm hash = getHashAlgorithm(HashType.SHA384); 43 return Encrypto(hash, source); 44 } 45 46 public static string GetInfoBySHA512(string source) 47 { 48 HashAlgorithm hash = getHashAlgorithm(HashType.SHA512); 49 return Encrypto(hash, source); 50 } 51 52 public static string GetInfoByRipeMD(string source) 53 { 54 HashAlgorithm hash = getHashAlgorithm(HashType.RIPEMD); 55 return Encrypto(hash, source); 56 } 57 58 #endregion 59 60 /// <summary> 61 /// 根据类型获取摘要算法名称 62 /// </summary> 63 /// <param name="t"></param> 64 /// <returns></returns> 65 private static HashAlgorithm getHashAlgorithm(HashType t) { 66 HashAlgorithm hash; 67 switch (t) { 68 case HashType.MD5: 69 hash = new MD5CryptoServiceProvider(); 70 break; 71 case HashType.SHA1: 72 hash = new SHA1Managed(); 73 break; 74 case HashType.SHA256: 75 hash = new SHA256Managed(); 76 break; 77 case HashType.SHA384: 78 hash = new SHA384Managed(); 79 break; 80 case HashType.SHA512: 81 hash = new SHA512Managed(); 82 break; 83 case HashType.RIPEMD: 84 hash = new RIPEMD160Managed(); 85 break; 86 default: 87 hash = new MD5CryptoServiceProvider(); 88 break; 89 } 90 return hash; 91 } 92 93 /// <summary> 94 /// 加密 95 /// </summary> 96 /// <param name="hash"></param> 97 /// <param name="source"></param> 98 /// <returns></returns> 99 private static string Encrypto(HashAlgorithm hash, string source) { 100 string dest = string.Empty; 101 try 102 { 103 byte[] btSource = Encoding.Default.GetBytes(source); 104 byte[] btDest = hash.ComputeHash(btSource); 105 dest = Convert.ToBase64String(btDest, 0, btDest.Length); 106 } 107 catch (Exception ex) 108 { 109 throw ex; 110 } 111 return dest; 112 } 113 } 114 115 /// <summary> 116 /// 信息摘要类型 117 /// </summary> 118 public enum HashType { 119 MD5=0, 120 SHA1=1, 121 SHA256=2, 122 SHA384=3, 123 SHA512=4, 124 RIPEMD=5 125 } 126 } 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Security.Cryptography;
5 using System.Text;
6 using System.Threading.Tasks;
7
8 namespace DemoCrtpto
9 {
10 /// <summary>
11 /// 信息摘要帮助类
12 /// </summary>
13 public class HashHelper
14 {
15 #region 信息摘要
16
17 /// <summary>
18 /// MD5信息摘要
19 /// </summary>
20 /// <param name="source"></param>
21 /// <returns></returns>
22 public static string GetInfoByMd5(string source)
23 {
24 HashAlgorithm hash = getHashAlgorithm(HashType.MD5);
25 return Encrypto(hash, source);
26 }
27
28 public static string GetInfoBySHA1(string source)
29 {
30 HashAlgorithm hash = getHashAlgorithm(HashType.SHA1);
31 return Encrypto(hash, source);
32 }
33
34 public static string GetInfoBySHA256(string source)
35 {
36 HashAlgorithm hash = getHashAlgorithm(HashType.SHA256);
37 return Encrypto(hash, source);
38 }
39
40 public static string GetInfoBySHA384(string source)
41 {
42 HashAlgorithm hash = getHashAlgorithm(HashType.SHA384);
43 return Encrypto(hash, source);
44 }
45
46 public static string GetInfoBySHA512(string source)
47 {
48 HashAlgorithm hash = getHashAlgorithm(HashType.SHA512);
49 return Encrypto(hash, source);
50 }
51
52 public static string GetInfoByRipeMD(string source)
53 {
54 HashAlgorithm hash = getHashAlgorithm(HashType.RIPEMD);
55 return Encrypto(hash, source);
56 }
57
58 #endregion
59
60 /// <summary>
61 /// 根据类型获取摘要算法名称
62 /// </summary>
63 /// <param name="t"></param>
64 /// <returns></returns>
65 private static HashAlgorithm getHashAlgorithm(HashType t) {
66 HashAlgorithm hash;
67 switch (t) {
68 case HashType.MD5:
69 hash = new MD5CryptoServiceProvider();
70 break;
71 case HashType.SHA1:
72 hash = new SHA1Managed();
73 break;
74 case HashType.SHA256:
75 hash = new SHA256Managed();
76 break;
77 case HashType.SHA384:
78 hash = new SHA384Managed();
79 break;
80 case HashType.SHA512:
81 hash = new SHA512Managed();
82 break;
83 case HashType.RIPEMD:
84 hash = new RIPEMD160Managed();
85 break;
86 default:
87 hash = new MD5CryptoServiceProvider();
88 break;
89 }
|