| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- using Muchinfo.MTPClient.CustomException;
- using Muchinfo.MTPClient.Resources;
- using System;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace Muchinfo.MTPClient.Infrastructure.Helpers
- {
- public class EncryptHelper
- {
- [DllImport("crypto.dll", EntryPoint = "MIGetSafeHandle", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern long MIGetSafeHandle();
- [DllImport("crypto.dll", EntryPoint = "MIFreeSafeHandle", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern void MIFreeSafeHandle(long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MILoad",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MILoad(IntPtr pDst, int iDst, long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MITransEncrypt",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MITransEncrypt(IntPtr pDst, int iDst, IntPtr pSrc, int iSrc, long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MIGetEncryptDataLen",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MIGetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen, long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MITransDecrypt",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MITransDecrypt(IntPtr pDst, int iDst, IntPtr pSrc, int iSrc, long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MIGetDecryptDataLen",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MIGetDecryptDataLen(ref int iRevLen, IntPtr pData, int iLen, long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MIMD5Encrypt",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MIMD5Encrypt(IntPtr pDst, ref int iDst, IntPtr pSrc, int iSrc);
- [DllImport("crypto.dll", EntryPoint = "MIMD5GetEncryptDataLen",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MIMD5GetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen);
- [DllImport("crypto.dll", EntryPoint = "MIAlterTransPwd",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MIAlterTransPwd(StringBuilder pPwd, long pSafeHandle);
- [DllImport("crypto.dll", EntryPoint = "MISHA256Encrypt",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MISHA256Encrypt(IntPtr pDst, ref int iDst, IntPtr pSrc, int iSrc);
- [DllImport("crypto.dll", EntryPoint = "MISHA256GetEncryptDataLen",
- ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
- private static extern int MISHA256GetEncryptDataLen(ref int iRevLen, IntPtr pData, int iLen);
- /// <summary>
- /// SHA256加密
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns>System.String.</returns>
- public static string SHA256(string source)
- {
- byte[] szOutData = null;
- var szInData = Encoding.UTF8.GetBytes(source);
- int iResult = 0;
- ////加密后的数据长度
- int iOutEncryptDataLen = 0;
- ////待加密数据长度
- int iInEncryptDataLen = szInData.Length;
- ////申请内存拷贝待加密数据
- IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
- Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
- // 获取加密后内存的长度
- iResult = MISHA256GetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
- if (0 == iResult)
- {
- ////创建内存
- IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
- ////加密
- iResult = MISHA256Encrypt(pOutEncryptData, ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
- if (iResult == 0)
- {
- ////拷贝到数组上面
- szOutData = new byte[iOutEncryptDataLen];
- Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
- }
- ////释放内存
- Marshal.FreeHGlobal(pOutEncryptData);
- }
- ////释放内存
- Marshal.FreeHGlobal(pInEncryptData);
- if (szOutData == null)
- {
- return null;
- }
- // return Encoding.UTF8.GetString(szOutData);
- return Encoding.UTF8.GetString(szOutData, 0, 64);
- }
- /// <summary>
- /// SHA256加密
- /// </summary>
- /// <param name="source">The source.</param>
- /// <returns>System.String.</returns>
- public static string MD5(string source)
- {
- byte[] szOutData = null;
- var szInData = Encoding.UTF8.GetBytes(source);
- int iResult = 0;
- ////加密后的数据长度
- int iOutEncryptDataLen = 0;
- ////待加密数据长度
- int iInEncryptDataLen = szInData.Length;
- ////申请内存拷贝待加密数据
- IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
- Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
- // 获取加密后内存的长度
- iResult = MIMD5GetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
- if (0 == iResult)
- {
- ////创建内存
- IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
- ////加密
- iResult = MIMD5Encrypt(pOutEncryptData, ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen);
- if (iResult == 0)
- {
- ////拷贝到数组上面
- szOutData = new byte[iOutEncryptDataLen];
- Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
- }
- ////释放内存
- Marshal.FreeHGlobal(pOutEncryptData);
- }
- ////释放内存
- Marshal.FreeHGlobal(pInEncryptData);
- if (szOutData == null)
- {
- return null;
- }
- return Encoding.UTF8.GetString(szOutData);
- }
- private readonly long pSafeHandle;
- /// <summary>
- /// Initializes a new instance of the <see cref="EncryptHelper"/> class.
- /// </summary>
- public EncryptHelper()
- {
- int iResult = 0;
- int iLoadTemp = 0;
- IntPtr pLoadTemp = IntPtr.Zero;
- this.pSafeHandle = MIGetSafeHandle();
- if (0 == this.pSafeHandle)
- {
- Console.Write(Client_Resource.Infrastructure_FailedToEncryptionAndDecryption);
- }
- iResult = MILoad(pLoadTemp, iLoadTemp, this.pSafeHandle);
- if (0 != iResult)
- {
- Console.Write(Client_Resource.Infrastructure_FailedToLoadKey);
- }
- }
- /// <summary>
- /// 修改加密密钥
- /// </summary>
- /// <param name="keyData">密钥</param>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
- public bool AlterKey(StringBuilder keyData)
- {
- int iResult = 0;
- iResult = MIAlterTransPwd(keyData, this.pSafeHandle);
- return iResult == 0;
- }
- /// <summary>
- /// 释放加密对象。
- /// </summary>
- public void FreeHandle()
- {
- MIFreeSafeHandle(this.pSafeHandle);
- }
- /// <summary>
- /// 加密
- /// </summary>
- /// <param name="szInStr">需加密的数据</param>
- /// <param name="szOutStr">加密后的数据</param>
- /// <returns>返回加密是否成功</returns>
- public bool Encrypt(string szInStr, out string szOutStr)
- {
- byte[] szOutData = null;
- var szInData = Encoding.UTF8.GetBytes(szInStr);
- int iResult = 0;
- ////加密后的数据长度
- int iOutEncryptDataLen = 0;
- bool iRev = false;
- ////待加密数据长度
- int iInEncryptDataLen = szInData.Length;
- ////申请内存拷贝待加密数据
- IntPtr pInEncryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iInEncryptDataLen);
- System.Runtime.InteropServices.Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
- ////获取加密后内存的长度
- iResult = MIGetEncryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, this.pSafeHandle);
- if (0 == iResult)
- {
- ////创建内存
- IntPtr pOutEncryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iOutEncryptDataLen);
- ////加密
- iResult = MITransEncrypt(pOutEncryptData, iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, this.pSafeHandle);
- if (iResult == 0)
- {
- iRev = true;
- ////拷贝到数组上面
- szOutData = new byte[iOutEncryptDataLen];
- System.Runtime.InteropServices.Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
- }
- ////释放内存
- System.Runtime.InteropServices.Marshal.FreeHGlobal(pOutEncryptData);
- }
- ////释放内存
- System.Runtime.InteropServices.Marshal.FreeHGlobal(pInEncryptData);
- if (szOutData == null)
- {
- throw new MuchinfoException(ExceptionManager.EncryptError);
- }
- szOutStr = Convert.ToBase64String(szOutData);
- return iRev;
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="szInData">需解密的数据</param>
- /// <param name="szOutData">解密后的数据</param>
- /// <returns>返回解密是否成功</returns>
- public bool Decrypt(byte[] szInData, ref byte[] szOutData)
- {
- bool iRev = false;
- int iResult = 0;
- ////解密后的数据长度
- int iOutDecryptDataLen = 0;
- ////待解密数据长度
- int iInDecryptDataLen = szInData.Length;
- ////申请内存拷贝待解密数据
- IntPtr pInDecryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iInDecryptDataLen);
- System.Runtime.InteropServices.Marshal.Copy(szInData, 0, pInDecryptData, iInDecryptDataLen);
- ////获取解密后内存的长度
- iResult = MIGetDecryptDataLen(ref iOutDecryptDataLen, pInDecryptData, iInDecryptDataLen, this.pSafeHandle);
- if (0 == iResult)
- {
- ////创建内存
- IntPtr pOutDecryptData = System.Runtime.InteropServices.Marshal.AllocHGlobal(iOutDecryptDataLen);
- ////加密
- iResult = MITransDecrypt(pOutDecryptData, iOutDecryptDataLen, pInDecryptData, iInDecryptDataLen, this.pSafeHandle);
- if (iResult == 0)
- {
- iRev = true;
- ////拷贝到数组上面
- szOutData = new byte[iOutDecryptDataLen];
- System.Runtime.InteropServices.Marshal.Copy(pOutDecryptData, szOutData, 0, iOutDecryptDataLen);
- }
- ////释放内存
- System.Runtime.InteropServices.Marshal.FreeHGlobal(pOutDecryptData);
- }
- ////释放内存
- System.Runtime.InteropServices.Marshal.FreeHGlobal(pInDecryptData);
- return iRev;
- }
- /// <summary>
- /// 解密
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public string AesDecrypt(string source)
- {
- byte[] szOutData = null;
- var szInData = Hex_16To2(source); //Convert.FromBase64String(source); // Encoding.UTF8.GetBytes(source);
- int iResult = 0;
- ////加密后的数据长度
- int iOutEncryptDataLen = 0;
- ////待加密数据长度
- int iInEncryptDataLen = szInData.Length;
- //获取加密句柄
- //long pSafeHandle = MIGetSafeHandle();
- ////申请内存拷贝待加密数据
- IntPtr pInEncryptData = Marshal.AllocHGlobal(iInEncryptDataLen);
- Marshal.Copy(szInData, 0, pInEncryptData, iInEncryptDataLen);
- // 获取加密后内存的长度
- iResult = MIGetDecryptDataLen(ref iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, pSafeHandle);
- if (0 == iResult)
- {
- ////创建内存
- IntPtr pOutEncryptData = Marshal.AllocHGlobal(iOutEncryptDataLen);
- ////加密
- iResult = MITransDecrypt(pOutEncryptData, iOutEncryptDataLen, pInEncryptData, iInEncryptDataLen, pSafeHandle);
- if (iResult == 0)
- {
- ////拷贝到数组上面
- szOutData = new byte[iOutEncryptDataLen];
- Marshal.Copy(pOutEncryptData, szOutData, 0, iOutEncryptDataLen);
- }
- ////释放内存
- Marshal.FreeHGlobal(pOutEncryptData);
- }
- ////释放内存
- Marshal.FreeHGlobal(pInEncryptData);
- if (szOutData == null)
- {
- return null;
- }
- return Encoding.UTF8.GetString(szOutData);
- // return Encoding.UTF8.GetString(szOutData, 0, 64);
- }
- /// <summary>
- /// 16进制转2进制
- /// </summary>
- public Byte[] Hex_16To2(String hexString)
- {
- if ((hexString.Length % 2) != 0)
- {
- hexString += " ";
- }
- Byte[] returnBytes = new Byte[hexString.Length / 2];
- for (Int32 i = 0; i < returnBytes.Length; i++)
- {
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
- }
- return returnBytes;
- }
- /// <summary>
- /// 2进制转16进制
- /// </summary>
- public String Hex_2To16(Byte[] bytes)
- {
- String hexString = String.Empty;
- Int32 iLength = 65535;
- if (bytes != null)
- {
- StringBuilder strB = new StringBuilder();
- if (bytes.Length < iLength)
- {
- iLength = bytes.Length;
- }
- for (int i = 0; i < iLength; i++)
- {
- strB.Append(bytes[i].ToString("x2"));
- }
- hexString = strB.ToString();
- }
- return hexString;
- }
- /// <summary>
- /// 加密密码
- /// </summary>
- /// <param name="secretKey">The secret key.</param>
- /// <param name="bankPassword">The bank password.</param>
- /// <returns>System.String.</returns>
- /// <exception cref="MuchinfoException"></exception>
- public string EncryptPassd(string secretKey, string bankPassword)
- {
- string encryptPass = string.Empty;
- var keyByte = Convert.FromBase64String(secretKey);
- byte[] decryptKey = null;
- ////使用本地密钥解密密钥
- this.Decrypt(keyByte, ref decryptKey);
- string bitStr = Encoding.UTF8.GetString(decryptKey);
- var sb = new StringBuilder(bitStr);
- ////修改密钥
- if (this.AlterKey(sb))
- {
- this.Encrypt(bankPassword, out encryptPass);
- this.FreeHandle();
- }
- else
- {
- throw new MuchinfoException(ExceptionManager.AlterKeyError);
- }
- return encryptPass;
- }
- }
- }
|