使用DES算法实现加密解密

使用DES算法实现加密解密

我们常见的加密算法有DES、MD5、IDEA、AES等等,这篇随笔介绍使用DES算法实现加密解密

首先介绍一下DES算法:

DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行分组,密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个密钥都有奇数个1)分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。

DES算法基本原理:

其入口参数有三个:key、data、mode。key为加密解密使用的密钥,data为加密解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。

附上常用的DES算法加密解密类:

 1 using System;
 2 using System.IO;
 3 using System.Security.Cryptography;
 4 using System.Text;
 5 
 6 namespace CodeFirst.Common
 7 {
 8     public class Encryption
 9     {
10         /// <summary>
11         /// 加密
12         /// </summary>
13         /// <param name="inputString">加密的字符串</param>
14         /// <returns></returns>
15         public static string DesEncrypt(string inputString)
16         {
17             return DesEncrypt(inputString, Key);
18         }
19         /// <summary>
20         /// 解密
21         /// </summary>
22         /// <param name="inputString">解密的字符串</param>
23         /// <returns></returns>
24         public static string DesDecrypt(string inputString)
25         {
26             return DesDecrypt(inputString, Key);
27         }
28         /// <summary>
29         /// 密匙
30         /// </summary>
31         public static string Key
32         {
33             get
34             {
35                 return "hongye10";
36             }
37         }
38         /// <summary>
39         /// 加密字符串
40         /// 注意:密钥必须为8位
41         /// </summary>
42         /// <param name="strText">字符串</param>
43         /// <param name="encryptKey">密钥</param>
44         /// <param name="encryptKey">返回加密后的字符串</param>
45         public static string DesEncrypt(string inputString, string encryptKey)
46         {
47             byte[] byKey = null;
48             byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
49             try
50             {
51                 byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
52                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();//加密服务提供者类
53                 byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString);
54                 MemoryStream ms = new MemoryStream();//内存流
55                 //将数据流连接到加密转换的流
56                 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
57                 cs.Write(inputByteArray, 0, inputByteArray.Length);
58                 cs.FlushFinalBlock();
59                 return Convert.ToBase64String(ms.ToArray());
60             }
61             catch (System.Exception error)
62             {
63                 //return error.Message;
64                 return null;
65             }
66         }
67         /// <summary>
68         /// 解密字符串
69         /// </summary>
70         /// <param name="this.inputString">加了密的字符串</param>
71         /// <param name="decryptKey">密钥</param>
72         /// <param name="decryptKey">返回解密后的字符串</param>
73         public static string DesDecrypt(string inputString, string decryptKey)
74         {
75             byte[] byKey = null;
76             byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
77             byte[] inputByteArray = new Byte[inputString.Length];
78             try
79             {
80                 byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
81                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
82                 inputByteArray = Convert.FromBase64String(inputString);
83                 MemoryStream ms = new MemoryStream();
84                 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
85                 cs.Write(inputByteArray, 0, inputByteArray.Length);
86                 cs.FlushFinalBlock();
87                 System.Text.Encoding encoding = new System.Text.UTF8Encoding();
88                 return encoding.GetString(ms.ToArray());
89             }
90             catch (System.Exception error)
91             {
92                 //return error.Message;
93                 return null;
94             }
95         }
96     }
97 }

猜你喜欢

转载自www.cnblogs.com/gygg/p/11274696.html