Exemple #1
0
        /// <summary>
        /// 外部身份验证
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public ApduMsg Auth(byte[] Key)
        {
            if (Key.Length != 8)
            {
                return(new ApduMsg("身份验证Key长度不正确"));
            }
            string RANDOMCMD = "0084000004";


            byte[] Random = SendStrCommand(RANDOMCMD);


            if (Random.Length != 6)
            {
                return(new ApduMsg("返回随机数长度不正确"));
            }

            byte[] Randomdata = new byte[8];

            Array.Copy(Random, 0, Randomdata, 0, 4);

            List <byte> cmdList = new List <byte>();

            cmdList.AddRange(CPUCardHelper.ConverToBytes("00 82 00 00 08"));
            cmdList.AddRange(CPUCardHelper.Encrypt(Randomdata, Key));

            byte[] data = CardSendCommand(cmdList.ToArray());

            return(ApduMsg.GetApduByData(data));
        }
Exemple #2
0
        /// <summary>
        /// 根据文件名称选择
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public ApduMsg SelectFileName(byte[] fileName)
        {
            //00A40400 09 A00000000386980701
            byte[] dd = CPUCardHelper.ConverToBytes("00A40400");


            List <byte> result = new List <byte>();

            result.AddRange(dd);
            result.Add((byte)fileName.Length);
            result.AddRange(fileName);
            byte[] cmd  = result.ToArray();
            byte[] data = CardSendCommand(cmd);
            return(ApduMsg.GetApduByData(data));
        }
Exemple #3
0
        /// <summary>
        /// 创建文件
        /// </summary>
        /// <param name="fileIDHex">文件标识符2字节</param>
        /// <param name="fileType">枚举文件类型</param>
        /// <param name="fileLenght">文件长度</param>
        /// <param name="fileNameHex">文件名称,十六进制</param>
        /// <param name="FileAccess"></param>
        /// <returns></returns>
        public ApduMsg CreateFile(ushort fileID, CPUFileType fileType, int fileLenght, string FileAccess = "0000")
        {
            //Demo
            //80E0-EF01-07-28-002A-F00E-FF-80
            //EF01 文件标识
            //07 LC data 长度(07)
            //28 文件类型(二进制)
            //002A 文件长度
            //F00E 读写权限
            //FF 保留
            //80 不支持线路保护

            ApduCommand cmd = new ApduCommand();

            cmd.CLA = 0x80;
            cmd.INS = 0xE0;
            cmd.SetP1P2(fileID);

            //构造一个data  ,长度,权限,
            List <byte> data = new List <byte>();

            //文件类型
            data.Add((byte)fileType);
            //文件长度
            data.AddRange(CPUCardHelper.IntConvertTo2Byte(fileLenght));
            //添加读写权限
            data.AddRange(CPUCardHelper.ConverToBytes(FileAccess));
            //添加文件名
            data.AddRange(CPUCardHelper.ConverToBytes("FFFF"));

            //不支持线路保护
            data.Add(0x80);

            cmd.Data = data.ToArray();

            ApduMsg msg = ApduMsg.GetApduByData(SendStrCommand(cmd));

            if (msg.Code == "6A-86")
            {
                msg.Msg += "(文件已存在)";
            }

            return(msg);
        }
Exemple #4
0
        /// <summary>
        /// 设置P1P2,(字符串类型,比如设置文件标识符)
        /// </summary>
        /// <param name="Hexvalue"></param>
        public bool SetP1P2(string Hexvalue)
        {
            try
            {
                byte[] data = CPUCardHelper.ConverToBytes(Hexvalue);
                P1 = data[0];
                P2 = data[1];

                if (data.Length != 2)
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemple #5
0
        public ApduMsg SelectDFFile(ushort fileId)
        {
            //00-A4-04-00-03-44-46-31-00
            string      cmd      = "00A40400";
            List <byte> listdata = CPUCardHelper.ConverToBytes(cmd).ToList();

            byte[] data = GetDFNameByID(fileId);
            byte   LC   = (byte)(data.Length + 1);

            listdata.Add(LC);
            listdata.AddRange(data);
            listdata.Add(0);

            //ApduCommand adpCMd = new ApduCommand(listdata.ToArray());
            ////adpCMd.LE = 0;



            byte[] Resdata = CardSendCommand(listdata.ToArray());
            return(ApduMsg.GetApduByData(Resdata));
        }
Exemple #6
0
 /// <summary>
 /// 身份验证
 /// </summary>
 /// <param name="Key"></param>
 /// <returns></returns>
 public ApduMsg Auth(string Key)
 {
     return(Auth(CPUCardHelper.ConverToBytes(Key)));
 }
Exemple #7
0
 /// <summary>
 /// 发送十六进制字符串
 /// </summary>
 /// <param name="str"></param>
 /// <returns></returns>
 public byte[] SendStrCommand(string str)
 {
     return(CardSendCommand(CPUCardHelper.ConverToBytes(str)));
 }
Exemple #8
0
 public void SetData(string hexValue)
 {
     Data = CPUCardHelper.ConverToBytes(hexValue);
 }