DESDZFP.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #region << 版 本 注 释 >>
  2. /**
  3. * 文件名: DESDZFP
  4. *【功 能】
  5. *
  6. *
  7. * 版本 变更时间 部门 作者 变更内容
  8. * ───────────────────────────────────────────────────────────────────────────────────
  9. * V1.0.0 $DATE$ 陈嘉俊 初版
  10. *
  11. *
  12. * Copyright(c)$YEAR$ Zhejiang Aisino Co.,Ltd. All Rights Reserved.
  13. * LICENSE INFORMATION
  14. */
  15. #endregion
  16. using System;
  17. using System.IO;
  18. using System.Runtime.InteropServices;
  19. using System.Security.Cryptography;
  20. using System.Text;
  21. namespace SiteCore.nuonuo
  22. {
  23. [Guid("88433774-746B-4B6D-B043-471EBEC31718")]
  24. public interface IDESDZFP
  25. {
  26. string Encrypt(string content);
  27. string Decrypt(string ciphertext);
  28. }
  29. [ClassInterface(ClassInterfaceType.None)]
  30. [Guid("2AA75AD0-92E8-421D-9B49-C192BF13A606")]
  31. public class DESDZFP
  32. {
  33. private const string Password = "LmMGStGtOpF4xNyvYt54EQ==";
  34. /// <summary>
  35. /// 加密报文数据
  36. /// </summary>
  37. /// <param name="content">待加密的报文</param>
  38. /// <returns>密文</returns>
  39. public static string Encrypt(string content)
  40. {
  41. if (string.IsNullOrEmpty(content))
  42. {
  43. throw new Exception("明文为空!");
  44. }
  45. byte[] BKey = new byte[8];
  46. byte[] BIV = new byte[8];
  47. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  48. des.Mode = CipherMode.CBC;
  49. des.Padding = PaddingMode.PKCS7;
  50. byte[] bytes = Convert.FromBase64String(Password);
  51. Buffer.BlockCopy(bytes, 0, BKey, 0, 8);
  52. Buffer.BlockCopy(bytes, 8, BIV, 0, 8);
  53. byte[] encrypt = Encoding.UTF8.GetBytes(content);
  54. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  55. byte[] md5Hash = md5.ComputeHash(encrypt, 0, encrypt.Length);
  56. byte[] totalByte = CombineBytes(md5Hash, encrypt);
  57. using (MemoryStream ms = new MemoryStream())
  58. {
  59. using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(BKey, BIV), CryptoStreamMode.Write))
  60. {
  61. cs.Write(totalByte, 0, totalByte.Length);
  62. cs.FlushFinalBlock();
  63. return Convert.ToBase64String(ms.ToArray());
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// 解密报文数据
  69. /// </summary>
  70. /// <param name="ciphertext">待解密的报文</param>
  71. /// <returns>明文</returns>
  72. public static string Decrypt(string ciphertext)
  73. {
  74. if (string.IsNullOrEmpty(ciphertext))
  75. {
  76. throw new Exception("密文为空!");
  77. }
  78. ciphertext = ciphertext.Replace(" ", "+");
  79. byte[] BKey = new byte[8];
  80. byte[] BIV = new byte[8];
  81. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  82. des.Mode = CipherMode.CBC;
  83. des.Padding = PaddingMode.PKCS7;
  84. byte[] bytes = Convert.FromBase64String(Password);
  85. Buffer.BlockCopy(bytes, 0, BKey, 0, 8);
  86. Buffer.BlockCopy(bytes, 8, BIV, 0, 8);
  87. byte[] totalByte = null;
  88. using (MemoryStream ms = new MemoryStream())
  89. {
  90. byte[] inData = Convert.FromBase64String(ciphertext);
  91. using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(BKey, BIV), CryptoStreamMode.Write))
  92. {
  93. cs.Write(inData, 0, inData.Length);
  94. cs.FlushFinalBlock();
  95. totalByte = ms.ToArray();
  96. }
  97. }
  98. if (totalByte.Length <= 16)
  99. {
  100. throw new Exception("密文格式错误!");
  101. }
  102. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  103. byte[] md5Hash = md5.ComputeHash(totalByte, 16, totalByte.Length - 16);
  104. for (int i = 0; i < md5Hash.Length; i++)
  105. {
  106. if (md5Hash[i] != totalByte[i])
  107. throw new Exception("Md5校验失败!");
  108. }
  109. return Encoding.UTF8.GetString(totalByte, 16, totalByte.Length - 16);
  110. }
  111. private static byte[] CombineBytes(byte[] bytes1, byte[] bytes2)
  112. {
  113. int len = bytes1.Length + bytes2.Length;
  114. byte[] lenArr = new byte[len];
  115. bytes1.CopyTo(lenArr, 0);
  116. bytes2.CopyTo(lenArr, bytes1.Length);
  117. return lenArr;
  118. }
  119. }
  120. }