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
.
Check out Daniel Müller's pcsc-sharp, https://github.com/danm-de/pcsc-sharp
works very well for me on windows and mono.
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
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;
}
© 2022 - 2024 — McMap. All rights reserved.