15

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

4

3 回答 3

9

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

在 Windows 和单声道上非常适合我。

于 2011-04-16T23:41:34.097 回答
1

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

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

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

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

于 2015-07-10T02:55:44.170 回答
0

对于 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;
}
于 2016-07-20T08:27:36.930 回答