示例#1
0
        public static String CSMMAC(String key, String data)
        {
            if (key.Length != 32)
            {
                new Exception("key's length must be 16!");
                return(null);
            }

            StringBuilder sb  = new StringBuilder(data);
            int           mod = data.Length % 32;

            if (mod != 0)
            {
                for (int i = 0; i < 32 - mod; i++)
                {
                    sb.Append("0");
                }
            }

            String oper = sb.ToString();

            int count = oper.Length / 32;

            String[] blocks = new String[count];

            for (int i = 0; i < count; i++)
            {
                blocks[i] = oper.Substring(i * 32, 32);
            }
            String vector = blocks[0];

            //循环进行异或
            for (int i = 1; i < count; i++)
            {
                vector = xOrString(vector, blocks[i]);
            }
            Console.WriteLine("vector:" + vector);
            //DES加密
            String left = ByteUtil.getHexStr(Encoding.Default.GetBytes(vector.Substring(0, 16)));

            Console.WriteLine("left:" + left);
            String right = ByteUtil.getHexStr(Encoding.Default.GetBytes(vector.Substring(16)));

            Console.WriteLine("right:" + right);
            left = BCDUtil.byteArrToString(CSm4.EncriptB(BCDUtil.HexStrToByteArray(key), BCDUtil.HexStrToByteArray(left)));
            Console.WriteLine("加密left:" + left);
            right = xOrString(left, right);
            Console.WriteLine("加密后异或:" + right);
            right = BCDUtil.byteArrToString(CSm4.EncriptB(BCDUtil.HexStrToByteArray(key), BCDUtil.HexStrToByteArray(right)));
            Console.WriteLine("异或后加密:" + right);
            return(right);
        }
示例#2
0
        /// <summary>
        /// 验证数据是否通过验证
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        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("");
        }
示例#3
0
        /// <summary>
        /// 换算PIN
        /// </summary>
        /// <param name="pwd">用户输入的密码</param>
        /// <param name="cardNo">获取的ic卡卡号</param>
        /// <returns></returns>
        public static string getPin(string pwd, string cardNo)
        {
            string result = string.Empty;
            int    len    = cardNo.Length - 4;
            string pwdLen = pwd.Length + "";

            if (pwdLen.Length == 1)
            {
                pwd = "0" + pwdLen + pwd;
            }
            string cardNoCover = "";

            if (len < 12)
            {
                cardNo = cardNo.Substring(4, len);
                for (int i = 0; i < 12 - len; i++)
                {
                    cardNoCover += "f";
                }
            }
            else
            {
                cardNo = cardNo.Substring(3, 12);
            }
            cardNo = cardNoCover + cardNo;
            for (int i = 0; i < 32; i++)
            {
                if (cardNo.Length < 32)
                {
                    cardNo = "0" + cardNo;
                }
                if (pwd.Length < 32)
                {
                    pwd = pwd + "f";
                }
            }

            string orx = MacUtils.xOrString(cardNo, pwd);

            //Console.WriteLine("orx="+ orx);
            result = BCDUtil.byteArrToString(CSm4.EncriptB(BCDUtil.ToByteArray(pinKey), BCDUtil.ToByteArray(orx)));
            // Console.WriteLine("result="+ result);
            Console.WriteLine(result.ToUpper());
            return(result.ToUpper());
        }