14

Is there an Open Source library for writing and reading data in C# from a smartcard using a smartcard reader? My smartcard model is mifare1k and my reader is ucr122u.

Wayne Koorts
  • 10,142
  • 10
  • 45
  • 72
xyrus the virus
  • 199
  • 1
  • 2
  • 7
  • possible duplicate of [Do you have an open source example for saving data in RFID smart card reader..](http://stackoverflow.com/questions/4014037/do-you-have-an-open-source-example-for-saving-data-in-rfid-smart-card-reader) – TFD Nov 07 '10 at 06:13
  • 2
    @TFD, this question is a lot more specific, which should also make it easier to answer. It mentions a specific card and reader. – Matthew Flaschen Nov 07 '10 at 06:17
  • @Matthew_Flaschen True, but IIRC Mifare UCR are Asian clones, need to find what base model is anyway. Have asked is first question. Also anyone using Mifare 1k needs their head read! – TFD Nov 08 '10 at 03:00
  • More like a duplicate of http://stackoverflow.com/questions/3099774/how-can-i-edit-the-content-of-my-smart-card or http://stackoverflow.com/questions/2793423/writing-a-text-file-to-smart-card-c – Martin Paljak Nov 09 '10 at 20:02

3 Answers3

8

Check out Daniel Müller's pcsc-sharp, https://github.com/danm-de/pcsc-sharp

works very well for me on windows and mono.

Guillaume Raymond
  • 1,328
  • 15
  • 28
IanNorton
  • 6,650
  • 2
  • 22
  • 27
1

I know this is an old question but you might want to PCSC-Sharp which is

PC/SC wrapper classes for .NET, written in C#. The package contains classes to access the Personal Computer/Smart Card Resource Manager using the system's native PC/SC API. Implements partial ISO7816 support. The library is written to run on both, Windows and Unix (Linux with Mono using PCSC Lite).

The project is on GitHub: https://github.com/danm-de/pcsc-sharp

You can also check the documentation here: https://danm.de/docs/pcsc-sharp/index.html

Maxime
  • 7,283
  • 4
  • 48
  • 49
0

for acr1252u

I have found the solution in a c++ code: only in the linker we need to add winscard.h

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdint>
#include <cstring>
#include <winscard.h>

std::wstring s2ws(const std::string& s);

int main(int argc, char* argv[]) {
    SCARDCONTEXT context = 0;
    LONG ret = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, &context);

    if (ret != SCARD_S_SUCCESS) {
        std::cout << "SCardEstablishContext: " << ret<< std::endl;
    }
    else {
        LPTSTR allReaderNames = nullptr;
        DWORD readerCount = SCARD_AUTOALLOCATE;

        ret = SCardListReaders(context, nullptr, reinterpret_cast<LPTSTR>(&allReaderNames), &readerCount);

        if (ret != SCARD_S_SUCCESS) {
            std::cout << "SCardListReaders: " << ret << std::endl;
        }
        else {
            std::string readerName("ACS ACR1252 1S CL Reader PICC 0");
            std::wstring stemp = s2ws(readerName);
            LPCWSTR result = stemp.c_str();
            DWORD activeProtocol = 0;
            SCARDHANDLE card = 0;

            ret = SCardConnect(context, result, SCARD_SHARE_DIRECT, 0, &card, &activeProtocol);

            if (ret != SCARD_S_SUCCESS) {
                std::cout << "SCardConnect: " << ret << std::endl;
            }
            else {
                std::vector<std::uint8_t> outputBuffer{ 0xE0, 0x0, 0x0, 0x21, 0x01, 0x71 };
                std::vector<std::uint8_t> inputBuffer(64, 0);
                DWORD bytesReturned = 0;

                DWORD controlcode = SCARD_CTL_CODE(3500);
                ret = SCardControl(card, controlcode, outputBuffer.data(), outputBuffer.size(), inputBuffer.data(), inputBuffer.size(), &bytesReturned);

                if (ret != SCARD_S_SUCCESS) {
                    std::cout << "SCardControl: " << ret << std::endl;
                }
                else {
                    std::cout << "Response: " << std::hex << std::setfill('0');
                    for (std::size_t i = 0; i < bytesReturned; ++i) {
                        std::cout << std::setw(2) << static_cast<std::uint32_t>(inputBuffer[i]) << " ";
                    }
                    std::cout << std::dec << std::endl;

                    SCardDisconnect(card, SCARD_LEAVE_CARD);
                }
            }

            // Release the memory that SCardListReaders allocated for us
            SCardFreeMemory(context, allReaderNames);
        }

        ret = SCardReleaseContext(context);

        if (ret != SCARD_S_SUCCESS) {
            std::cout << "SCardReleaseContext: " << ret << std::endl;
        }
        std::getchar();
    }

    return 0;
}

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}