Example #1
0
        public static Stream ReadBinary(SmartCard card, int fileId, int offset, int expectedLength)
        {
            if ((fileId < 0) || (fileId > 0x1F))
            {
                throw new ArgumentOutOfRangeException("id");
            }
            if ((offset < 0) || (offset > 0xFF))
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            Command cmd = new Command();

            cmd.Instruction    = 0xB0;
            cmd.Parameter1     = (byte)((1 << 7) | (fileId & 0x1F));
            cmd.Parameter2     = (byte)offset;
            cmd.ResponseLength = (expectedLength <= 0) ? 0xFF : expectedLength;
            byte[] command = cmd.ToArray();
            byte[] buffer  = new byte[((expectedLength <= 0) ? 256 : expectedLength) + 2];
            //Console.Write("Sending command {0}...", BitConverter.ToString(command));
            int len = card.Transmit(command, buffer);
            //Console.WriteLine(BitConverter.ToString(buffer, 0, len));
            CommandStatus status = GetStatus(buffer, len);

            if (!status.IsNormal())
            {
                throw status.ToException();
            }
            Stream result = new MemoryStream();

            result.Write(buffer, 0, len - 2);
            while (status.Sw1 == 0x61)
            {
                if (buffer.Length < status.Sw2 + 2)
                {
                    buffer = new byte[status.Sw2 + 2];
                }
                cmd                = new Command();
                cmd.Instruction    = 0xC0;
                cmd.ResponseLength = status.Sw2;
                command            = cmd.ToArray();
                len                = card.Transmit(command, buffer);
                status             = GetStatus(buffer, len);
                if (!status.IsNormal())
                {
                    throw status.ToException();
                }
                result.Write(buffer, 0, len - 2);
            }
            result.Position = 0;
            return(result);
        }
Example #2
0
        public static Stream ReadBinary(SmartCard card, int offset, int expectedLength)
        {
            if ((offset < 0) || (offset > 0x7FFF))
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            Command cmd = new Command();

            cmd.Instruction    = 0xB0;
            cmd.Parameter1     = (byte)((offset >> 8) & 0x7F);
            cmd.Parameter2     = (byte)offset;
            cmd.ResponseLength = expectedLength;
            byte[] command = cmd.ToArray();
            byte[] buffer  = new byte[expectedLength + 2];
            //Console.Write("Sending command {0}...", BitConverter.ToString(command));
            int len = card.Transmit(command, buffer);
            //Console.WriteLine(BitConverter.ToString(buffer, 0, len));
            CommandStatus status = GetStatus(buffer, len);

            if (!status.IsNormal())
            {
                throw status.ToException();
            }
            Stream result = new MemoryStream();

            result.Write(buffer, 0, len - 2);
            while (status.Sw1 == 0x61)
            {
                throw new NotImplementedException("Reading of remaining length not implemented");
            }
            result.Position = 0;
            return(result);
        }
Example #3
0
        public static Stream Select(SmartCard card, SelectType selectType, byte[] file, SelectFileOccurrence fileOccurrence, SelectFileControlInformation fileControlInformation, int expectedLength)
        {
            Command cmd = new Command();

            cmd.Instruction    = 0xA4;
            cmd.Parameter1     = (byte)selectType;
            cmd.Parameter2     = (byte)((byte)fileOccurrence | (byte)fileControlInformation);
            cmd.Data           = file;
            cmd.ResponseLength = expectedLength;
            byte[] command = cmd.ToArray();
            byte[] buffer  = new byte[256];
            //Console.Write("Sending command {0}...", BitConverter.ToString(command));
            int len = card.Transmit(command, buffer);
            //Console.WriteLine(BitConverter.ToString(buffer, 0, len));
            CommandStatus status = GetStatus(buffer, len);

            if (!status.IsNormal())
            {
                throw status.ToException();
            }
            Stream result = new MemoryStream();

            result.Write(buffer, 0, len - 2);
            while (status.Sw1 == 0x61)
            {
                throw new NotImplementedException("Reading of remaining length not implemented");
            }
            return(result);
        }