static void Main(string[] args) { string sourcePath = @"D:\Temp45\1-31973.MRC"; string outputPath = @"D:\Temp45\1-31973.txt"; int counter = 0; using (IsoReader reader = new IsoReader(sourcePath, IrbisEncoding.Oem)) using (TextWriter writer = new StreamWriter(outputPath, false, Encoding.UTF8)) { foreach (MarcRecord record in reader) { if (record == null) { continue; } PlainText.WriteRecord(writer, record); Console.Write('.'); counter++; } } Console.WriteLine(); Console.WriteLine(counter); Console.WriteLine(); }
public void UpdateBinary(byte msb, byte lsb, byte[] data) { var isoReader = new IsoReader( ContextFactory.Instance.Establish(SCardScope.System), _reader, SCardShareMode.Shared, SCardProtocol.Any, false); var updateBinaryCmd = new CommandApdu(IsoCase.Case3Short, SCardProtocol.Any) { CLA = CustomCla, Instruction = InstructionCode.UpdateBinary, P1 = msb, P2 = lsb, Data = data }; //Debug.WriteLine($"Update Binary: {BitConverter.ToString(updateBinaryCmd.ToArray())}"); var response = isoReader.Transmit(updateBinaryCmd); //Debug.WriteLine($"SW1 SW2 = {response.SW1:X2} {response.SW2:X2}"); if ((response.SW1 == (byte)SW1Code.Normal) && (response.SW2 == 0x00)) { return; } else { throw new InvalidOperationException("Update binary failed"); } }
/// <summary> /// Constractor for the SmartApplication object /// </summary> /// <param name="selectresponce">The reponce of the card following the SELECT application command </param> /// <param name="reader">The IsoReader communicating with the application/card </param> public SmartApplication(byte[] selectresponce, IsoReader reader) { _reader = reader; RES_SELECT = selectresponce; TLV_SELECT = new SmartTlv(selectresponce); AID = TLV_SELECT.TagList.Single(t => t.TagStringName == "84").TagValue.ToArray(); }
private void WriteAllCardBytes(IsoReader isoReader, byte[] bytes, int packetSize) { var bytesToWrite = new List <byte>(bytes); //while (bytesToWrite.Count < 38 * 4) // bytesToWrite.Add(0x00); while (bytesToWrite.Count % packetSize != 0) { bytesToWrite.Add(0x00); } for (int i = 0; i < bytesToWrite.Count / packetSize; i++) { var updateBinaryCmd = new CommandApdu(IsoCase.Case3Short, SCardProtocol.Any) { CLA = 0xFF, Instruction = InstructionCode.UpdateBinary, P1 = 0x00, P2 = (byte)((16 / packetSize) + i), Data = bytesToWrite.Skip(i * packetSize).Take(packetSize).ToArray() }; var response = isoReader.Transmit(updateBinaryCmd); Console.WriteLine("UpdateBinary: {0},{1}", response.SW1, response.SW2); } }
private IsoReader GetReader(ref bool iSSecureElement) { IsoReader reader; // Establish SCard context SCardContext hContext = new SCardContext(); hContext.Establish(SCardScope.System); // Retrieve the list of Smartcard readers string[] szReaders = hContext.GetReaders(); // Create a reader object using the existing context reader = new IsoReader(hContext); // Connect to the card foreach (var sZReader in szReaders) { reader.Connect(sZReader, SCardShareMode.Shared, SCardProtocol.T0 | SCardProtocol.T1); var response = reader.Transmit(GetSelectAppBytes(reader)); if (response.SW1 == 0x90) { iSSecureElement = true; break; } } return(reader); }
private void Monitor_CardInserted(object sender, CardStatusEventArgs e) { var contextFactory = ContextFactory.Instance; try { using (var ctx = contextFactory.Establish(SCardScope.System)) { using (var isoReader = new IsoReader(ctx, monitor.ReaderNames[0], SCardShareMode.Shared, SCardProtocol.Any, false)) { var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) { CLA = 0xFF, Instruction = (InstructionCode)0xCA, P1 = 0x00, P2 = 0x00, Le = 0x00 }; var response = isoReader.Transmit(apdu); var data = response.GetData(); UidReceived(this, new CardUidReceivedEventArgs(data)); LogAction?.Invoke("SCardWatcher Received : " + BitConverter.ToString(data)); } } } catch (Exception ex) { LogAction?.Invoke("SCardWatcher Error - " + ex); } }
public APDUResponse SendAPDU(string reader, APDURequest request) { using (var context = new SCardContext()) { context.Establish(SCardScope.System); using (var isoReader = new IsoReader(context, reader, SCardShareMode.Shared, SCardProtocol.Any)) { var apdu = new CommandApdu(IsoCase.Case3Short, isoReader.ActiveProtocol) { CLA = request.CLA, // Class INS = request.INS, //Instruction P1 = request.P1, // Parameter 1 P2 = request.P2, // Parameter 2 Data = request.Data }; byte[] id = null; var response = isoReader.Transmit(apdu); return(new APDUResponse() { Data = response.GetData(), SW1 = response.SW1, SW2 = response.SW2 }); }; } }
private void CreateContextAndReader() { context = new SCardContext(); context.Establish(scope); context.EnsureOK(); reader = new IsoReader(context); }
private static void Main() { var contextFactory = ContextFactory.Instance; using (var ctx = contextFactory.Establish(SCardScope.System)) { var readerNames = ctx.GetReaders(); if (NoReaderFound(readerNames)) { Console.WriteLine("You need at least one reader in order to run this example."); Console.ReadKey(); return; } var name = ChooseReader(readerNames); if (name == null) { return; } using (var isoReader = new IsoReader( context: ctx, readerName: name, mode: SCardShareMode.Shared, protocol: SCardProtocol.Any, releaseContextOnDispose: false)) { // Build a GET CHALLENGE command var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) { CLA = 0x00, // Class Instruction = InstructionCode.GetChallenge, P1 = 0x00, // Parameter 1 P2 = 0x00, // Parameter 2 Le = 0x08 // Expected length of the returned data }; Console.WriteLine("Send APDU with \"GET CHALLENGE\" command: {0}", BitConverter.ToString(apdu.ToArray())); var response = isoReader.Transmit(apdu); Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", response.SW1, response.SW2); if (!response.HasData) { Console.WriteLine("No data. (Card does not understand \"GET CHALLENGE\")"); } else { var data = response.GetData(); Console.WriteLine("Challenge: {0}", BitConverter.ToString(data)); } } } Console.ReadKey(); }
public bool FileExist(string path) { using (var fs = new FileStream(IsoFileInfo.FullName, FileMode.Open, FileAccess.Read)) { IsoReader reader = new IsoReader(); reader.Parse(fs); var ret = DirectoryRecord.GetAllRecords(reader.WorkPvd.RootDir); return(ret.Any(x => x.IsFile && (x.FullPath.ToLower() == path.ToLower() || Path.Combine(mountInfo.MountTarget.FullName, x.FullPath).ToLower() == path.ToLower()))); } }
private CommandApdu GetSelectAppBytes(IsoReader isoReader) { return(new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol) { CLA = 0x00, Instruction = InstructionCode.SelectFile, P1 = 0x04, P2 = 0x00, Data = new byte[] { 0xA0, 0x00, 0x00, 0x07, 0x48, 0x46, 0x4A, 0x49, 0x2D, 0x54, 0x61, 0x78, 0x43, 0x6F, 0x72, 0x65 } }); }
public CommandApdu SignInvoiceCommand(IsoReader reader, byte[] data) { return(new CommandApdu(IsoCase.Case4Extended, reader.ActiveProtocol) { CLA = 0x88, INS = 0x13, P1 = 0x04, P2 = 0x00, Data = data }); }
public CommandApdu PINVerify(IsoReader reader) { return(new CommandApdu(IsoCase.Case3Short, reader.ActiveProtocol) { CLA = 0x88, INS = 0x11, P1 = 0x04, P2 = 0x00, Data = GetIntArray(Properties.Settings.Default.PIN) }); }
public SmartCard(string readerName) { var contextFactory = ContextFactory.Instance; _context = contextFactory.Establish(SCardScope.System); if (string.IsNullOrEmpty(readerName)) { throw new ApplicationException("No smartCard readers found"); } reader = new IsoReader(_context, readerName, SCardShareMode.Exclusive, SCardProtocol.Any, true); }
static void Main(string[] args) { Console.OutputEncoding = Encoding.Unicode; using (var ctx = ContextFactory.Instance.Establish(SCardScope.User)) { var firstReader = ctx .GetReaders() .FirstOrDefault(); if (firstReader == null) { Console.WriteLine("No reader connected."); return; } using (var isoReader = new IsoReader(context: ctx, readerName: firstReader, mode: SCardShareMode.Shared, protocol: SCardProtocol.Any)) { var selectApdu = new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol) { CLA = 0x00, INS = 0xA4, P1 = 0x04, P2 = 0x00, Data = new byte[] { 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }, Le = 0x00 }; Console.WriteLine("Send Select APDU command: \r\n{0}", BitConverter.ToString(selectApdu.ToArray())); var selectResponse = isoReader.Transmit(selectApdu); Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", selectResponse.SW1, selectResponse.SW2); var readProfileApdu = new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol) { CLA = 0x00, INS = 0xCA, P1 = 0x11, P2 = 0x00, Data = new byte[] { 0x00, 0x00 }, Le = 0x00 }; Console.WriteLine("Send Read Profile APDU command: \r\n{0}", BitConverter.ToString(readProfileApdu.ToArray())); var profileResponse = isoReader.Transmit(readProfileApdu); Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", profileResponse.SW1, profileResponse.SW2); if (profileResponse.HasData) { var data = profileResponse.GetData(); var cardNumber = GetCardNumber(data[..12]);
private void submit_Click(object sender, RoutedEventArgs e) { try { using (var ctx = ContextFactory.Instance.Establish(SCardScope.User)) { var firstReader = ctx .GetReaders() .FirstOrDefault(); if (firstReader == null) { System.Diagnostics.Debug.WriteLine("No reader connected."); return; } using (var isoReader = new IsoReader(context: ctx, readerName: firstReader, mode: SCardShareMode.Shared, protocol: SCardProtocol.Any)) { var selectApdu = new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol) { CLA = 0x00, INS = 0xA4, P1 = 0x04, P2 = 0x00, Data = new byte[] { 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }, Le = 0x00 }; System.Diagnostics.Debug.WriteLine("Send Select APDU command: \r\n{0}", BitConverter.ToString(selectApdu.ToArray())); var selectResponse = isoReader.Transmit(selectApdu); System.Diagnostics.Debug.WriteLine("SW1 SW2 = {0:X2} {1:X2}", selectResponse.SW1, selectResponse.SW2); var readProfileApdu = new CommandApdu(IsoCase.Case4Short, isoReader.ActiveProtocol) { CLA = 0x00, INS = 0xCA, P1 = 0x11, P2 = 0x00, Data = new byte[] { 0x00, 0x00 }, Le = 0x00 }; System.Diagnostics.Debug.WriteLine("Send Read Profile APDU command: \r\n{0}", BitConverter.ToString(readProfileApdu.ToArray())); var profileResponse = isoReader.Transmit(readProfileApdu); System.Diagnostics.Debug.WriteLine("SW1 SW2 = {0:X2} {1:X2}", profileResponse.SW1, profileResponse.SW2); if (profileResponse.HasData) { var data = profileResponse.GetData(); var cardNumber = GetCardNumber(data[..12]);
private ReaderStatus GetReaderStatus(IsoReader reader) { var readerName = new string[0]; var cardState = SCardState.Unknown; var protocol = SCardProtocol.Unset; var atr = new byte[0]; var error = reader.Reader.Status(out readerName, out cardState, out protocol, out atr); return(new ReaderStatus { ReaderName = readerName, CardState = cardState, Protocol = protocol, Atr = atr }); }
public IsoReader TryConnect(String cardName) { try { IsoReader card = new IsoReader(this.reader); card.Connect(cardName, SCardShareMode.Shared, SCardProtocol.Any); return(card); } catch (PCSCException) { return(null); } }
public string Write() { Log.Debug("Card write"); try { using (var reader = new IsoReader(CardContext, GetReader(), SCardShareMode.Shared, SCardProtocol.Any, false)) { var status = GetReaderStatus(reader); Log.Debug(String.Format("Card State: {0}", status.CardState)); if (!status.CardState.HasFlag(SCardState.Present)) { return(null); } var id = GetCardId(reader); var cardName = GetCardName(status.Atr); var cardType = GetInt16(cardName); var isMifare = cardType == CardReader.Mifare1KCard || cardType == CardReader.Mifare4KCard; var isMifareUltralight = cardType == CardReader.MifareUltralightCard; Log.Debug(String.Format("Card Id: {0}", BitConverter.ToString(id))); var cardString = BitConverter.ToString(cardName.Concat(id).ToArray()).Replace("-", ""); if (isMifareUltralight) { var msg = new NdefMessage { new NdefUriRecord { Uri = CardReader.CardUri + "/#/" + cardString } }; var data = msg.ToByteArray(); var buffer = new List <byte>(new byte[] { 0x03, (byte)data.Length }.Concat(data)); WriteAllCardBytes(reader, buffer.ToArray(), isMifareUltralight ? 4 : 16); } return(BitConverter.ToString(cardName.Concat(id).ToArray()).Replace("-", "")); } } catch (Exception ex) { Log.Error(ex); } return(null); }
private byte[] GetAllCardBytes(IsoReader reader, int packetSize) { try { var firstDataBlock = 16 / packetSize; var readSize = 16; var bytesToRead = 0; var buffer = new List <byte>(); while (true) { var blockToRead = (byte)(firstDataBlock + (buffer.Count / packetSize)); var readBinaryCmd = new CommandApdu(IsoCase.Case2Short, SCardProtocol.Any) { CLA = 0xFF, Instruction = InstructionCode.ReadBinary, P1 = 0x00, P2 = blockToRead, Le = readSize }; var response = reader.Transmit(readBinaryCmd); var data = response.GetData(); if (buffer.Count == 0) { bytesToRead = data[1] + 1 + 1; } buffer.AddRange(data.Take(bytesToRead - buffer.Count < readSize ? bytesToRead - buffer.Count : readSize).ToArray()); if (buffer.Count >= bytesToRead) { break; } } Log.Debug(String.Format("ReadBinary: {0}", BitConverter.ToString(buffer.ToArray()))); Log.Debug(String.Format("Buffersize: Reported: {0}, Actual: {1}", bytesToRead, buffer.Count)); return(buffer.ToArray()); } catch (Exception ex) { Log.Error(ex); } return(new byte[0]); }
public string ReadAllText(IFileInfo file) { using (var fs = new FileStream(IsoFileInfo.FullName, FileMode.Open, FileAccess.Read)) { IsoReader reader = new IsoReader(); reader.Parse(fs); //var ret = DirectoryRecord.GetAllRecords(reader.WorkPvd.RootDir); //var fr = ret.First(x => x.IsFile && x.FullPath.ToLower() == file.FullName.ToLower()); var dat = (file as IsoFileWrapper).record.GetFileData(fs, reader.WorkPvd); MemoryStream ms = new MemoryStream(dat); var rdr = new StreamReader(ms); return(rdr.ReadToEnd()); } }
public byte[] GetCardId(IsoReader reader) { var command = new CommandApdu(IsoCase.Case2Short, SCardProtocol.Any) { CLA = 0xFF, Instruction = InstructionCode.GetData, P1 = 0x00, P2 = 0x00, Le = 0x00 }; var response = reader.Transmit(command); return(response.GetData()); }
internal void LoadIso(string fullName) { try { IsoReader reader = new IsoReader(); reader.Parse(fullName); pvdd = reader.Pvds.Last(); UpdateList(pvdd.RootDir); } catch (UnauthorizedAccessException ex) { MessageBox.Show("Access error.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public List <String> GetConnected() { List <String> ret = new List <String>(); List <String> readers = new List <string>(ctx.GetReaders()); foreach (string s in readers) { IsoReader f = TryConnect(s); if (f == null) { continue; } ret.Add(s); } return(ret); }
public IFileInfo GetFile(string path) { using (var fs = new FileStream(IsoFileInfo.FullName, FileMode.Open, FileAccess.Read)) { IsoReader reader = new IsoReader(); reader.Parse(fs); var ret = DirectoryRecord.GetAllRecords(reader.WorkPvd.RootDir); var aa = ret.First(x => x.IsFile && x.FullPath.ToLower() == path.ToLower()); return(new IsoFileWrapper(new MountInfo() { IsoPath = IsoFileInfo }, aa) { Filesystem = this }); } }
private void CardInserted(object sender, CardStatusEventArgs e) { Log.Debug("Card inserted"); try { using (var reader = new IsoReader(CardContext, GetReader(), SCardShareMode.Shared, SCardProtocol.Any, false)) { var id = GetCardId(reader); var status = GetReaderStatus(reader); var cardName = GetCardName(status.Atr); var cardType = GetInt16(cardName); var isMifare = cardType == Mifare1KCard || cardType == Mifare4KCard; var isMifareUltralight = cardType == MifareUltralightCard; //var bytes = GetAllCardBytes(reader, isMifareUltralight ? 4 : 16); var isShopCard = true; // IsShopCard(bytes); Log.Debug(String.Format("Card Id: {0}, Shop Card: {1}", BitConverter.ToString(id), isShopCard)); if (isShopCard) { var cardString = BitConverter.ToString(cardName.Concat(id).ToArray()).Replace("-", ""); EventAggregator.Publish(new CardInserted { CardId = cardString }); } else { EventAggregator.Publish(new InvalidCardInserted { }); } } } catch (Exception ex) { Log.Error(ex); EventAggregator.Publish(new InvalidCardInserted { }); } }
public void isoReaderInit() { try { var ctx = contextFactory.Establish(SCardScope.System); isoReader = new IsoReader( ctx, nfcReader, SCardShareMode.Shared, SCardProtocol.Any, false); card = new MifareCard(isoReader); } catch (Exception) { //MessageBox.Show(ex.Message, "Info", MessageBoxButton.OK, MessageBoxImage.Information); } }
public static MifareUltralight GetTagInfo() { var contextFactory = ContextFactory.Instance; var context = contextFactory.Establish(SCardScope.System); var readerName = ChooseReader(context); if (readerName == null) { return(null); } var isoReader = new IsoReader(context, readerName, SCardShareMode.Shared, SCardProtocol.Any, false); PrintReaderStatus(isoReader.Reader); return(new MifareUltralight(context, isoReader)); }
internal static void MountIso(MountInfo mountInfo) { Stuff.MountInfos.Add(mountInfo); //extract .indx/meta.xml var f = mountInfo; if (f.Reader == null) { IsoReader reader = new IsoReader(); reader.Parse(f.IsoPath.FullName); f.Reader = reader; } var r = new IsoDirectoryInfoWrapper(f, f.Reader.WorkPvd.RootDir); //r.Parent = null; r.Filesystem = new IsoFilesystem(f) { IsoFileInfo = f.IsoPath }; if (r.Filesystem.FileExist(".indx\\meta.xml")) { var txt = r.Filesystem.ReadAllText(".indx\\meta.xml"); var doc = XDocument.Parse(txt); foreach (var item in doc.Descendants("tag")) { var nm = item.Attribute("name").Value; var tagg = Stuff.AddTag(new TagInfo() { Name = nm }); foreach (var fitem in item.Descendants("file")) { var pt = fitem.Value; var path = Path.Combine(mountInfo.MountTarget.FullName, pt); var fls = Stuff.GetAllFiles(mountInfo.MountTarget); var fr = fls.First(z => z.FullName.ToLower() == path.ToLower()); tagg.AddFile(fr); //tagg.AddFile((r.Filesystem as IsoFilesystem).GetFile(path)); } } } }
public static void ReadCard(SCardContext context, string reader, ref string tes) { IsoReader isoReader = new IsoReader(context, reader, SCardShareMode.Shared, SCardProtocol.Any); var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) { CLA = 0xff, // Class. INS = 0xCA, // what instrution you are using. P1 = 0x00, // Parameter 1. P2 = 0x00, // Parameter 2. Le = 0x4 // Length of the return value. }; Response test = isoReader.Transmit(apdu); uID = BitConverter.ToString(test.GetData()); string[] a = uID.Split('-'); Array.Reverse(a); parrent.CardInserted(string.Join("", a)); }