WP7里面的MD5加密

 
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Text;
  12. using System.IO;
  13. using System.Security.Cryptography;
  14.  
  15.  
  16. public class MD5CryptoServiceProvider : MD5Helper
  17. {
  18. public MD5CryptoServiceProvider()
  19. : base()
  20. {
  21. }
  22. }
  23. /// <summary>
  24. /// Summary description for MD5.
  25. /// </summary>
  26. public class MD5Helper : IDisposable
  27. {
  28. static public MD5Helper Create(string hashName)
  29. {
  30. if (hashName == "MD5")
  31. return new MD5Helper();
  32. else
  33. throw new NotSupportedException();
  34. }
  35. static public String GetMd5String(String source)
  36. {
  37. MD5Helper md = MD5CryptoServiceProvider.Create();
  38. byte[] hash;
  39. //Create a new instance of ASCIIEncoding to
  40. //convert the string into an array of Unicode bytes.
  41. UTF8Encoding enc = new UTF8Encoding();
  42. // ASCIIEncoding enc = new ASCIIEncoding();
  43. //Convert the string into an array of bytes.
  44. byte[] buffer = enc.GetBytes(source);
  45. //Create the hash value from the array of bytes.
  46. hash = md.ComputeHash(buffer);
  47. StringBuilder sb = new StringBuilder();
  48. foreach (byte b in hash)
  49. sb.Append(b.ToString("x2"));
  50. return sb.ToString();
  51. }
  52. static public MD5Helper Create()
  53. {
  54. return new MD5Helper();
  55. }
  56. #region base implementation of the MD5
  57. #region constants
  58. private const byte S11 = 7;
  59. private const byte S12 = 12;
  60. private const byte S13 = 17;
  61. private const byte S14 = 22;
  62. private const byte S21 = 5;
  63. private const byte S22 = 9;
  64. private const byte S23 = 14;
  65. private const byte S24 = 20;
  66. private const byte S31 = 4;
  67. private const byte S32 = 11;
  68. private const byte S33 = 16;
  69. private const byte S34 = 23;
  70. private const byte S41 = 6;
  71. private const byte S42 = 10;
  72. private const byte S43 = 15;
  73. private const byte S44 = 21;
  74. static private byte[] PADDING = new byte[] {
  75. 0x80, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  81. };
  82. #endregion
  83. #region F, G, H and I are basic MD5 functions.
  84. static private uint F(uint x, uint y, uint z)
  85. {
  86. return (((x) & (y)) | ((~x) & (z)));
  87. }
  88. static private uint G(uint x, uint y, uint z)
  89. {
  90. return (((x) & (z)) | ((y) & (~z)));
  91. }
  92. static private uint H(uint x, uint y, uint z)
  93. {
  94. return ((x) ^ (y) ^ (z));
  95. }
  96. static private uint I(uint x, uint y, uint z)
  97. {
  98. return ((y) ^ ((x) | (~z)));
  99. }
  100. #endregion
  101. #region rotates x left n bits.
  102. /// <summary>
  103. /// rotates x left n bits.
  104. /// </summary>
  105. /// <param name="x"></param>
  106. /// <param name="n"></param>
  107. /// <returns></returns>
  108. static private uint ROTATE_LEFT(uint x, byte n)
  109. {
  110. return (((x) << (n)) | ((x) >> (32 - (n))));
  111. }
  112. #endregion
  113. #region FF, GG, HH, and II transformations
  114. /// FF, GG, HH, and II transformations
  115. /// for rounds 1, 2, 3, and 4.
  116. /// Rotation is separate from addition to prevent recomputation.
  117. static private void FF(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
  118. {
  119. (a) += F((b), (c), (d)) + (x) + (uint)(ac);
  120. (a) = ROTATE_LEFT((a), (s));
  121. (a) += (b);
  122. }
  123. static private void GG(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
  124. {
  125. (a) += G((b), (c), (d)) + (x) + (uint)(ac);
  126. (a) = ROTATE_LEFT((a), (s));
  127. (a) += (b);
  128. }
  129. static private void HH(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
  130. {
  131. (a) += H((b), (c), (d)) + (x) + (uint)(ac);
  132. (a) = ROTATE_LEFT((a), (s));
  133. (a) += (b);
  134. }
  135. static private void II(ref uint a, uint b, uint c, uint d, uint x, byte s, uint ac)
  136. {
  137. (a) += I((b), (c), (d)) + (x) + (uint)(ac);
  138. (a) = ROTATE_LEFT((a), (s));
  139. (a) += (b);
  140. }
  141. #endregion
  142. #region context info
  143. /// <summary>
  144. /// state (ABCD)
  145. /// </summary>
  146. uint[] state = new uint[4];
  147. /// <summary>
  148. /// number of bits, modulo 2^64 (lsb first)
  149. /// </summary>
  150. uint[] count = new uint[2];
  151. /// <summary>
  152. /// input buffer
  153. /// </summary>
  154. byte[] buffer = new byte[64];
  155. #endregion
  156. internal MD5Helper()
  157. {
  158. Initialize();
  159. }
  160. /// <summary>
  161. /// MD5 initialization. Begins an MD5 operation, writing a new context.
  162. /// </summary>
  163. /// <remarks>
  164. /// The RFC named it "MD5Init"
  165. /// </remarks>
  166. public virtual void Initialize()
  167. {
  168. count[0] = count[1] = 0;
  169. // Load magic initialization constants.
  170. state[0] = 0x67452301;
  171. state[1] = 0xefcdab89;
  172. state[2] = 0x98badcfe;
  173. state[3] = 0x10325476;
  174. }
  175. /// <summary>
  176. /// MD5 block update operation. Continues an MD5 message-digest
  177. /// operation, processing another message block, and updating the
  178. /// context.
  179. /// </summary>
  180. /// <param name="input"></param>
  181. /// <param name="offset"></param>
  182. /// <param name="count"></param>
  183. /// <remarks>The RFC Named it MD5Update</remarks>
  184. protected virtual void HashCore(byte[] input, int offset, int count)
  185. {
  186. int i;
  187. int index;
  188. int partLen;
  189. // Compute number of bytes mod 64
  190. index = (int)((this.count[0] >> 3) & 0x3F);
  191. // Update number of bits
  192. if ((this.count[0] += (uint)((uint)count << 3)) < ((uint)count << 3))
  193. this.count[1]++;
  194. this.count[1] += ((uint)count >> 29);
  195. partLen = 64 - index;
  196. // Transform as many times as possible.
  197. if (count >= partLen)
  198. {
  199. Buffer.BlockCopy(input, offset, this.buffer, index, partLen);
  200. Transform(this.buffer, 0);
  201. for (i = partLen; i + 63 < count; i += 64)
  202. Transform(input, offset + i);
  203. index = 0;
  204. }
  205. else
  206. i = 0;
  207. // Buffer remaining input
  208. Buffer.BlockCopy(input, offset + i, this.buffer, index, count - i);
  209. }
  210. /// <summary>
  211. /// MD5 finalization. Ends an MD5 message-digest operation, writing the
  212. /// the message digest and zeroizing the context.
  213. /// </summary>
  214. /// <returns>message digest</returns>
  215. /// <remarks>The RFC named it MD5Final</remarks>
  216. protected virtual byte[] HashFinal()
  217. {
  218. byte[] digest = new byte[16];
  219. byte[] bits = new byte[8];
  220. int index, padLen;
  221. // Save number of bits
  222. Encode(bits, 0, this.count, 0, 8);
  223. // Pad out to 56 mod 64.
  224. index = (int)((uint)(this.count[0] >> 3) & 0x3f);
  225. padLen = (index < 56) ? (56 - index) : (120 - index);
  226. HashCore(PADDING, 0, padLen);
  227. // Append length (before padding)
  228. HashCore(bits, 0, 8);
  229. // Store state in digest
  230. Encode(digest, 0, state, 0, 16);
  231. // Zeroize sensitive information.
  232. count[0] = count[1] = 0;
  233. state[0] = 0;
  234. state[1] = 0;
  235. state[2] = 0;
  236. state[3] = 0;
  237. // initialize again, to be ready to use
  238. Initialize();
  239. return digest;
  240. }
  241. /// <summary>
  242. /// MD5 basic transformation. Transforms state based on 64 bytes block.
  243. /// </summary>
  244. /// <param name="block"></param>
  245. /// <param name="offset"></param>
  246. private void Transform(byte[] block, int offset)
  247. {
  248. uint a = state[0], b = state[1], c = state[2], d = state[3];
  249. uint[] x = new uint[16];
  250. Decode(x, 0, block, offset, 64);
  251. // Round 1
  252. FF(ref a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
  253. FF(ref d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
  254. FF(ref c, d, a, b, x[2], S13, 0x242070db); /* 3 */
  255. FF(ref b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
  256. FF(ref a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
  257. FF(ref d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
  258. FF(ref c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
  259. FF(ref b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
  260. FF(ref a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
  261. FF(ref d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
  262. FF(ref c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  263. FF(ref b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  264. FF(ref a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  265. FF(ref d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  266. FF(ref c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  267. FF(ref b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  268. // Round 2
  269. GG(ref a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
  270. GG(ref d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
  271. GG(ref c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  272. GG(ref b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
  273. GG(ref a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
  274. GG(ref d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  275. GG(ref c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  276. GG(ref b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
  277. GG(ref a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
  278. GG(ref d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  279. GG(ref c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
  280. GG(ref b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
  281. GG(ref a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  282. GG(ref d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
  283. GG(ref c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
  284. GG(ref b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  285. // Round 3
  286. HH(ref a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
  287. HH(ref d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
  288. HH(ref c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  289. HH(ref b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  290. HH(ref a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
  291. HH(ref d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
  292. HH(ref c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
  293. HH(ref b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  294. HH(ref a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  295. HH(ref d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
  296. HH(ref c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
  297. HH(ref b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
  298. HH(ref a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
  299. HH(ref d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  300. HH(ref c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  301. HH(ref b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
  302. // Round 4
  303. II(ref a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
  304. II(ref d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
  305. II(ref c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  306. II(ref b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
  307. II(ref a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  308. II(ref d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
  309. II(ref c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  310. II(ref b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
  311. II(ref a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
  312. II(ref d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  313. II(ref c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
  314. II(ref b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  315. II(ref a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
  316. II(ref d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  317. II(ref c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
  318. II(ref b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
  319. state[0] += a;
  320. state[1] += b;
  321. state[2] += c;
  322. state[3] += d;
  323. // Zeroize sensitive information.
  324. for (int i = 0; i < x.Length; i++)
  325. x[i] = 0;
  326. }
  327. /// <summary>
  328. /// Encodes input (uint) into output (byte). Assumes len is
  329. /// multiple of 4.
  330. /// </summary>
  331. /// <param name="output"></param>
  332. /// <param name="outputOffset"></param>
  333. /// <param name="input"></param>
  334. /// <param name="inputOffset"></param>
  335. /// <param name="count"></param>
  336. private static void Encode(byte[] output, int outputOffset, uint[] input, int inputOffset, int count)
  337. {
  338. int i, j;
  339. int end = outputOffset + count;
  340. for (i = inputOffset, j = outputOffset; j < end; i++, j += 4)
  341. {
  342. output[j] = (byte)(input[i] & 0xff);
  343. output[j + 1] = (byte)((input[i] >> 8) & 0xff);
  344. output[j + 2] = (byte)((input[i] >> 16) & 0xff);
  345. output[j + 3] = (byte)((input[i] >> 24) & 0xff);
  346. }
  347. }
  348. /// <summary>
  349. /// Decodes input (byte) into output (uint). Assumes len is
  350. /// a multiple of 4.
  351. /// </summary>
  352. /// <param name="output"></param>
  353. /// <param name="outputOffset"></param>
  354. /// <param name="input"></param>
  355. /// <param name="inputOffset"></param>
  356. /// <param name="count"></param>
  357. static private void Decode(uint[] output, int outputOffset, byte[] input, int inputOffset, int count)
  358. {
  359. int i, j;
  360. int end = inputOffset + count;
  361. for (i = outputOffset, j = inputOffset; j < end; i++, j += 4)
  362. output[i] = ((uint)input[j]) | (((uint)input[j + 1]) << 8) | (((uint)input[j + 2]) << 16) | (((uint)input[j + 3]) <<
  363. 24);
  364. }
  365. #endregion
  366. #region expose the same interface as the regular MD5 object
  367. protected byte[] HashValue;
  368. protected int State;
  369. public virtual bool CanReuseTransform
  370. {
  371. get
  372. {
  373. return true;
  374. }
  375. }
  376. public virtual bool CanTransformMultipleBlocks
  377. {
  378. get
  379. {
  380. return true;
  381. }
  382. }
  383. public virtual byte[] Hash
  384. {
  385. get
  386. {
  387. if (this.State != 0)
  388. throw new InvalidOperationException();
  389. return (byte[])HashValue.Clone();
  390. }
  391. }
  392. public virtual int HashSize
  393. {
  394. get
  395. {
  396. return HashSizeValue;
  397. }
  398. }
  399. protected int HashSizeValue = 128;
  400. public virtual int InputBlockSize
  401. {
  402. get
  403. {
  404. return 1;
  405. }
  406. }
  407. public virtual int OutputBlockSize
  408. {
  409. get
  410. {
  411. return 1;
  412. }
  413. }
  414. public void Clear()
  415. {
  416. Dispose(true);
  417. }
  418. public byte[] ComputeHash(byte[] buffer)
  419. {
  420. return ComputeHash(buffer, 0, buffer.Length);
  421. }
  422. public byte[] ComputeHash(byte[] buffer, int offset, int count)
  423. {
  424. Initialize();
  425. HashCore(buffer, offset, count);
  426. HashValue = HashFinal();
  427. return (byte[])HashValue.Clone();
  428. }
  429. public byte[] ComputeHash(Stream inputStream)
  430. {
  431. Initialize();
  432. int count = 0;
  433. byte[] buffer = new byte[4096];
  434. while (0 < (count = inputStream.Read(buffer, 0, 4096)))
  435. {
  436. HashCore(buffer, 0, count);
  437. }
  438. HashValue = HashFinal();
  439. return (byte[])HashValue.Clone();
  440. }
  441. public int TransformBlock(
  442. byte[] inputBuffer,
  443. int inputOffset,
  444. int inputCount,
  445. byte[] outputBuffer,
  446. int outputOffset
  447. )
  448. {
  449. if (inputBuffer == null)
  450. {
  451. throw new ArgumentNullException("inputBuffer");
  452. }
  453. if (inputOffset < 0)
  454. {
  455. throw new ArgumentOutOfRangeException("inputOffset");
  456. }
  457. if ((inputCount < 0) || (inputCount > inputBuffer.Length))
  458. {
  459. throw new ArgumentException("inputCount");
  460. }
  461. if ((inputBuffer.Length - inputCount) < inputOffset)
  462. {
  463. throw new ArgumentOutOfRangeException("inputOffset");
  464. }
  465. if (this.State == 0)
  466. {
  467. Initialize();
  468. this.State = 1;
  469. }
  470. HashCore(inputBuffer, inputOffset, inputCount);
  471. if ((inputBuffer != outputBuffer) || (inputOffset != outputOffset))
  472. {
  473. Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
  474. }
  475. return inputCount;
  476. }
  477. public byte[] TransformFinalBlock(
  478. byte[] inputBuffer,
  479. int inputOffset,
  480. int inputCount
  481. )
  482. {
  483. if (inputBuffer == null)
  484. {
  485. throw new ArgumentNullException("inputBuffer");
  486. }
  487. if (inputOffset < 0)
  488. {
  489. throw new ArgumentOutOfRangeException("inputOffset");
  490. }
  491. if ((inputCount < 0) || (inputCount > inputBuffer.Length))
  492. {
  493. throw new ArgumentException("inputCount");
  494. }
  495. if ((inputBuffer.Length - inputCount) < inputOffset)
  496. {
  497. throw new ArgumentOutOfRangeException("inputOffset");
  498. }
  499. if (this.State == 0)
  500. {
  501. Initialize();
  502. }
  503. HashCore(inputBuffer, inputOffset, inputCount);
  504. HashValue = HashFinal();
  505. byte[] buffer = new byte[inputCount];
  506. Buffer.BlockCopy(inputBuffer, inputOffset, buffer, 0, inputCount);
  507. this.State = 0;
  508. return buffer;
  509. }
  510. #endregion
  511. protected virtual void Dispose(bool disposing)
  512. {
  513. if (!disposing)
  514. Initialize();
  515. }
  516. public void Dispose()
  517. {
  518. Dispose(true);
  519. }
  520. }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值