DES加密,解密代码分享-C#版
DES加密和解官代码分享,提供完整C#版DES加密和解官代码下载.
在用户登录时我们常常要用到密码加密代码,在读别人代码时看到一段实用加密和解密代码,需要用的朋友请直接看代码。
DES加密/解密类代码如下:
1
using System;2
using System.Security.Cryptography; 3
using System.Text;4
namespace ROYcms.DB5


{6

/**//// <summary>7
/// DES加密/解密类。8
/// </summary>9
public class DESEncrypt10

{11
public DESEncrypt()12

{ 13
}14

15

========加密========#region ========加密======== 16
17

/**//// <summary>18
/// 加密19
/// </summary>20
/// <param name="Text"></param>21
/// <returns></returns>22
public static string Encrypt(string Text) 23

{24
return Encrypt(Text,"izhufan.cn");25
}26

/**//// <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

{ 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

{ 46
ret.AppendFormat("{0:X2}",b); 47
} 48
return ret.ToString(); 49
} 50

51
#endregion52
53

========解密========#region ========解密======== 54
55
56

/**//// <summary>57
/// 解密58
/// </summary>59
/// <param name="Text"></param>60
/// <returns></returns>61
public static string Decrypt(string Text) 62

{63
return Decrypt(Text, "izhufan.cn");64
}65

/**//// <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

{ 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

{ 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

94

95
}96
}97

DES现在已经不视为一种安全的加密算法,因为它使用的56位秘钥过短,以现代计算能力,24小时内即可能被破解。也有一些分析报告提出了该算法的理论上的弱点,虽然实际情况未必出现。该标准在最近已经被高级加密标准(AES)所取代。
浙公网安备 33010602011771号