Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using PCSC;
  8.  
  9. namespace ConsoleApplication1
  10. {
  11.     class Program
  12.     {
  13.  
  14.  
  15.         private static SCardError err;
  16.         private static SCardReader reader;
  17.         private static System.IntPtr protocol;
  18.         private static SCardContext hContext;
  19.         static void Main(string[] args)
  20.         {
  21.             try
  22.             {
  23.                 connect();
  24.  
  25.                 byte[] commandBytes = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10 };
  26.                 sendCommand(commandBytes, "SELECT TELECOM");
  27.  
  28.                 // SELECT TELECOM
  29.                 commandBytes = new byte[] {0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10};
  30.                 sendCommand(commandBytes, "SELECT TELECOM");
  31.  
  32.                 // GET RESPONSE
  33.                 commandBytes = new byte[] {0xA0, 0xC0, 0x00, 0x00, 0x16};
  34.                 sendCommand(commandBytes, "GET RESPONSE");
  35.  
  36.                 // SELECT SMS
  37.                 commandBytes = new byte[] {0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6F, 0x3C};
  38.                 sendCommand(commandBytes, "SELECT SMS");
  39.  
  40.                 // GET RESPONSE
  41.                 commandBytes = new byte[] {0xA0, 0xC0, 0x00, 0x00, 0x0F};
  42.                 sendCommand(commandBytes, "GET RESPONSE");
  43.  
  44.                 // READ RECORD //3 numer rekordu, 4 mode
  45.                 commandBytes = new byte[] {0xA0, 0xB2, 0x01, 0x04, 0xB0};
  46.                 sendCommand(commandBytes, "READ RECORD");
  47.  
  48.                 hContext.Release();
  49.             }
  50.             catch (PCSCException ex)
  51.             {
  52.                 Console.WriteLine("Blad: " + ex.Message + " (" + ex.SCardError.ToString() + ")");
  53.             }
  54.             catch (Exception e)
  55.             {
  56.                 Console.WriteLine("nieznany blad");
  57.             }
  58.             Console.ReadLine();
  59.         }
  60.  
  61.         public static void toTelecom()
  62.         {
  63.              byte[] commandBytes = new byte[] {0xA0, 0xA4, 0x00, 0x00, 0x02, 0x7F, 0x10};
  64.                 sendCommand(commandBytes, "SELECT TELECOM");
  65.  
  66.                 // GET RESPONSE
  67.                 commandBytes = new byte[] {0xA0, 0xC0, 0x00, 0x00, 0x16};
  68.                 sendCommand(commandBytes, "GET RESPONSE");
  69.  
  70.                 // SELECT SMS
  71.                 commandBytes = new byte[] {0xA0, 0xA4, 0x00, 0x00, 0x02, 0x6F, 0x3C};
  72.                 sendCommand(commandBytes, "SELECT SMS");
  73.  
  74.                 // GET RESPONSE
  75.                 commandBytes = new byte[] {0xA0, 0xC0, 0x00, 0x00, 0x0F};
  76.                 sendCommand(commandBytes, "GET RESPONSE");
  77.  
  78.         }
  79.  
  80.         public static void connect()
  81.         {
  82.             hContext = new SCardContext();
  83.             hContext.Establish(SCardScope.System);
  84.  
  85.             string[] readerList = hContext.GetReaders();
  86.             Boolean noReaders = readerList.Length <= 0;
  87.             if (noReaders)
  88.             {
  89.                 throw new PCSCException(SCardError.NoReadersAvailable, "Blad czytnika");
  90.             }
  91.  
  92.             Console.WriteLine("Nazwa czytnika: " + readerList[0]);
  93.  
  94.             reader = new SCardReader(hContext);
  95.  
  96.             err = reader.Connect(readerList[0],
  97.                 SCardShareMode.Shared,
  98.                 SCardProtocol.T0 | SCardProtocol.T1);
  99.             checkError(err);
  100.  
  101.             switch (reader.ActiveProtocol)
  102.             {
  103.                 case SCardProtocol.T0:
  104.                     protocol = SCardPCI.T0;
  105.                     break;
  106.                 case SCardProtocol.T1:
  107.                     protocol = SCardPCI.T1;
  108.                     break;
  109.                 default:
  110.                     throw new PCSCException(SCardError.ProtocolMismatch, "nieobslugiwany protokol: "+ reader.ActiveProtocol.ToString());
  111.             }
  112.         }
  113.  
  114.         public static void sendCommand(byte[] comand, String name)
  115.         {
  116.             byte[] recivedBytes = new byte[256];
  117.             err = reader.Transmit(protocol, comand, ref recivedBytes);
  118.             checkError(err);
  119.             writeResponse(recivedBytes, name);
  120.         }
  121.  
  122.         public static void writeResponse(byte[] recivedBytes, String responseCode)
  123.         {
  124.                             Console.Write(responseCode+ ": ");
  125.                             for (int i = 0; i < recivedBytes.Length; i++)
  126.                                 Console.Write("{0:X2} ", recivedBytes[i]);
  127.             Console.WriteLine();
  128.         }
  129.  
  130.         static void checkError(SCardError err)
  131.         {
  132.             if (err != SCardError.Success)
  133.             {
  134.                 throw new PCSCException(err, SCardHelper.StringifyError(err));
  135.             }
  136.         }
  137.  
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement