EncryptHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using Muchinfo.MTPClient.CustomException;
  2. using Muchinfo.MTPClient.Resources;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace Muchinfo.MTPClient.Infrastructure.Helpers
  7. {
  8. public class EncryptHelper
  9. {
  10. [DllImport("crypto.dll", EntryPoint = "MIGetSafeHandle", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  11. private static extern long MIGetSafeHandle();
  12. [DllImport("crypto.dll", EntryPoint = "MIFreeSafeHandle", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  13. private static extern void MIFreeSafeHandle(long pSafeHandle);
  14. [DllImport("crypto.dll", EntryPoint = "MILoad",
  15. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  16. private static extern int MILoad(IntPtr pDst, int iDst, long pSafeHandle);
  17. [DllImport("crypto.dll", EntryPoint = "MITransEncrypt",
  18. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  19. private static extern int MITransEncrypt(IntPtr pDst, int iDst, IntPtr pSrc, int iSrc, long pSafeHandle);
  20. [DllImport("crypto.dll", EntryPoint = "MIGetEncryptDataLen",
  21. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  22. private static extern int MIGetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen, long pSafeHandle);
  23. [DllImport("crypto.dll", EntryPoint = "MITransDecrypt",
  24. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  25. private static extern int MITransDecrypt(IntPtr pDst, int iDst, IntPtr pSrc, int iSrc, long pSafeHandle);
  26. [DllImport("crypto.dll", EntryPoint = "MIGetDecryptDataLen",
  27. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  28. private static extern int MIGetDecryptDataLen(ref int iRevLen, IntPtr pData, int iLen, long pSafeHandle);
  29. [DllImport("crypto.dll", EntryPoint = "MIMD5Encrypt",
  30. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  31. private static extern int MIMD5Encrypt(IntPtr pDst, ref int iDst, IntPtr pSrc, int iSrc);
  32. [DllImport("crypto.dll", EntryPoint = "MIMD5GetEncryptDataLen",
  33. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  34. private static extern int MIMD5GetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen);
  35. [DllImport("crypto.dll", EntryPoint = "MIAlterTransPwd",
  36. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  37. private static extern int MIAlterTransPwd(StringBuilder pPwd, long pSafeHandle);
  38. [DllImport("crypto.dll", EntryPoint = "MISHA256Encrypt",
  39. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  40. private static extern int MISHA256Encrypt(IntPtr pDst, ref int iDst, IntPtr pSrc, int iSrc);
  41. [DllImport("crypto.dll", EntryPoint = "MISHA256GetEncryptDataLen",
  42. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  43. private static extern int MISHA256GetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen);
  44. /// <summary>
  45. /// SHA256加密
  46. /// </summary>
  47. /// <param name="source">The source.</param>
  48. /// <returns>System.String.</returns>
  49. public static string SHA256(string source)
  50. {
  51. byte[] szOutData = null;
  52. var szInData = Encoding.UTF8.GetBytes(source);
  53. int iResult = 0;
  54. ////加密后的数据长度
  55. int iOutEncryptDataLen = 0;
  56. ////待加密数据长度
  57. int iInEncryptDataLen = szInData.Length;
  58. ////申请内存拷贝待加密数据
  59. IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
  60. Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
  61. // 获取加密后内存的长度
  62. iResult = MISHA256GetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
  63. if (0 == iResult)
  64. {
  65. ////创建内存
  66. IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
  67. ////加密
  68. iResult = MISHA256Encrypt(pOutEncryptData, ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
  69. if (iResult == 0)
  70. {
  71. ////拷贝到数组上面
  72. szOutData = new byte[iOutEncryptDataLen];
  73. Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
  74. }
  75. ////释放内存
  76. Marshal.FreeHGlobal(pOutEncryptData);
  77. }
  78. ////释放内存
  79. Marshal.FreeHGlobal(pInEncryptData);
  80. if (szOutData == null)
  81. {
  82. return null;
  83. }
  84. // return Encoding.UTF8.GetString(szOutData);
  85. return Encoding.UTF8.GetString(szOutData, 0, 64);
  86. }
  87. /// <summary>
  88. /// SHA256加密
  89. /// </summary>
  90. /// <param name="source">The source.</param>
  91. /// <returns>System.String.</returns>
  92. public static string MD5(string source)
  93. {
  94. byte[] szOutData = null;
  95. var szInData = Encoding.UTF8.GetBytes(source);
  96. int iResult = 0;
  97. ////加密后的数据长度
  98. int iOutEncryptDataLen = 0;
  99. ////待加密数据长度
  100. int iInEncryptDataLen = szInData.Length;
  101. ////申请内存拷贝待加密数据
  102. IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
  103. Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
  104. // 获取加密后内存的长度
  105. iResult = MIMD5GetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
  106. if (0 == iResult)
  107. {
  108. ////创建内存
  109. IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
  110. ////加密
  111. iResult = MIMD5Encrypt(pOutEncryptData, ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
  112. if (iResult == 0)
  113. {
  114. ////拷贝到数组上面
  115. szOutData = new byte[iOutEncryptDataLen];
  116. Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
  117. }
  118. ////释放内存
  119. Marshal.FreeHGlobal(pOutEncryptData);
  120. }
  121. ////释放内存
  122. Marshal.FreeHGlobal(pInEncryptData);
  123. if (szOutData == null)
  124. {
  125. return null;
  126. }
  127. return Encoding.UTF8.GetString(szOutData);
  128. }
  129. private readonly long pSafeHandle;
  130. /// <summary>
  131. /// Initializes a new instance of the <see cref="EncryptHelper"/> class.
  132. /// </summary>
  133. public EncryptHelper()
  134. {
  135. int iResult = 0;
  136. int iLoadTemp = 0;
  137. IntPtr pLoadTemp = IntPtr.Zero;
  138. this.pSafeHandle = MIGetSafeHandle();
  139. if (0 == this.pSafeHandle)
  140. {
  141. Console.Write(Client_Resource.Infrastructure_FailedToEncryptionAndDecryption);
  142. }
  143. iResult = MILoad(pLoadTemp, iLoadTemp, this.pSafeHandle);
  144. if (0 != iResult)
  145. {
  146. Console.Write(Client_Resource.Infrastructure_FailedToLoadKey);
  147. }
  148. }
  149. /// <summary>
  150. /// 修改加密密钥
  151. /// </summary>
  152. /// <param name="keyData">密钥</param>
  153. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  154. public bool AlterKey(StringBuilder keyData)
  155. {
  156. int iResult = 0;
  157. iResult = MIAlterTransPwd(keyData, this.pSafeHandle);
  158. return iResult == 0;
  159. }
  160. /// <summary>
  161. /// 释放加密对象。
  162. /// </summary>
  163. public void FreeHandle()
  164. {
  165. MIFreeSafeHandle(this.pSafeHandle);
  166. }
  167. /// <summary>
  168. /// 加密
  169. /// </summary>
  170. /// <param name="szInStr">需加密的数据</param>
  171. /// <param name="szOutStr">加密后的数据</param>
  172. /// <returns>返回加密是否成功</returns>
  173. public bool Encrypt(string szInStr, out string szOutStr)
  174. {
  175. byte[] szOutData = null;
  176. var szInData = Encoding.UTF8.GetBytes(szInStr);
  177. int iResult = 0;
  178. ////加密后的数据长度
  179. int iOutEncryptDataLen = 0;
  180. bool iRev = false;
  181. ////待加密数据长度
  182. int iInEncryptDataLen = szInData.Length;
  183. ////申请内存拷贝待加密数据
  184. IntPtr pInEncryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iInEncryptDataLen);
  185. System.Runtime.InteropServices.Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
  186. ////获取加密后内存的长度
  187. iResult = MIGetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, this.pSafeHandle);
  188. if (0 == iResult)
  189. {
  190. ////创建内存
  191. IntPtr pOutEncryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iOutEncryptDataLen);
  192. ////加密
  193. iResult = MITransEncrypt(pOutEncryptData, iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, this.pSafeHandle);
  194. if (iResult == 0)
  195. {
  196. iRev = true;
  197. ////拷贝到数组上面
  198. szOutData = new byte[iOutEncryptDataLen];
  199. System.Runtime.InteropServices.Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
  200. }
  201. ////释放内存
  202. System.Runtime.InteropServices.Marshal.FreeHGlobal(pOutEncryptData);
  203. }
  204. ////释放内存
  205. System.Runtime.InteropServices.Marshal.FreeHGlobal(pInEncryptData);
  206. if (szOutData == null)
  207. {
  208. throw new MuchinfoException(ExceptionManager.EncryptError);
  209. }
  210. szOutStr = Convert.ToBase64String(szOutData);
  211. return iRev;
  212. }
  213. /// <summary>
  214. /// 解密
  215. /// </summary>
  216. /// <param name="szInData">需解密的数据</param>
  217. /// <param name="szOutData">解密后的数据</param>
  218. /// <returns>返回解密是否成功</returns>
  219. public bool Decrypt(byte[] szInData, ref byte[] szOutData)
  220. {
  221. bool iRev = false;
  222. int iResult = 0;
  223. ////解密后的数据长度
  224. int iOutDecryptDataLen = 0;
  225. ////待解密数据长度
  226. int iInDecryptDataLen = szInData.Length;
  227. ////申请内存拷贝待解密数据
  228. IntPtr pInDecryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iInDecryptDataLen);
  229. System.Runtime.InteropServices.Marshal.Copy(szInData, 0, pInDecryptData, iInDecryptDataLen);
  230. ////获取解密后内存的长度
  231. iResult = MIGetDecryptDataLen(ref iOutDecryptDataLen, pInDecryptData, iInDecryptDataLen, this.pSafeHandle);
  232. if (0 == iResult)
  233. {
  234. ////创建内存
  235. IntPtr pOutDecryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iOutDecryptDataLen);
  236. ////加密
  237. iResult = MITransDecrypt(pOutDecryptData, iOutDecryptDataLen, pInDecryptData, iInDecryptDataLen, this.pSafeHandle);
  238. if (iResult == 0)
  239. {
  240. iRev = true;
  241. ////拷贝到数组上面
  242. szOutData = new byte[iOutDecryptDataLen];
  243. System.Runtime.InteropServices.Marshal.Copy(pOutDecryptData, szOutData, 0, iOutDecryptDataLen);
  244. }
  245. ////释放内存
  246. System.Runtime.InteropServices.Marshal.FreeHGlobal(pOutDecryptData);
  247. }
  248. ////释放内存
  249. System.Runtime.InteropServices.Marshal.FreeHGlobal(pInDecryptData);
  250. return iRev;
  251. }
  252. /// <summary>
  253. /// 解密
  254. /// </summary>
  255. /// <param name="source"></param>
  256. /// <returns></returns>
  257. public string AesDecrypt(string source)
  258. {
  259. byte[] szOutData = null;
  260. var szInData = Hex_16To2(source); //Convert.FromBase64String(source); // Encoding.UTF8.GetBytes(source);
  261. int iResult = 0;
  262. ////加密后的数据长度
  263. int iOutEncryptDataLen = 0;
  264. ////待加密数据长度
  265. int iInEncryptDataLen = szInData.Length;
  266. //获取加密句柄
  267. //long pSafeHandle = MIGetSafeHandle();
  268. ////申请内存拷贝待加密数据
  269. IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
  270. Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
  271. // 获取加密后内存的长度
  272. iResult = MIGetDecryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, pSafeHandle);
  273. if (0 == iResult)
  274. {
  275. ////创建内存
  276. IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
  277. ////加密
  278. iResult = MITransDecrypt(pOutEncryptData, iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, pSafeHandle);
  279. if (iResult == 0)
  280. {
  281. ////拷贝到数组上面
  282. szOutData = new byte[iOutEncryptDataLen];
  283. Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
  284. }
  285. ////释放内存
  286. Marshal.FreeHGlobal(pOutEncryptData);
  287. }
  288. ////释放内存
  289. Marshal.FreeHGlobal(pInEncryptData);
  290. if (szOutData == null)
  291. {
  292. return null;
  293. }
  294. return Encoding.UTF8.GetString(szOutData);
  295. // return Encoding.UTF8.GetString(szOutData, 0, 64);
  296. }
  297. /// <summary>
  298. /// 16进制转2进制
  299. /// </summary>
  300. public Byte[] Hex_16To2(String hexString)
  301. {
  302. if ((hexString.Length % 2) != 0)
  303. {
  304. hexString += " ";
  305. }
  306. Byte[] returnBytes = new Byte[hexString.Length / 2];
  307. for (Int32 i = 0; i < returnBytes.Length; i++)
  308. {
  309. returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
  310. }
  311. return returnBytes;
  312. }
  313. /// <summary>
  314. /// 2进制转16进制
  315. /// </summary>
  316. public String Hex_2To16(Byte[] bytes)
  317. {
  318. String hexString = String.Empty;
  319. Int32 iLength = 65535;
  320. if (bytes != null)
  321. {
  322. StringBuilder strB = new StringBuilder();
  323. if (bytes.Length < iLength)
  324. {
  325. iLength = bytes.Length;
  326. }
  327. for (int i = 0; i < iLength; i++)
  328. {
  329. strB.Append(bytes[i].ToString("x2"));
  330. }
  331. hexString = strB.ToString();
  332. }
  333. return hexString;
  334. }
  335. /// <summary>
  336. /// 加密密码
  337. /// </summary>
  338. /// <param name="secretKey">The secret key.</param>
  339. /// <param name="bankPassword">The bank password.</param>
  340. /// <returns>System.String.</returns>
  341. /// <exception cref="MuchinfoException"></exception>
  342. public string EncryptPassd(string secretKey, string bankPassword)
  343. {
  344. string encryptPass = string.Empty;
  345. var keyByte = Convert.FromBase64String(secretKey);
  346. byte[] decryptKey = null;
  347. ////使用本地密钥解密密钥
  348. this.Decrypt(keyByte, ref decryptKey);
  349. string bitStr = Encoding.UTF8.GetString(decryptKey);
  350. var sb = new StringBuilder(bitStr);
  351. ////修改密钥
  352. if (this.AlterKey(sb))
  353. {
  354. this.Encrypt(bankPassword, out encryptPass);
  355. this.FreeHandle();
  356. }
  357. else
  358. {
  359. throw new MuchinfoException(ExceptionManager.AlterKeyError);
  360. }
  361. return encryptPass;
  362. }
  363. }
  364. }