Example #1
0
        /// <summary>
        /// 计算mac 公共类
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] calculateMac(Dictionary <int, string> data, string msgType)
        {
            //计算MAC用到的数据
            byte[] countMacData = EncryptWT.countMacData(msgType, EncryptWT.encryptLKL(data, null));
            Console.WriteLine(BCDUtil.byteArrToString(countMacData));
            string str = MacUtils.CSMMAC(mackey, BCDUtil.byteArrToString(countMacData));//ConfigurationManager.AppSettings["WTMacKey"]

            Console.WriteLine("bcd2Str:" + PosProtocol.bcd2Str(System.Text.Encoding.Default.GetBytes(str.Substring(0, 8))));
            //MAC转byte[]
            String strMac = PosProtocol.bcd2Str(System.Text.Encoding.Default.GetBytes(str.Substring(0, 8).ToUpper()));

            byte[] macByte = BCDUtil.ToByteArray(strMac);
            return(macByte);
        }
Example #2
0
        /// <summary>
        /// 验证数据是否通过验证
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string validateKey(string data, string key)
        {
            string validata = data.Substring(data.Length - 8, 8).ToUpper();

            byte[] sources   = BCDUtil.HexStrToByteArray(data.Substring(0, 32));
            byte[] desKeys   = BCDUtil.HexStrToByteArray(key);
            byte[] encSource = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            byte[] encKeys   = CSm4.DecriptB(desKeys, sources);
            if (PosProtocol.bytesToHexString(CSm4.EncriptB(encKeys, encSource)).Substring(0, 8).Equals(validata))
            {
                Console.WriteLine("密钥验证通过返回灌输密钥!!!!");
                return(PosProtocol.bytesToHexString(encKeys));
            }
            return("");
        }