How do you write / read to a smartcard? [closed]
Asked Answered
H

3

16

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.

Hammons answered 7/11, 2010 at 6:11 Comment(4)
possible duplicate of Do you have an open source example for saving data in RFID smart card reader..Wilford
@TFD, this question is a lot more specific, which should also make it easier to answer. It mentions a specific card and reader.Leaper
@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!Wilford
More like a duplicate of #3100274 or #2793923Ventriloquy
D
9

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

works very well for me on windows and mono.

Dawndawna answered 16/4, 2011 at 23:41 Comment(1)
Current link: github.com/danm-de/pcsc-sharpShackelford
C
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

Cigarillo answered 10/7, 2015 at 2:55 Comment(0)
M
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;
}
Mammilla answered 20/7, 2016 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.