Loading

计算机网络安全 —— 报文摘要算法 MD5 (三)

一、报文摘要算法基本概念

​ 使用加密通常可达到报文鉴别的目的,因为伪造的报文解密后一般不能得到可理解的内容。但简单采用这种方法,计算机很难自动识别报文是否被篡改。另外,对于不需要保密而只需要报文鉴别的网络应用,对整个报文的加密和解密,会使计算机增加很多不必要的负担(加密和解密要花费相当多的CPU时间)。

​ 更有效的方法是使用报文摘要(MessageDigest,MD)来进行报文鉴别

​ 发送方将可变 长度的报文m经过报文摘要算法运算后,得出固定长度的报文摘要H(m)。然后对H(m)进行加密,得出EK(H(m)),并将其附加在报文m后面发送出去。接收方把EK(H(m))解密还原为H(m),再把收到的报文进行报文摘要运算,看结果是否与收到的H(m)一样。如不一样,则可断定收到的报文不是发送方产生的。具体流程如下:

img

目前广泛应用的报文摘要算法有MD5[RFC1321]和安全散列算法1(SecureHashAlgorithm,SHA-1)。MD5输出128位的摘要,SHA-1输出160位的摘要。SHA-1比MD5更安全些,但计算起来比MD5要慢

二、.NET 使用 MD5、SHA1、SHA128、SHA512 报文摘要算法

 1 using System;
 2 using System.Security.Cryptography;
 3 using System.Text;
 4 
 5 namespace encryption.md5
 6 {
 7     public class Md5Util
 8     {
 9         public enum MD
10         {
11             MD5,
12             SHA1,
13             SHA256,
14             SHA512,
15         }
16         private static HashAlgorithm CreateHashAlgorithm(MD sha)
17         {
18             switch (sha)
19             {
20                 case MD.MD5:
21                     return new MD5CryptoServiceProvider();
22                 case MD.SHA1:
23                     return SHA1.Create();
24                 case MD.SHA256:
25                     return SHA256.Create();
26                 case MD.SHA512:
27                     return SHA512.Create();
28             }
29             
30             throw new Exception($"The type does not exits,type:{sha}");
31         }
32 
33         /// <summary>
34         /// 获取指定byte数组的MD5
35         /// </summary>
36         /// <param name="source"></param>
37         /// <param name="encoding"><see cref="Encoding"/>默认值:UTF8</param>
38         /// <returns></returns>
39         public static byte[] GetMD5(byte[] source, MD sha=MD.MD5, Encoding encoding = null)
40         {
41             byte[] output = CreateHashAlgorithm(sha).ComputeHash(source);
42             return output;
43         }
44 
45         /// <summary>
46         /// 获取指定字符串的MD5
47         /// </summary>
48         /// <param name="source"></param>
49         /// <param name="encoding"><see cref="Encoding"/>默认值:UTF8</param>
50         /// <returns></returns>
51         public static byte[] GetMD5(string source, MD sha = MD.MD5, Encoding encoding = null)
52         {
53             if (encoding == null) encoding = Encoding.UTF8;
54             return GetMD5(encoding.GetBytes(source), sha, encoding);
55         }
56 
57 
58         /// <summary>
59         /// MD5 校验
60         /// </summary>
61         /// <param name="input">校验二进制</param>
62         /// <param name="hash">待比较的MD5 值</param>
63         /// <param name="encoding"></param>
64         /// <returns>true:相同;false:被纂改</returns>
65         public static bool VerifyMD5(byte[] input, byte[] hash, MD sha = MD.MD5, Encoding encoding = null)
66         {
67             if (encoding == null) encoding = Encoding.UTF8;
68             var buffer = GetMD5(input, sha,encoding);
69             if (Convert.ToBase64String(buffer) == Convert.ToBase64String(hash))
70             {
71                 return true;
72             }
73             return false;
74         }
75 
76         /// <summary>
77         /// MD5 校验
78         /// </summary>
79         /// <param name="input">校验字符串</param>
80         /// <param name="hash">待比较的MD5 值</param>
81         /// <param name="encoding"></param>
82         /// <returns>true:相同;false:被纂改</returns>
83         public static bool VerifyMD5(string input, byte[] hash, MD sha = MD.MD5, Encoding encoding = null)
84         {
85             if (encoding == null) encoding = Encoding.UTF8;
86             return VerifyMD5(encoding.GetBytes(input), hash, sha,encoding);
87         }
88     }
89 }

三、测试代码与结果

 1     static void Main()
 2     {
 3         {
 4             Console.WriteLine("-----------------------------------------------------生成MD5--------------------------------------------------");
 5             var input = "目前广泛应用的报文摘要算法有MD5[RFC1321]和安全散列算法1(SecureHashAlgorithm,SHA-1)。";
 6             Console.WriteLine($"内容:{input}");
 7             byte[] md5 = Md5Util.GetMD5(input);
 8             Console.WriteLine($"MD5:{Convert.ToBase64String(md5)}");
 9         }
10 
11         {
12             Console.WriteLine("-----------------------------------------------------MD5防篡改校验--------------------------------------------------");
13             var input = "https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography.md5?view=net-5.0";
14             Console.WriteLine($"内容:{input}");
15             byte[] md5 = Md5Util.GetMD5(input+"不一致");
16             Console.WriteLine($"MD5校验:{Md5Util.VerifyMD5(input, md5)}");
17         }
18 
19         {
20             Console.WriteLine("-----------------------------------------------------生成SHA512--------------------------------------------------");
21             var input = "目前广泛应用的报文摘要算法有MD5[RFC1321]和安全散列算法1(SecureHashAlgorithm,SHA-1)。";
22             Console.WriteLine($"内容:{input}");
23             byte[] md5 = Md5Util.GetMD5(input,Md5Util.MD.SHA512);
24             Console.WriteLine($"SHA512:{Convert.ToBase64String(md5)}");
25         }
26 
27         {
28             Console.WriteLine("-----------------------------------------------------SHA512防篡改校验--------------------------------------------------");
29             var input = "https://docs.microsoft.com/zh-tw/dotnet/api/system.security.cryptography.md5?view=net-5.0";
30             Console.WriteLine($"内容:{input}");
31             byte[] md5 = Md5Util.GetMD5(input, Md5Util.MD.SHA512);
32             Console.WriteLine($"SHA512校验:{Md5Util.VerifyMD5(input, md5, Md5Util.MD.SHA512)}");
33         }
34 
35         Console.ReadKey();
36     }

img

代码示例:https://github.com/Dwayne112401/encryption

相关内容:计算机网络安全 —— 对称加密算法 DES (一)计算机网络安全 —— 非对称加密算法 RSA 和数字签名(二)计算机网络安全 —— 实体鉴别与生成大随机数(四)

posted @ 2021-01-27 19:06  Dwaynerbing  阅读(1602)  评论(0编辑  收藏  举报