在用户登录时我们常常要用到密码加密代码,在读别人代码时看到一段实用加密和解密代码,需要用的朋友请直接看代码。
DES加密/解密类代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Security.Cryptography;
3
using System.Text;
4
namespace ROYcms.DB
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
7
/// DES加密/解密类。
8
/// </summary>
9
public class DESEncrypt
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
public DESEncrypt()
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
}
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
========加密========#region ========加密========
16
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
18
/// 加密
19
/// </summary>
20
/// <param name="Text"></param>
21
/// <returns></returns>
22
public static string Encrypt(string Text)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
return Encrypt(Text,"izhufan.cn");
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
27
/// 加密数据
28
/// </summary>
29
/// <param name="Text"></param>
30
/// <param name="sKey"></param>
31
/// <returns></returns>
32
public static string Encrypt(string Text,string sKey)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
35
byte[] inputByteArray;
36
inputByteArray=Encoding.Default.GetBytes(Text);
37
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
38
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
39
System.IO.MemoryStream ms=new System.IO.MemoryStream();
40
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
41
cs.Write(inputByteArray,0,inputByteArray.Length);
42
cs.FlushFinalBlock();
43
StringBuilder ret=new StringBuilder();
44
foreach( byte b in ms.ToArray())
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
ret.AppendFormat("{0:X2}",b);
47
}
48
return ret.ToString();
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
#endregion
52
53![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
========解密========#region ========解密========
54
55
56![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
57
/// 解密
58
/// </summary>
59
/// <param name="Text"></param>
60
/// <returns></returns>
61
public static string Decrypt(string Text)
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
return Decrypt(Text, "izhufan.cn");
64
}
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
66
/// 解密数据
67
/// </summary>
68
/// <param name="Text"></param>
69
/// <param name="sKey"></param>
70
/// <returns></returns>
71
public static string Decrypt(string Text,string sKey)
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
74
int len;
75
len=Text.Length/2;
76
byte[] inputByteArray = new byte[len];
77
int x,i;
78
for(x=0;x<len;x++)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
81
inputByteArray[x]=(byte)i;
82
}
83
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
84
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
85
System.IO.MemoryStream ms=new System.IO.MemoryStream();
86
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
87
cs.Write(inputByteArray,0,inputByteArray.Length);
88
cs.FlushFinalBlock();
89
return Encoding.Default.GetString(ms.ToArray());
90
}
91
92
#endregion
93![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
}
96
}
97![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
数据加密标准 (DES)是一种加密算法(一种加密信息的方法),1976年被美国联邦政府的联邦信息处理标准(FIPS)所选中,随后既在国际上广泛流传开来。这个算法因为包含一些机密设计元素,相关的短密钥长度以及被怀疑内含国家安全局(NSA)的后门而在开始是有争议的,DES因此收到强烈的学院派式的审查,并以此推动了现代的分组密码及其密码分析。
DES现在已经不视为一种安全的加密算法,因为它使用的56位秘钥过短,以现代计算能力,24小时内即可能被破解。也有一些分析报告提出了该算法的理论上的弱点,虽然实际情况未必出现。该标准在最近已经被高级加密标准(AES)所取代。