你如何写/读智能卡?

时间:2010-11-07 06:11:11

标签: c# smartcard rfid mifare

是否有一个开源库,用于使用智能卡读卡器从智能卡中写入和读取C#中的数据?我的智能卡型号为mifare1k,我的读者为ucr122u

3 个答案:

答案 0 :(得分:8)

查看DanielMüller的pcsc-sharp,https://github.com/danm-de/pcsc-sharp

在Windows和单声道上对我很有效。

答案 1 :(得分:1)

我知道这是一个老问题但您可能想要PCSC-Sharp

  

.NET的PC / SC包装类,用C#编写。该包包含类   使用系统访问个人计算机/智能卡资源管理器   原生PC / SC API。实现部分ISO7816支持。   该库被编写为在Windows和Unix上运行(使用Mono的Linux)   PCSC Lite)。

该项目在GitHub上: https://github.com/danm-de/pcsc-sharp

您还可以在此处查看文档: https://danm.de/docs/pcsc-sharp/index.html

答案 2 :(得分:0)

for acr1252u

我在c ++代码中找到了解决方案: 只在链接器中我们需要添加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;
}