You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
7.3 KiB

  1. ///-----------------------------------------------------------------------
  2. /// <copyright file="EncryptMgr.cs" company="Origtek">
  3. /// 程式代號: EncryptMgr
  4. /// 程式名稱: 加解密總管
  5. /// 程式說明: 提供加密解密
  6. /// 起始作者: Hercules
  7. /// 起始日期: 2016/06/17
  8. /// 最新修改人: Hercules
  9. /// 最新修日期: 2016/06/17
  10. /// </copyright>
  11. ///-----------------------------------------------------------------------
  12. #region 程式異動記錄
  13. /// xx.YYYY/MM/DD VER AUTHOR COMMENTS(說明修改的內容)
  14. /// 01.2016/06/17 1.0 Hercules CREATE
  15. /// 最新修改人: Hercules
  16. /// 最新修日期: 2016/06/17
  17. #endregion
  18. using OT.COM.Encryption;
  19. using OT.COM.LogisticsUtil;
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Security.Cryptography;
  25. using System.Text;
  26. namespace SoldierData
  27. {
  28. /// <summary>
  29. /// 類別名稱:EncryptMgr
  30. /// 類別說明:加解密總管
  31. /// 起始作者:Hercules
  32. /// 起始日期:2016/06/17
  33. /// 最新修改人: Hercules
  34. /// 最新修日期: 2016/06/17
  35. /// </summary>
  36. public class SecurityUtil
  37. {
  38. private static Util _ut = new Util();
  39. private static byte[] _Key = System.Text.Encoding.Unicode.GetBytes(_ut.GetSettingString("KEY"));
  40. private static byte[] _IV = System.Text.Encoding.Unicode.GetBytes(_ut.GetSettingString("IV"));
  41. private static AES _aes = new AES();
  42. /// <summary>
  43. /// 函式名稱:Encrypt
  44. /// 函式說明:加密API
  45. /// 起始作者:Hercules
  46. /// 起始日期:2016/06/17
  47. /// 最新修改人: Hercules
  48. /// 最新修日期: 2016/06/17
  49. /// </summary>
  50. /// <param name="i_sPlaintext">明碼</param>
  51. /// <param name="o_sCiphertext">暗碼</param>
  52. /// <returns>錯誤訊息,若成功則為Null</returns>
  53. public static string Encrypt(string i_sPlaintext, out string o_sCiphertext)
  54. {
  55. string sMsg = null;
  56. string sRes = null;
  57. try
  58. {
  59. do
  60. {
  61. byte[] baRes = _aes.EncryptStringToByte(
  62. i_sPlaintext,
  63. _Key,
  64. _IV);
  65. sRes = Convert.ToBase64String(baRes);
  66. }
  67. while (false);
  68. }
  69. catch (Exception ex)
  70. {
  71. sMsg = _ut.GetLastExceptionMsg(ex);
  72. }
  73. finally
  74. {
  75. o_sCiphertext = sRes;
  76. }
  77. return sMsg;
  78. }
  79. /// <summary>
  80. /// 函式名稱:Decrypt
  81. /// 函式說明:解密API
  82. /// 起始作者:Hercules
  83. /// 起始日期:2016/06/17
  84. /// 最新修改人: Hercules
  85. /// 最新修日期: 2016/06/17
  86. /// </summary>
  87. /// <param name="i_sCiphertext">暗碼</param>
  88. /// <param name="o_sPlaintext">明碼</param>
  89. /// <returns>錯誤訊息,若成功則為Null</returns>
  90. public static string Decrypt(string i_sCiphertext, out string o_sPlaintext)
  91. {
  92. string sMsg = null;
  93. string sRes = null;
  94. try
  95. {
  96. do
  97. {
  98. byte[] baCiphertext = Convert.FromBase64String(i_sCiphertext);
  99. sRes = _aes.DecryptByteToString(
  100. baCiphertext,
  101. _Key,
  102. _IV);
  103. }
  104. while (false);
  105. }
  106. catch (Exception ex)
  107. {
  108. sMsg = _ut.GetLastExceptionMsg(ex);
  109. }
  110. finally
  111. {
  112. o_sPlaintext = sRes;
  113. }
  114. return sMsg;
  115. }
  116. //默认密钥向量
  117. private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  118. /**/
  119. /**/
  120. /**/
  121. /// <summary>
  122. /// DES加密字符串
  123. /// </summary>
  124. /// <param name="encryptString">待加密的字符串</param>
  125. /// <param name="encryptKey">加密密钥,要求为8位</param>
  126. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  127. public static string EncryptDES(string encryptString, out string o_sCiphertext)
  128. {
  129. string sMsg = null;
  130. try
  131. {
  132. byte[] rgbKey = Encoding.UTF8.GetBytes("chipmos_".Substring(0, 8));
  133. byte[] rgbIV = Keys;
  134. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  135. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  136. MemoryStream mStream = new MemoryStream();
  137. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  138. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  139. cStream.FlushFinalBlock();
  140. o_sCiphertext = Convert.ToBase64String(mStream.ToArray());
  141. }
  142. catch (Exception ex)
  143. {
  144. o_sCiphertext = encryptString;
  145. sMsg = _ut.GetLastExceptionMsg(ex);
  146. }
  147. return sMsg;
  148. }
  149. /**/
  150. /**/
  151. /**/
  152. /// <summary>
  153. /// DES解密字符串
  154. /// </summary>
  155. /// <param name="decryptString">待解密的字符串</param>
  156. /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
  157. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  158. public static string DecryptDES(string decryptString, out string o_sPlaintext)
  159. {
  160. string sMsg = null;
  161. try
  162. {
  163. byte[] rgbKey = Encoding.UTF8.GetBytes("chipmos_".Substring(0, 8));
  164. byte[] rgbIV = Keys;
  165. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  166. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  167. MemoryStream mStream = new MemoryStream();
  168. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  169. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  170. cStream.FlushFinalBlock();
  171. o_sPlaintext = Encoding.UTF8.GetString(mStream.ToArray());
  172. }
  173. catch (Exception ex)
  174. {
  175. o_sPlaintext = decryptString;
  176. sMsg = _ut.GetLastExceptionMsg(ex);
  177. }
  178. return sMsg;
  179. }
  180. private static char[] constant =
  181. {
  182. '0','1','2','3','4','5','6','7','8','9',
  183. 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  184. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
  185. };
  186. public static string GetRandom(int Length)
  187. {
  188. System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
  189. Random rd = new Random();
  190. for (int i = 0; i < Length; i++)
  191. {
  192. newRandom.Append(constant[rd.Next(62)]);
  193. }
  194. return newRandom.ToString();
  195. }
  196. }
  197. }