{"id":244,"date":"2010-12-31T13:09:49","date_gmt":"2010-12-31T11:09:49","guid":{"rendered":"https:\/\/www.livediesel.de\/?page_id=244"},"modified":"2010-12-31T13:59:55","modified_gmt":"2010-12-31T11:59:55","slug":"smartcard-programming","status":"publish","type":"page","link":"https:\/\/www.livediesel.de\/?page_id=244","title":{"rendered":"SmartCard programming in C#"},"content":{"rendered":"<p><strong>Communicate with SmartCards using C# and WinSCard<\/strong><\/p>\n<p>Because I&#8217;ve not found any complete wrapper lib for C# I created it. If there is something missing, feel free to contact me.<\/p>\n<p><a href=\"https:\/\/www.livediesel.de\/wp-content\/uploads\/2010\/12\/WinSCard.zip\">WinSCard.zip<\/a><\/p>\n<p><strong>Some little helper functions to make the life easier<\/strong><\/p>\n<p>Required values:<\/p>\n<pre lang=\"csharp\">\r\nIntPtr pcscContext; \/\/ Pointer to the resource manager context\r\nIntPtr pcscCard; \/\/ Pointer to the connection to the smart card\r\nuint lastResult; \/\/ Stores the last SCard function result\r\nSCardSelectedFileResponse selectedFileResponse; \/\/ Stores the last response to SelectFile command\r\n<\/pre>\n<p>SCardGetAttrib helper:<\/p>\n<pre lang=\"csharp\">\r\nprivate bool GetAttrib(SCARD_ATTR attr, ref byte[] buffer, ref uint bufferlen)\r\n{\r\n\u00a0if (WinSCard.SCARD_S_SUCCESS == (lastResult = WinSCard.SCardGetAttrib(pcscCard, attr, buffer, ref bufferlen)))\r\n\u00a0 return true;\r\n\u00a0return false;\r\n}\r\n\r\nbyte[] buffer = new byte[256];\r\nuint bufferlen = 256;\r\nstring vendorName = \"\";\r\nif (GetAttrib(SCARD_ATTR.VENDOR_NAME, ref buffer, ref bufferlen))\r\nvendorName = WinSCard.SCardByteArrayToString(buffer, (int)bufferlen);\r\n<\/pre>\n<p>SCardTransmit helper:<\/p>\n<pre lang=\"csharp\">\r\nprivate bool Transmit(byte[] command, ref byte[] buffer, ref uint bufferlen)\r\n{\r\n WinSCard.SCARD_IO_REQUEST req = new WinSCard.SCARD_IO_REQUEST();\r\n req.cbPciLength = 0x8;\r\n req.dwProtocol = 0x1;\r\n buffer = new byte[0x3E9];\r\n bufferlen = 0x3E9;\r\n if (WinSCard.SCARD_S_SUCCESS == (lastResult = WinSCard.SCardTransmit(pcscCard, req, command, (uint) command.Length, 0, buffer, ref bufferlen)))\r\n  return true;\r\n return false;\r\n}\r\n<\/pre>\n<p>Using SCardTransmit with the SelectFile Helper:<\/p>\n<pre lang=\"csharp\">\r\nprivate bool SelectFile(ushort Index)\r\n{\r\n byte[] buffer = new byte[0];\r\n uint bufferlen = 0;\r\n byte[] cmd = BitConverter.GetBytes(Index);\r\n cmd = new byte[] { 0x00, 0xA4, 0x00, 0x00, 0x02, cmd[1], cmd[0] };\r\n if (!Transmit(cmd, ref buffer, ref bufferlen)) return false;\r\n if (WinSCard.SCARD_SW_DATA_STILL_AVAIL == (WinSCard.SCardGetStatusWord(buffer, (int)bufferlen) &amp; 0xFF00))\r\n {\r\n  cmd = new byte[] { 0x00, 0xC0, 0x00, 0x00, (byte)(WinSCard.SCardGetStatusWord(buffer, (int)bufferlen) &amp; 0xFF) };\r\n  if (Transmit(cmd, ref buffer, ref bufferlen))\r\n   selectedFileResponse = new SCardSelectedFileResponse(buffer);\r\n }\r\n return true;\r\n}\r\n\r\nSelectFile(0x3F00);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Communicate with SmartCards using C# and WinSCard Because I&#8217;ve not found any complete wrapper lib for C# I created it. If there is something missing, feel free to contact me. WinSCard.zip Some little helper functions to make the life easier Required values: IntPtr pcscContext; \/\/ Pointer to the resource manager context IntPtr pcscCard; \/\/ Pointer [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":184,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-244","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/pages\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.livediesel.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=244"}],"version-history":[{"count":32,"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/pages\/244\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/pages\/244\/revisions\/287"}],"up":[{"embeddable":true,"href":"https:\/\/www.livediesel.de\/index.php?rest_route=\/wp\/v2\/pages\/184"}],"wp:attachment":[{"href":"https:\/\/www.livediesel.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}