EncryptHelper.cs 16 KB

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