public string GetBlockValue(int block, int hCard) { var value = @""; _returnCode = AuthenticateBlock(block, hCard); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Fail to authenticate block", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } _recvBuffLen = _sentBuff[4] + 2; _sentBuff = new byte[255]; _recvBuff = new byte[255]; _sentBuff = APDUCommands.GetBlockValue(block); _returnCode = ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff.Length, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Fail to fetch block value", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } for (int j = 0; j <= _recvBuffLen - 3; j++) { value += Convert.ToChar(_recvBuff[j]); } return(value); }
private int AuthenticateBlock(int nBlock, int hCard) { _ioSendRequest = new ModWinsCard.SCARD_IO_REQUEST { dwProtocol = ModWinsCard.SCARD_PROTOCOL_UNDEFINED, cbPciLength = 8 }; _recvBuffLen = 0x02; _sentBuff = new byte[255]; _recvBuff = new byte[255]; _sentBuff = APDUCommands.AuthenticateBlock(nBlock); return(ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff.Length, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen)); }
public void UpdateBlockValue(int block, string value, int hCard) { _returnCode = AuthenticateBlock(block, hCard); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS) { throw new ApplicationException(@"Fail to authenticate block", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } _recvBuffLen = 0x02; _sentBuff = new byte[255]; _recvBuff = new byte[255]; _sentBuff = APDUCommands.SetBlockValue(block, value); _returnCode = ModWinsCard.SCardTransmit(hCard, ref _ioSendRequest, ref _sentBuff[0], _sentBuff[4] + 5, ref _ioSendRequest, ref _recvBuff[0], ref _recvBuffLen); if (_returnCode != ModWinsCard.SCARD_S_SUCCESS || _recvBuff[0].ToString(@"X2") != @"90") { throw new ApplicationException(@"Fail to update block value", new ApplicationException(ModWinsCard.GetScardErrMsg(_returnCode))); } }