首页 > 解决方案 > SW=6985 读取电子护照(DG5,DG7)中间出错

问题描述

我正在开发用于读取 Epassport 数据的 Xamarin.Android 应用程序并遇到问题:通常当我在读取 Epassport 的过程中使用安全消息大数据组(DG5、DG7)进行读取时,会给出 RAPDU = 6985(0x6985、0x69 0x85),但是有时一切正常(约 65% 的情况)。

我试过写 isoDep.SetTimeout(2000); 但这并没有帮助,SSC 在 VerificationMessage 和 SendSecureMessageAPDU 中总是正确递增,KS_Enc 和 KS_MAC 不会改变,我不知道为什么 Epassport 给我 6985,帮帮我

    // Read first 4 bytes for FileLength
     
    byte[] Header4, Header5;
            DO97 = new byte[] { 0x97, 0x01, 0x80 }; // 0x80 = 126
            byte i = 0;



            //RAPDU length of data Epassport = 255 but need 256, so 256 = 128  * 2;
            // Problem arises here in WHILE
            while (i != FileLength[0] && FileLength.Length >= 2) // Need for big files (DG5 photo /DG7 signature) 
            {
                //Read first 128 bytes
               Header4 = new byte[] { 0x0C, 0xB0, i, 0x04, 0x80, 0x00, 0x00, 0x00 }; // offset 0x04 because first 4 bytes for lenght 
                var result4 = SendSecureMessageAPDU(Header4, DO97);  // ERROR HERE 
                VerificationRAPDU(result4);
                Data_DG = Data_DG.Concat(TripleDES(TripleDESMode.Decoding, result4, KS_Enc)).SkipLast(24).ToArray();

                //Read last 128 bytes
               Header5 = new byte[] { 0x0C, 0xB0, i, 0x84, 0x80, 0x00, 0x00, 0x00 };             
                var result5 = SendSecureMessageAPDU(Header5, DO97); // OR ERROR HERE !!!!!
                VerificationRAPDU(result5);
                Data_DG = Data_DG.Concat(TripleDES(TripleDESMode.Decoding, result5, KS_Enc)).SkipLast(24).ToArray();

                i++;
            }

            // For short file (Length <255 bytes) and last block bigfile ( where length also <255 bytes) 
            byte[] Header6 = new byte[] { 0x0C, 0xB0, i, 0x04, 0x80, 0x00, 0x00, 0x00 };

            // Header6 = new byte[] { 0x0C, 0xB0, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00 };

            if (FileLength.Length > 1)
            {
                DO97 = new byte[] { 0x97, 0x01, FileLength[1] };
            }
            else
            {
                DO97 = new byte[] { 0x97, 0x01, FileLength[0] };
            }

            var result6 = SendSecureMessageAPDU(Header6, DO97);
            VerificationRAPDU(result6);

            Data_DG = Data_DG.Concat(TripleDES(TripleDESMode.Decoding, result6, KS_Enc)).SkipLast(16).ToArray();
            string testDG = BitConverter.ToString(Data_DG).Replace("-", "");

            var asddas = Encoding.UTF8.GetString(Data_DG);

            return Data_DG;
        }
 public byte[] SendSecureMessageAPDU(byte[] Header, byte[] buildDO)
        {
            byte[] M = Header.Concat(buildDO).ToArray();

            // Вычисление MAC от M:
            string sM = BitConverter.ToString(M);

            if (SSC[SSC.Length - 2] == 255)
            {
                SSC[SSC.Length - 3]++;
                SSC[SSC.Length - 2]++;
                SSC[SSC.Length - 1]++;
            }
            else if (SSC[SSC.Length - 1] == 255)
            {
                SSC[SSC.Length - 2]++;
                SSC[SSC.Length - 1]++;
            }
            else
                SSC[SSC.Length - 1]++;

            byte[] N = SSC.Concat(Header)
                        .Concat(buildDO)  
                        .ToArray();

            byte[] CC = MAC(N, KS_MAC);
           
            byte[] DO8E = new byte[] { 0x8E, 0x08, };
            DO8E = DO8E.Concat(CC).ToArray();

            string sD08E = BitConverter.ToString(DO8E);

            // build secure command
            var command = Header.Take(4).ToArray()
                           .Concat(new byte[] { (byte)(buildDO.Length + DO8E.Length) }) // Length
                           .Concat(buildDO)
                           .Concat(DO8E)
                           .Concat(new byte[] { 0x00 })
                           .ToArray();

            var result = isoDep.Transceive(command).ToArray(); // Error RAPDU = 6985

            if (result.Length == 2)  
            {               
                throw new RAPDU_Exception("Error RAPDU ePassport");
            }

            return result;
        }

标签: xamarinnfcapducontactless-smartcard

解决方案


推荐阅读