Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.IO;
3
using System.Windows.Forms;
4
using System.Collections.Generic;
5
using System.Text;
6
using System.Security.Cryptography;
7
namespace MyDes
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// DES加/解密与MD5加密
11
/// </summary>
12
public class My_Des
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
私有属性#region 私有属性
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
16
/// MD5加密的字符串
17
/// </summary>
18
private string md5Str = null;
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
20
/// DES加密的字符串
21
/// </summary>
22
private string encryptStr = null;
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// DES解密的字符串
25
/// </summary>
26
private string decryptStr = null;
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
28
/// DES密匙
29
/// </summary>
30
private string mydesKey = null;
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
32
/// 返回的字符串
33
/// </summary>
34
private string mydesStr = null;
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
36
/// 错误信息
37
/// </summary>
38
private string messAge = null;
39
#endregion
40![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
公共属性#region 公共属性
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
42
/// MD5加密字符串
43
/// </summary>
44
public string MD5Str
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return md5Str; }
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ md5Str = value; }
48
}
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
50
/// DES加密的字符串
51
/// </summary>
52
public string EncryptStr
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return encryptStr; }
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ encryptStr = value; }
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
58
///DES 解密的字符串
59
/// </summary>
60
public string DecryptStr
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return decryptStr; }
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ decryptStr = value; }
64
}
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
66
/// DES密匙
67
/// </summary>
68
public string MyDesKey
69![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return mydesKey; }
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ mydesKey = value; }
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
74
/// 返回的字符串
75
/// </summary>
76
public string MyDesStr
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return mydesStr; }
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ mydesStr = value; }
80
}
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
82
/// 错误信息
83
/// </summary>
84
public string Message
85![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return messAge; }
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ messAge = value; }
88
}
89
#endregion
90![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
公共方法#region 公共方法
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
92
/// 执行DES加密
93
/// </summary>
94
public void DesEncrypt()
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
try
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
98
byte[] MyStr_E = Encoding.UTF8.GetBytes(this.encryptStr);
99
byte[] MyKey_E = Encoding.UTF8.GetBytes(this.mydesKey);
100
DESCryptoServiceProvider MyDes_E = new DESCryptoServiceProvider();
101
MyDes_E.Key = MyKey_E;
102
MyDes_E.IV = MyKey_E;
103
MemoryStream MyMem_E = new MemoryStream();
104
CryptoStream MyCry_E = new CryptoStream(MyMem_E, MyDes_E.CreateEncryptor(), CryptoStreamMode.Write);
105
MyCry_E.Write(MyStr_E, 0, MyStr_E.Length);
106
MyCry_E.FlushFinalBlock();
107
MyCry_E.Close();
108
this.mydesStr = Convert.ToBase64String(MyMem_E.ToArray());
109
}
110
catch (Exception Error)
111![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
this.messAge = "DES加密出错:"+Error.Message;
113
}
114
}
115![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
116
/// 执行DES解密
117
/// </summary>
118
public void DesDecrypt()
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
120
try
121![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
122
byte[] MyStr_D = Convert.FromBase64String(this.decryptStr);
123
byte[] MyKey_D = Encoding.UTF8.GetBytes(this.mydesKey);
124
DESCryptoServiceProvider MyDes_D = new DESCryptoServiceProvider();
125
MyDes_D.Key = MyKey_D;
126
MyDes_D.IV = MyKey_D;
127
MemoryStream MyMem_D = new MemoryStream();
128
CryptoStream MyCry_D = new CryptoStream(MyMem_D, MyDes_D.CreateDecryptor(), CryptoStreamMode.Write);
129
MyCry_D.Write(MyStr_D, 0, MyStr_D.Length);
130
MyCry_D.FlushFinalBlock();
131
MyCry_D.Close();
132
this.mydesStr = Encoding.UTF8.GetString(MyMem_D.ToArray());
133
}
134
catch (Exception Error)
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
this.messAge ="DES解密出错:" +Error.Message;
137
}
138
}
139![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
140
/// 执行MD5加密
141
/// </summary>
142
public void MD5JiaMi()
143![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
144
MD5CryptoServiceProvider MyMD5 = new MD5CryptoServiceProvider();
145
try
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
147
Byte[] MyMD5_Str = MyMD5.ComputeHash(Encoding.UTF8.GetBytes(this.md5Str));
148
this.MyDesStr = Encoding.UTF8.GetString(MyMD5_Str);
149
}
150
catch (Exception Error)
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
152
this.messAge ="MD5加密出错:"+ Error.Message;
153
}
154
}
155
#endregion
156
}
157
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.IO;
5
using System.Security.Cryptography;
6
namespace ZZZ.Common
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
9
/// DES 加解密
10
/// </summary>
11
public class DES
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
14
/// DES加密
15
/// </summary>
16
/// <param name="input">待加密的字符串</param>
17
/// <param name="key">加密密钥</param>
18
/// <returns></returns>
19
public static string Encrypt(string EncryptString, byte[] Key, byte[] IV)
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
byte[] inputByteArray = Encoding.UTF8.GetBytes(EncryptString);
22
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
23
MemoryStream mStream = new MemoryStream();
24
CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
25
cStream.Write(inputByteArray, 0, inputByteArray.Length);
26
cStream.FlushFinalBlock();
27
return Convert.ToBase64String(mStream.ToArray());
28
}
29![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
30
/// DES解密
31
/// </summary>
32
/// <param name="input">待解密的字符串</param>
33
/// <param name="key">解密密钥,要求为8位,和加密密钥相同</param>
34
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
35
public static string Decrypt(string DecryptString, byte[] Key, byte[] IV)
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
try
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
byte[] inputByteArray = Convert.FromBase64String(DecryptString);
40
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
41
MemoryStream mStream = new MemoryStream();
42
CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
43
cStream.Write(inputByteArray, 0, inputByteArray.Length);
44
cStream.FlushFinalBlock();
45
return Encoding.UTF8.GetString(mStream.ToArray());
46
}
47
catch
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
return string.Empty;
50
}
51
}
52
}
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
54
/// RSA加解密算法
55
/// </summary>
56
public class RSA
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
59
/// RSA加密函数
60
/// </summary>
61
/// <param name="xmlPublicKey">说明KEY必须是XML的行式,返回的是字符串</param>
62
/// <param name="EncryptString"></param>
63
/// <returns></returns>
64
public string Encrypt(string xmlPublicKey, string EncryptString)
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
byte[] PlainTextBArray;
67
byte[] CypherTextBArray;
68
string Result;
69
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
70
rsa.FromXmlString(xmlPublicKey);
71
PlainTextBArray = (new UnicodeEncoding()).GetBytes(EncryptString);
72
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
73
Result = Convert.ToBase64String(CypherTextBArray);
74
return Result;
75
}
76![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
77
/// RSA解密函数
78
/// </summary>
79
/// <param name="xmlPrivateKey"></param>
80
/// <param name="DecryptString"></param>
81
/// <returns></returns>
82
public string Decrypt(string xmlPrivateKey, string DecryptString)
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
84
byte[] PlainTextBArray;
85
byte[] DypherTextBArray;
86
string Result;
87
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
88
rsa.FromXmlString(xmlPrivateKey);
89
PlainTextBArray = Convert.FromBase64String(DecryptString);
90
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
91
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
92
return Result;
93
}
94![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
95
/// 产生RSA的密钥
96
/// </summary>
97
/// <param name="xmlKeys">私钥</param>
98
/// <param name="xmlPublicKey">公钥</param>
99
public void RSAKey(out string xmlKeys, out string xmlPublicKey)
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
101
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
102
xmlKeys = rsa.ToXmlString(true);
103
xmlPublicKey = rsa.ToXmlString(false);
104
}
105
}
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
108![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
109
MD5加密--(MD5解密技术好像才刚刚出现,代码是不会有的了)
110![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
111
// 与asp兼容的md5编码函数。
112
// 目前仅支持 Md5OLD=1 的情况,也就是不对输入的字串进行ansi编码。绝大部分asp程序都是用这种方式获得md5编码。
113
// 输出的md5编码为16位。如需32位编码,请将最后的 WordToHex(b) + WordToHex(c) 修改为:
114
// WordToHex(a) + WordToHex(b) + WordToHex(c) + WordToHex(d)
115![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
116
using System;
117
namespace MyFunc
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
119
public class MyMD5
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
private const int BITS_TO_A_BYTE = 8;
122
private const int BYTES_TO_A_WORD = 4;
123
private const int BITS_TO_A_WORD = 32;
124
private int[] m_lOnBits;
125
private int[] m_l2Power;
126
private int GetASC(string SourceStr)
127![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
128
Byte[] BytesofString = System.Text.Encoding.Default.GetBytes(SourceStr.Substring(0,1));
129
if (BytesofString.Length == 1)
130![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
131
return BytesofString[0];
132
}
133
else
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
int tmpNum = BytesofString[0] * 0x100 + BytesofString[1];
136
if (tmpNum>0x8000) tmpNum -= 0x10000;
137
return tmpNum;
138
}
139
}
140
private int LShift(int lValue, int iShiftBits)
141![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
142
if (iShiftBits==0) return lValue;
143
if (iShiftBits==31)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
if (lValue % 2==1)
146
return Convert.ToInt32(0x80000000-0x100000000);
147
else
148
return 0;
149
}
150
long tmpLShift;
151
if ((lValue & m_l2Power[31 - iShiftBits])!=0)
152![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
153
tmpLShift = ((lValue & m_lOnBits[31 - (iShiftBits + 1)]) * m_l2Power[iShiftBits]) | Convert.ToInt32(0x80000000-0x100000000);
154
}
155
else
156
tmpLShift=(lValue & m_lOnBits[31 - iShiftBits]) * m_l2Power[iShiftBits];
157
tmpLShift %= 0x100000000;
158
if (tmpLShift>=0x80000000) tmpLShift -= 0x100000000;
159
return Convert.ToInt32(tmpLShift);
160
}
161
private int RShift(int lValue,int iShiftBits)
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
if (iShiftBits==0) return lValue;
164
if (iShiftBits==31)
165![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
166
if ((lValue & 0x80000000)!=0)
167
return 1;
168
else
169
return 0;
170
}
171
int tmpRShift = (lValue & 0x7FFFFFFE) / m_l2Power[iShiftBits];
172
if ((lValue & 0x80000000)!=0)
173![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
174
tmpRShift |= (0x40000000 / m_l2Power[iShiftBits - 1]);
175
}
176
return tmpRShift;
177
}
178
private int RotateLeft(int lValue, int iShiftBits)
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
return (LShift(lValue, iShiftBits) | RShift(lValue, (32 - iShiftBits)));
181
}
182
private int AddUnsigned(int lX, int lY)
183![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
184
long lX4,lY4,lX8,lY8,lResult;
185
lX8 = lX & 0x80000000;
186
lY8 = lY & 0x80000000;
187
lX4 = lX & 0x40000000;
188
lY4 = lY & 0x40000000;
189
lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
190
if ((lX4 & lY4)!=0)
191![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
192
lResult = lResult ^ 0x80000000 ^ lX8 ^ lY8;
193
}
194
else
195![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
196
if ((lX4 | lY4)!=0)
197![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
198
if ((lResult & 0x40000000)!=0)
199
lResult = lResult ^ 0xC0000000 ^ lX8 ^ lY8;
200
else
201
lResult = lResult ^ 0x40000000 ^ lX8 ^ lY8;
202
}
203
else
204![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
205
lResult = lResult ^ lX8 ^ lY8;
206
}
207
}
208
lResult %= 0x100000000;
209
if (lResult>=0x80000000) lResult -= 0x100000000;
210
return Convert.ToInt32(lResult);
211
}
212
private int md5_F(int x, int y, int z)
213![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
214
return (x & y) | ((x ^ -1) & z);
215
}
216
private int md5_G(int x, int y, int z)
217![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
218
return (x & z) | (y & (z ^ -1));
219
}
220
private int md5_H(int x, int y, int z)
221![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
222
return (x ^ y ^ z);
223
}
224
private int md5_I(int x, int y, int z)
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
226
return (y ^ (x | (z ^ -1)));
227
}
228
private void md5_FF(ref int a, int b, int c, int d, int x, int s, int ac)
229![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
230
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_F(b, c, d), x), ac));
231
a = RotateLeft(a, s);
232
a = AddUnsigned(a, b);
233
}
234
private void md5_GG(ref int a, int b, int c, int d, int x, int s, int ac)
235![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
236
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_G(b, c, d), x), ac));
237
a = RotateLeft(a, s);
238
a = AddUnsigned(a, b);
239
}
240
private void md5_HH(ref int a, int b, int c, int d, int x, int s, int ac)
241![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
242
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_H(b, c, d), x), ac));
243
a = RotateLeft(a, s);
244
a = AddUnsigned(a, b);
245
}
246
private void md5_II(ref int a, int b, int c, int d, int x, int s, int ac)
247![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
248
a = AddUnsigned(a, AddUnsigned(AddUnsigned(md5_I(b, c, d), x), ac));
249
a = RotateLeft(a, s);
250
a = AddUnsigned(a, b);
251
}
252
private int[] ConvertToWordArray(string sMessage)
253![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
254
int lMessageLength,lNumberOfWords,lBytePosition,lByteCount,lWordCount;
255
const int MODULUS_BITS = 512;
256
const int CONGRUENT_BITS = 448;
257
lMessageLength = sMessage.Length;
258
lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) / BITS_TO_A_BYTE)) / (MODULUS_BITS / BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS / BITS_TO_A_WORD);
259
int[] lWordArray = new int [lNumberOfWords];
260
lBytePosition = 0;
261
lByteCount = 0;
262
while (lByteCount < lMessageLength)
263![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
264
lWordCount = lByteCount / BYTES_TO_A_WORD;
265
lBytePosition = (lByteCount % BYTES_TO_A_WORD) * BITS_TO_A_BYTE;
266
lWordArray[lWordCount] |= LShift(GetASC(sMessage.Substring(lByteCount, 1)), lBytePosition);
267
lByteCount++;
268
}
269
lWordCount = lByteCount / BYTES_TO_A_WORD;
270
lBytePosition = (lByteCount % BYTES_TO_A_WORD) * BITS_TO_A_BYTE;
271
lWordArray[lWordCount] |= LShift(0x80, lBytePosition);
272
lWordArray[lNumberOfWords - 2] = LShift(lMessageLength, 3);
273
lWordArray[lNumberOfWords - 1] = RShift(lMessageLength, 29);
274
return lWordArray;
275
}
276
private string WordToHex(int lValue)
277![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
278
string WordToHextmp,tmpStr;
279
WordToHextmp = "";
280
int lByte;
281
for (int lCount = 0; lCount<=3; lCount++)
282![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
283
lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) & m_lOnBits[BITS_TO_A_BYTE - 1];
284
tmpStr = "0" + Convert.ToString(lByte,16);
285
WordToHextmp += tmpStr.Substring(tmpStr.Length-2, 2);
286
}
287
return WordToHextmp;
288
}
289
public string MD5(string sMessage)
290![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
291![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
m_lOnBits = new int[31]
{1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215,33554431,67108863,134217727,268435455,536870911,1073741823,2147483647};
292![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
m_l2Power = new int[31]
{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824};
293
const int S11 = 7;
294
const int S12 = 12;
295
const int S13 = 17;
296
const int S14 = 22;
297
const int S21 = 5;
298
const int S22 = 9;
299
const int S23 = 14;
300
const int S24 = 20;
301
const int S31 = 4;
302
const int S32 = 11;
303
const int S33 = 16;
304
const int S34 = 23;
305
const int S41 = 6;
306
const int S42 = 10;
307
const int S43 = 15;
308
const int S44 = 21;
309
int k,AA,BB,CC,DD,a,b,c,d;
310
int[] x = ConvertToWordArray(sMessage);
311
a = 0x67452301;
312
b = Convert.ToInt32(0xEFCDAB89-0x100000000);
313
c = Convert.ToInt32(0x98BADCFE-0x100000000);
314
d = 0x10325476;
315
for (k = 0; k < x.Length; k += 16)
316![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
317
AA = a;
318
BB = b;
319
CC = c;
320
DD = d;
321
md5_FF(ref a, b, c, d, x[k + 0], S11, Convert.ToInt32(0xD76AA478-0x100000000));
322
md5_FF(ref d, a, b, c, x[k + 1], S12, Convert.ToInt32(0xE8C7B756-0x100000000));
323
md5_FF(ref c, d, a, b, x[k + 2], S13, 0x242070DB);
324
md5_FF(ref b, c, d, a, x[k + 3], S14, Convert.ToInt32(0xC1BDCEEE-0x100000000));
325
md5_FF(ref a, b, c, d, x[k + 4], S11, Convert.ToInt32(0xF57C0FAF-0x100000000));
326
md5_FF(ref d, a, b, c, x[k + 5], S12, 0x4787C62A);
327
md5_FF(ref c, d, a, b, x[k + 6], S13, Convert.ToInt32(0xA8304613-0x100000000));
328
md5_FF(ref b, c, d, a, x[k + 7], S14, Convert.ToInt32(0xFD469501-0x100000000));
329
md5_FF(ref a, b, c, d, x[k + 8], S11, 0x698098D8);
330
md5_FF(ref d, a, b, c, x[k + 9], S12, Convert.ToInt32(0x8B44F7AF-0x100000000));
331
md5_FF(ref c, d, a, b, x[k + 10], S13, Convert.ToInt32(0xFFFF5BB1-0x100000000));
332
md5_FF(ref b, c, d, a, x[k + 11], S14, Convert.ToInt32(0x895CD7BE-0x100000000));
333
md5_FF(ref a, b, c, d, x[k + 12], S11, 0x6B901122);
334
md5_FF(ref d, a, b, c, x[k + 13], S12, Convert.ToInt32(0xFD987193-0x100000000));
335
md5_FF(ref c, d, a, b, x[k + 14], S13, Convert.ToInt32(0xA679438E-0x100000000));
336
md5_FF(ref b, c, d, a, x[k + 15], S14, 0x49B40821);
337
md5_GG(ref a, b, c, d, x[k + 1], S21, Convert.ToInt32(0xF61E2562-0x100000000));
338
md5_GG(ref d, a, b, c, x[k + 6], S22, Convert.ToInt32(0xC040B340-0x100000000));
339
md5_GG(ref c, d, a, b, x[k + 11], S23, 0x265E5A51);
340
md5_GG(ref b, c, d, a, x[k + 0], S24, Convert.ToInt32(0xE9B6C7AA-0x100000000));
341
md5_GG(ref a, b, c, d, x[k + 5], S21, Convert.ToInt32(0xD62F105D-0x100000000));
342
md5_GG(ref d, a, b, c, x[k + 10], S22, 0x2441453);
343
md5_GG(ref c, d, a, b, x[k + 15], S23, Convert.ToInt32(0xD8A1E681-0x100000000));
344
md5_GG(ref b, c, d, a, x[k + 4], S24, Convert.ToInt32(0xE7D3FBC8-0x100000000));
345
md5_GG(ref a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
346
md5_GG(ref d, a, b, c, x[k + 14], S22, Convert.ToInt32(0xC33707D6-0x100000000));
347
md5_GG(ref c, d, a, b, x[k + 3], S23, Convert.ToInt32(0xF4D50D87-0x100000000));
348
md5_GG(ref b, c, d, a, x[k + 8], S24, 0x455A14ED);
349
md5_GG(ref a, b, c, d, x[k + 13], S21, Convert.ToInt32(0xA9E3E905-0x100000000));
350
md5_GG(ref d, a, b, c, x[k + 2], S22, Convert.ToInt32(0xFCEFA3F8-0x100000000));
351
md5_GG(ref c, d, a, b, x[k + 7], S23, 0x676F02D9);
352
md5_GG(ref b, c, d, a, x[k + 12], S24, Convert.ToInt32(0x8D2A4C8A-0x100000000));
353
md5_HH(ref a, b, c, d, x[k + 5], S31, Convert.ToInt32(0xFFFA3942-0x100000000));
354
md5_HH(ref d, a, b, c, x[k + 8], S32, Convert.ToInt32(0x8771F681-0x100000000));
355
md5_HH(ref c, d, a, b, x[k + 11], S33, 0x6D9D6122);
356
md5_HH(ref b, c, d, a, x[k + 14], S34, Convert.ToInt32(0xFDE5380C-0x100000000));
357
md5_HH(ref a, b, c, d, x[k + 1], S31, Convert.ToInt32(0xA4BEEA44-0x100000000));
358
md5_HH(ref d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
359
md5_HH(ref c, d, a, b, x[k + 7], S33, Convert.ToInt32(0xF6BB4B60-0x100000000));
360
md5_HH(ref b, c, d, a, x[k + 10], S34, Convert.ToInt32(0xBEBFBC70-0x100000000));
361
md5_HH(ref a, b, c, d, x[k + 13], S31, 0x289B7EC6);
362
md5_HH(ref d, a, b, c, x[k + 0], S32, Convert.ToInt32(0xEAA127FA-0x100000000));
363
md5_HH(ref c, d, a, b, x[k + 3], S33, Convert.ToInt32(0xD4EF3085-0x100000000));
364
md5_HH(ref b, c, d, a, x[k + 6], S34, 0x4881D05);
365
md5_HH(ref a, b, c, d, x[k + 9], S31, Convert.ToInt32(0xD9D4D039-0x100000000));
366
md5_HH(ref d, a, b, c, x[k + 12], S32, Convert.ToInt32(0xE6DB99E5-0x100000000));
367
md5_HH(ref c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
368
md5_HH(ref b, c, d, a, x[k + 2], S34, Convert.ToInt32(0xC4AC5665-0x100000000));
369
md5_II(ref a, b, c, d, x[k + 0], S41, Convert.ToInt32(0xF4292244-0x100000000));
370
md5_II(ref d, a, b, c, x[k + 7], S42, 0x432AFF97);
371
md5_II(ref c, d, a, b, x[k + 14], S43, Convert.ToInt32(0xAB9423A7-0x100000000));
372
md5_II(ref b, c, d, a, x[k + 5], S44, Convert.ToInt32(0xFC93A039-0x100000000));
373
md5_II(ref a, b, c, d, x[k + 12], S41, 0x655B59C3);
374
md5_II(ref d, a, b, c, x[k + 3], S42, Convert.ToInt32(0x8F0CCC92-0x100000000));
375
md5_II(ref c, d, a, b, x[k + 10], S43, Convert.ToInt32(0xFFEFF47D-0x100000000));
376
md5_II(ref b, c, d, a, x[k + 1], S44, Convert.ToInt32(0x85845DD1-0x100000000));
377
md5_II(ref a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
378
md5_II(ref d, a, b, c, x[k + 15], S42, Convert.ToInt32(0xFE2CE6E0-0x100000000));
379
md5_II(ref c, d, a, b, x[k + 6], S43, Convert.ToInt32(0xA3014314-0x100000000));
380
md5_II(ref b, c, d, a, x[k + 13], S44, 0x4E0811A1);
381
md5_II(ref a, b, c, d, x[k + 4], S41, Convert.ToInt32(0xF7537E82-0x100000000));
382
md5_II(ref d, a, b, c, x[k + 11], S42, Convert.ToInt32(0xBD3AF235-0x100000000));
383
md5_II(ref c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
384
md5_II(ref b, c, d, a, x[k + 9], S44, Convert.ToInt32(0xEB86D391-0x100000000));
385
a = AddUnsigned(a, AA);
386
b = AddUnsigned(b, BB);
387
c = AddUnsigned(c, CC);
388
d = AddUnsigned(d, DD);
389
}
390
return WordToHex(a) + WordToHex(b) + WordToHex(c) + WordToHex(d).ToLower();
391
}
392![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
393
/// 十六位加密
394
/// </summary>
395
/// <param name="str"></param>
396
/// <param name="size"></param>
397
/// <returns></returns>
398
public string MD5(string str,int size)
399![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
400
string s = MD5(str);
401
if (size == 32)
402![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
403
return s;
404
}
405
return s.Substring(8, 16);
406
}
407
}
408
}
posted @
2009-04-07 11:03
kaka骏
阅读(
210)
评论()
编辑
收藏
举报