public static byte[] GetSMResponse(Apdu apdu, byte[] resp, BSO encOut, BSO sigOut, byte[] random) { TLV respTLV = new TLV(); ByteArray ClearData = new ByteArray(resp).Sub(0, resp.Length - 2); byte[] CypherTextObject = null; if (encOut == null && sigOut == null) { if (ClearData.Size != 0) { respTLV[0x81] = ClearData; } } else { if (encOut != null) { respTLV[0x87] = new ByteArray(1).Append(CardHandler.encrypt3DES(encOut.Data, ClearData)); CypherTextObject = respTLV[0x87]; } else { if (ClearData.Size != 0) { respTLV[0x81] = ClearData; } } if (sigOut != null) { if (random == null) { throw new ISO7816Exception(Error.ConditionsOfUseNotSatisfied); } var MACObject = new ByteArray(random); MACObject = MACObject.Append(ByteArray.ANSIPad(new ByteArray(apdu.GetBytes()).Left(4))); TLV MacTLV = new TLV(); if (CypherTextObject != null) { MacTLV[0x87] = CypherTextObject; } else { MacTLV[0x81] = ClearData; } MACObject = MACObject.Append(MacTLV.GetBytes()); var mac = CardHandler.getMAC(sigOut.Data, ByteArray.ANSIPad(MACObject)); respTLV[0x8e] = mac; } } ByteArray smResp = new ByteArray(respTLV.GetBytes()); smResp = smResp.Append(new byte[] { resp[resp.Length - 2], resp[resp.Length - 1] }); return(smResp); }