TasEncryptHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using Muchinfo.MTPClient.CustomException;
  5. namespace Muchinfo.MTPClient.Infrastructure.Helpers
  6. {
  7. public class TasEncryptHelper
  8. {
  9. [DllImport("libmism32.dll", EntryPoint = "MIMD5Encrypt", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  10. private static extern int MuchinfoMD5Encrypt(IntPtr pDst, ref int iDst, IntPtr pSrc, int iSrc);
  11. [DllImport("libmism32.dll", EntryPoint = "MIMD5GetEncryptDataLen", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  12. private static extern int MuchinfoMD5GetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen);
  13. [DllImport("libmism32.dll", EntryPoint = "MIGetSafeHandle", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  14. private static extern long MIGetSafeHandle();
  15. [DllImport("libmism32.dll", EntryPoint = "MIFreeSafeHandle", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  16. private static extern void MIFreeSafeHandle(long pSafeHandle);
  17. [DllImport("libmism32.dll", EntryPoint = "MILoad",
  18. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  19. private static extern int MILoad(IntPtr pDst, int iDst, long pSafeHandle);
  20. [DllImport("libmism32.dll", EntryPoint = "MITransEncrypt",
  21. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  22. private static extern int MITransEncrypt(IntPtr pDst, int iDst, IntPtr pSrc, int iSrc, long pSafeHandle);
  23. [DllImport("libmism32.dll", EntryPoint = "MIGetEncryptDataLen",
  24. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  25. private static extern int MIGetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen, long pSafeHandle);
  26. [DllImport("libmism32.dll", EntryPoint = "MITransDecrypt",
  27. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  28. private static extern int MITransDecrypt(IntPtr pDst, int iDst, IntPtr pSrc, int iSrc, long pSafeHandle);
  29. [DllImport("libmism32.dll", EntryPoint = "MIGetDecryptDataLen",
  30. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  31. private static extern int MIGetDecryptDataLen(ref int iRevLen, IntPtr pData, int iLen, long pSafeHandle);
  32. [DllImport("libmism32.dll", EntryPoint = "MIAlterTransPwd",
  33. ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
  34. private static extern int MIAlterTransPwd(StringBuilder pPwd, long pSafeHandle);
  35. /// <summary>
  36. /// MD5加密
  37. /// </summary>
  38. /// <param name="source">The source.</param>
  39. /// <returns>System.String.</returns>
  40. public static string MD5(string source)
  41. {
  42. byte[] szOutData = null;
  43. var szInData = Encoding.UTF8.GetBytes(source);
  44. int iResult = 0;
  45. ////加密后的数据长度
  46. int iOutEncryptDataLen = 0;
  47. ////待加密数据长度
  48. int iInEncryptDataLen = szInData.Length;
  49. ////申请内存拷贝待加密数据
  50. IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
  51. Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
  52. // 获取加密后内存的长度
  53. iResult = MuchinfoMD5GetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
  54. if (0 == iResult)
  55. {
  56. ////创建内存
  57. IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
  58. ////加密
  59. iResult = MuchinfoMD5Encrypt(pOutEncryptData, ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
  60. if (iResult == 0)
  61. {
  62. ////拷贝到数组上面
  63. szOutData = new byte[iOutEncryptDataLen];
  64. Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
  65. }
  66. ////释放内存
  67. Marshal.FreeHGlobal(pOutEncryptData);
  68. }
  69. ////释放内存
  70. Marshal.FreeHGlobal(pInEncryptData);
  71. if (szOutData == null)
  72. {
  73. return null;
  74. }
  75. return Encoding.UTF8.GetString(szOutData);
  76. }
  77. private long pSafeHandle;
  78. /// <summary>
  79. /// Initializes a new instance of the <see cref="EncryptHelper"/> class.
  80. /// </summary>
  81. public TasEncryptHelper()
  82. {
  83. int iResult = 0;
  84. int iLoadTemp = 0;
  85. IntPtr pLoadTemp = IntPtr.Zero;
  86. this.pSafeHandle = MIGetSafeHandle();
  87. if (0 == this.pSafeHandle)
  88. {
  89. Console.Write(Muchinfo.MTPClient.Resources.Client_Resource.Infrastructure_FailedToEncryptionAndDecryption);
  90. }
  91. iResult = MILoad(pLoadTemp, iLoadTemp, this.pSafeHandle);
  92. if (0 != iResult)
  93. {
  94. Console.Write(Muchinfo.MTPClient.Resources.Client_Resource.Infrastructure_FailedToLoadKey);
  95. }
  96. }
  97. /// <summary>
  98. /// 修改加密密钥
  99. /// </summary>
  100. /// <param name="keyData">密钥</param>
  101. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  102. public bool AlterKey(StringBuilder keyData)
  103. {
  104. int iResult = 0;
  105. iResult = MIAlterTransPwd(keyData, this.pSafeHandle);
  106. return iResult == 0;
  107. }
  108. /// <summary>
  109. /// 释放加密对象。
  110. /// </summary>
  111. public void FreeHandle()
  112. {
  113. MIFreeSafeHandle(this.pSafeHandle);
  114. }
  115. /// <summary>
  116. /// 加密
  117. /// </summary>
  118. /// <param name="szInStr">需加密的数据</param>
  119. /// <param name="szOutStr">加密后的数据</param>
  120. /// <returns>返回加密是否成功</returns>
  121. public bool Encrypt(string szInStr, out string szOutStr)
  122. {
  123. byte[] szOutData = null;
  124. var szInData = Encoding.UTF8.GetBytes(szInStr);
  125. int iResult = 0;
  126. ////加密后的数据长度
  127. int iOutEncryptDataLen = 0;
  128. bool iRev = false;
  129. ////待加密数据长度
  130. int iInEncryptDataLen = szInData.Length;
  131. ////申请内存拷贝待加密数据
  132. IntPtr pInEncryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iInEncryptDataLen);
  133. System.Runtime.InteropServices.Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
  134. ////获取加密后内存的长度
  135. iResult = MIGetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, this.pSafeHandle);
  136. if (0 == iResult)
  137. {
  138. ////创建内存
  139. IntPtr pOutEncryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iOutEncryptDataLen);
  140. ////加密
  141. iResult = MITransEncrypt(pOutEncryptData, iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, this.pSafeHandle);
  142. if (iResult == 0)
  143. {
  144. iRev = true;
  145. ////拷贝到数组上面
  146. szOutData = new byte[iOutEncryptDataLen];
  147. System.Runtime.InteropServices.Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
  148. }
  149. ////释放内存
  150. System.Runtime.InteropServices.Marshal.FreeHGlobal(pOutEncryptData);
  151. }
  152. ////释放内存
  153. System.Runtime.InteropServices.Marshal.FreeHGlobal(pInEncryptData);
  154. if (szOutData == null)
  155. {
  156. throw new MuchinfoException(ExceptionManager.EncryptError);
  157. }
  158. szOutStr = Convert.ToBase64String(szOutData);
  159. return iRev;
  160. }
  161. /// <summary>
  162. /// 解密
  163. /// </summary>
  164. /// <param name="szInData">需解密的数据</param>
  165. /// <param name="szOutData">解密后的数据</param>
  166. /// <returns>返回解密是否成功</returns>
  167. public bool Decrypt(byte[] szInData, ref byte[] szOutData)
  168. {
  169. bool iRev = false;
  170. int iResult = 0;
  171. ////解密后的数据长度
  172. int iOutDecryptDataLen = 0;
  173. ////待解密数据长度
  174. int iInDecryptDataLen = szInData.Length;
  175. ////申请内存拷贝待解密数据
  176. IntPtr pInDecryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iInDecryptDataLen);
  177. System.Runtime.InteropServices.Marshal.Copy(szInData, 0, pInDecryptData, iInDecryptDataLen);
  178. ////获取解密后内存的长度
  179. iResult = MIGetDecryptDataLen(ref iOutDecryptDataLen, pInDecryptData, iInDecryptDataLen, this.pSafeHandle);
  180. if (0 == iResult)
  181. {
  182. ////创建内存
  183. IntPtr pOutDecryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iOutDecryptDataLen);
  184. ////加密
  185. iResult = MITransDecrypt(pOutDecryptData, iOutDecryptDataLen, pInDecryptData, iInDecryptDataLen, this.pSafeHandle);
  186. if (iResult == 0)
  187. {
  188. iRev = true;
  189. ////拷贝到数组上面
  190. szOutData = new byte[iOutDecryptDataLen];
  191. System.Runtime.InteropServices.Marshal.Copy(pOutDecryptData, szOutData, 0, iOutDecryptDataLen);
  192. }
  193. ////释放内存
  194. System.Runtime.InteropServices.Marshal.FreeHGlobal(pOutDecryptData);
  195. }
  196. ////释放内存
  197. System.Runtime.InteropServices.Marshal.FreeHGlobal(pInDecryptData);
  198. return iRev;
  199. }
  200. /// <summary>
  201. /// 加密密码
  202. /// </summary>
  203. /// <param name="secretKey">The secret key.</param>
  204. /// <param name="bankPassword">The bank password.</param>
  205. /// <returns>System.String.</returns>
  206. /// <exception cref="MuchinfoException"></exception>
  207. public string EncryptPassd(string secretKey, string bankPassword)
  208. {
  209. string encryptPass = string.Empty;
  210. var keyByte = Convert.FromBase64String(secretKey);
  211. byte[] decryptKey = null;
  212. ////使用本地密钥解密密钥
  213. this.Decrypt(keyByte, ref decryptKey);
  214. string bitStr = Encoding.UTF8.GetString(decryptKey);
  215. var sb = new StringBuilder(bitStr);
  216. ////修改密钥
  217. if (this.AlterKey(sb))
  218. {
  219. this.Encrypt(bankPassword, out encryptPass);
  220. this.FreeHandle();
  221. }
  222. else
  223. {
  224. throw new MuchinfoException(ExceptionManager.AlterKeyError);
  225. }
  226. return encryptPass;
  227. }
  228. }
  229. }