YinHaiEncrypt.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. using PTMedicalInsurance.Variables;
  5. namespace PTMedicalInsurance.Common
  6. {
  7. class YinHaiEncrypt
  8. {
  9. //加密
  10. [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_encrypt_key@16", CallingConvention = CallingConvention.StdCall)]
  11. private static extern int gm_ecb_encrypt_key(byte[] pub_key, byte[] plain, int plain_len, byte[] cipher);
  12. //解密
  13. [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_decrypt_key@16", CallingConvention = CallingConvention.StdCall)]
  14. private static extern int gm_ecb_decrypt_key(byte[] pub_key, byte[] cipher, int cipher_len, byte[] plain);
  15. //生成签名
  16. [DllImport("hsafsitool.dll", EntryPoint = "gm_sign_key@24", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
  17. private static extern int gm_sign_key(byte[] userid, byte[] prv_key, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);
  18. //验签
  19. [DllImport("hsafsitool.dll", EntryPoint = "gm_verify_key@20", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
  20. private static extern int gm_verify_key(byte[] userid, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);
  21. public const string pubKey = "725871EF03A7AA708708615A0B9400C14A9E7E4F0E5E14E911AE39CD9177C17685DED3BBDB1B242F09393BA7A0C852338A0D06D1619B79B70CF3CCD3754FF646";
  22. public const string prvKey = "BC5E29E032BF908DEB6EB180A0C61ED7F9AA7BF56C35B727A85CD65F79504384";
  23. public string Signature(string data)
  24. {
  25. string rtn = "";
  26. try
  27. {
  28. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  29. byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);
  30. byte[] userid = Encoding.UTF8.GetBytes("");
  31. byte[] msg = Encoding.UTF8.GetBytes(data);
  32. byte[] singrs = new byte[1024];
  33. if (gm_sign_key(userid, prv_key, pub_key, msg, msg.Length, singrs) > 0)
  34. {
  35. rtn = Encoding.UTF8.GetString(singrs).Replace("\u0000", "").Trim();
  36. }
  37. else
  38. {
  39. rtn = "";
  40. }
  41. return rtn;
  42. }
  43. catch (Exception ex)
  44. {
  45. rtn = "" + ex.Message;
  46. return rtn;
  47. }
  48. finally
  49. {
  50. Global.writeLog("", data, rtn);
  51. }
  52. }
  53. public int VerifySignature(string plain, string signature)
  54. {
  55. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  56. byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);
  57. byte[] userid = Encoding.UTF8.GetBytes("");
  58. byte[] msg = Encoding.UTF8.GetBytes(plain);
  59. byte[] singrs = Encoding.UTF8.GetBytes(signature);
  60. int result = gm_verify_key(userid, pub_key, msg, msg.Length, singrs);
  61. return result;
  62. }
  63. public string Encrypt(string plain)
  64. {
  65. byte[] plainB = Encoding.UTF8.GetBytes(plain);
  66. byte[] cipher = new byte[(plainB.Length + 2048)];
  67. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  68. Global.writeLog("Encrypt明文:'" + plain + "'");
  69. if (gm_ecb_encrypt_key(pub_key, plainB, plainB.Length, cipher) > 0)
  70. {
  71. string result = Encoding.UTF8.GetString(cipher).Replace("\u0000", "").Trim();
  72. Global.writeLog("Encrypt密文:" + result);
  73. return result;
  74. }
  75. else
  76. {
  77. return "";
  78. }
  79. }
  80. public string Decrypt(string cipher)
  81. {
  82. byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);
  83. byte[] cipherB = Encoding.UTF8.GetBytes(cipher);
  84. byte[] plain = new byte[(cipherB.Length)];
  85. if (gm_ecb_decrypt_key(pub_key, cipherB, cipher.Length, plain) > 0)
  86. {
  87. return Encoding.UTF8.GetString(plain);
  88. }
  89. else
  90. {
  91. return "";
  92. }
  93. }
  94. }
  95. }