| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | using System;using System.Runtime.InteropServices;using System.Text;using PTMedicalInsurance.Variables;namespace PTMedicalInsurance.Common{    class YinHaiEncrypt    {        //加密        [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_encrypt_key@16", CallingConvention = CallingConvention.StdCall)]        private static extern int gm_ecb_encrypt_key(byte[] pub_key, byte[] plain, int plain_len, byte[] cipher);        //解密        [DllImport("hsafsiyhsafe.dll", CharSet = CharSet.Ansi, EntryPoint = "gm_ecb_decrypt_key@16", CallingConvention = CallingConvention.StdCall)]        private static extern int gm_ecb_decrypt_key(byte[] pub_key, byte[] cipher, int cipher_len, byte[] plain);        //生成签名        [DllImport("hsafsitool.dll", EntryPoint = "gm_sign_key@24", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]        private static extern int gm_sign_key(byte[] userid, byte[] prv_key, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);        //验签        [DllImport("hsafsitool.dll", EntryPoint = "gm_verify_key@20", CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]        private static extern int gm_verify_key(byte[] userid, byte[] pub_key, byte[] msg, int msg_len, byte[] singrs);        public const string pubKey = "725871EF03A7AA708708615A0B9400C14A9E7E4F0E5E14E911AE39CD9177C17685DED3BBDB1B242F09393BA7A0C852338A0D06D1619B79B70CF3CCD3754FF646";        public const string prvKey = "BC5E29E032BF908DEB6EB180A0C61ED7F9AA7BF56C35B727A85CD65F79504384";        public string Signature(string data)        {            string rtn = "";            try            {                byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);                byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);                byte[] userid = Encoding.UTF8.GetBytes("");                byte[] msg = Encoding.UTF8.GetBytes(data);                byte[] singrs = new byte[1024];                if (gm_sign_key(userid, prv_key, pub_key, msg, msg.Length, singrs) > 0)                {                    rtn = Encoding.UTF8.GetString(singrs).Replace("\u0000", "").Trim();                }                else                {                    rtn = "";                }                return rtn;            }            catch (Exception ex)            {                rtn = "" + ex.Message;                return rtn;            }            finally            {                Global.writeLog("", data, rtn);            }        }        public int VerifySignature(string plain, string signature)        {            byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);            byte[] prv_key = Encoding.UTF8.GetBytes(prvKey);            byte[] userid = Encoding.UTF8.GetBytes("");            byte[] msg = Encoding.UTF8.GetBytes(plain);            byte[] singrs = Encoding.UTF8.GetBytes(signature);            int result = gm_verify_key(userid, pub_key, msg, msg.Length, singrs);            return result;        }        public string Encrypt(string plain)        {            byte[] plainB = Encoding.UTF8.GetBytes(plain);            byte[] cipher = new byte[(plainB.Length + 2048)];            byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);            Global.writeLog("Encrypt明文:'" + plain + "'");            if (gm_ecb_encrypt_key(pub_key, plainB, plainB.Length, cipher) > 0)            {                string result = Encoding.UTF8.GetString(cipher).Replace("\u0000", "").Trim();                Global.writeLog("Encrypt密文:" + result);                return result;            }            else            {                return "";            }        }        public string Decrypt(string cipher)        {            byte[] pub_key = Encoding.UTF8.GetBytes(pubKey);            byte[] cipherB = Encoding.UTF8.GetBytes(cipher);            byte[] plain = new byte[(cipherB.Length)];            if (gm_ecb_decrypt_key(pub_key, cipherB, cipher.Length, plain) > 0)            {                return Encoding.UTF8.GetString(plain);            }            else            {                return "";            }        }    }}
 |