using System.Collections.Generic;
using System.Net.Sockets;
public static void Main()
var hsm = new MyHsm(30 * 1000, true);
hsm.Config("10.34.95.10", 1500);
hsm.AddVal(MyHsmFldDef.FAA[0], "23F0179AFA985AE2F9BE3B8977C5CA66");
hsm.AddVal(MyHsmFldDef.FAB[0], "4022751500028505");
hsm.AddVal(MyHsmFldDef.FAC[0], "910");
hsm.AddVal(MyHsmFldDef.FAD[0], "201");
string sRspCode = hsm.GetValue<string>(MyHsmFldDef.F01[0]);
string sErrCode = hsm.GetValue<string>(MyHsmFldDef.F02[0]);
string sCvv = hsm.GetValue<string>(MyHsmFldDef.FAE[0]);
public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue>
public void AddOrReplace(TKey key, TValue val)
if (this.ContainsKey(key))
public static class MyHsmCmdDef
public static MyDictionary<String, String[]> HsmCmd { get; private set; }
public static void Load()
HsmCmd = new MyDictionary<string, string[]>();
HsmCmd.AddOrReplace("A0", new String[] { "A0", "A1", "Generate a Key" });
HsmCmd.AddOrReplace("A2", new String[] { "A2", "A3", "Generate and Print a Component" });
HsmCmd.AddOrReplace("A4", new String[] { "A4", "A5", "Form a Key from Encrypted Components" });
HsmCmd.AddOrReplace("A6", new String[] { "A6", "A7", "Import a Key" });
HsmCmd.AddOrReplace("A8", new String[] { "A8", "A9", "Export a Key" });
HsmCmd.AddOrReplace("AA", new String[] { "AA", "AB", "Translate a TMK, TPK or PVK" });
HsmCmd.AddOrReplace("AC", new String[] { "AC", "AD", "Translate a TAK" });
HsmCmd.AddOrReplace("AE", new String[] { "AE", "AF", "Translate a TMK, TPK or PVK from LMK to Another TMK, TPK or PVK" });
HsmCmd.AddOrReplace("AG", new String[] { "AG", "AH", "Translate a TAK from LMK to TMK Encryption" });
HsmCmd.AddOrReplace("AS", new String[] { "AS", "AT", "Generate a CVK Pair" });
HsmCmd.AddOrReplace("AU", new String[] { "AU", "AV", "Translate a CVK Pair from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("AW", new String[] { "AW", "AX", "Translate a CVK Pair from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("AY", new String[] { "AY", "AZ", "Translate a CVK Pair from Old LMK to New LMK Encryption" });
HsmCmd.AddOrReplace("B0", new String[] { "B0", "B1", "Translate Key Scheme" });
HsmCmd.AddOrReplace("B2", new String[] { "B2", "B3", "Echo Command" });
HsmCmd.AddOrReplace("BA", new String[] { "BA", "BB", "Encrypt a Clear PIN" });
HsmCmd.AddOrReplace("BC", new String[] { "BC", "BD", "Verify a Terminal PIN Using the Comparison Method" });
HsmCmd.AddOrReplace("BE", new String[] { "BE", "BF", "Verify an Interchange PIN Using the Comparison Method" });
HsmCmd.AddOrReplace("BG", new String[] { "BG", "BH", "Translate a PIN and PIN Length" });
HsmCmd.AddOrReplace("BI", new String[] { "BI", "BJ", "Generate a BDK" });
HsmCmd.AddOrReplace("BK", new String[] { "BK", "BL", "Generate an IBM PIN Offset (of a customer selected PIN)" });
HsmCmd.AddOrReplace("BM", new String[] { "BM", "BN", "Load the Excluded PIN Table" });
HsmCmd.AddOrReplace("BQ", new String[] { "BQ", "BR", "Translate PIN Algorithm" });
HsmCmd.AddOrReplace("BS", new String[] { "BS", "BT", "Erase the Key Change Storage" });
HsmCmd.AddOrReplace("BU", new String[] { "BU", "BV", "Generate a Key Check Value" });
HsmCmd.AddOrReplace("BW", new String[] { "BW", "BX", "Translate Keys from Old LMK to New LMK" });
HsmCmd.AddOrReplace("BY", new String[] { "BY", "BZ", "Translate ZMK from ZMK to LMK encryption" });
HsmCmd.AddOrReplace("CA", new String[] { "CA", "CB", "Translate a PIN from TPK to ZPK Encryption" });
HsmCmd.AddOrReplace("CC", new String[] { "CC", "CD", "Translate a PIN from One ZPK to Another" });
HsmCmd.AddOrReplace("CE", new String[] { "CE", "CF", "Generate a Diebold PIN Offset" });
HsmCmd.AddOrReplace("CG", new String[] { "CG", "CH", "Verify a Terminal PIN Using the Diebold Method" });
HsmCmd.AddOrReplace("CI", new String[] { "CI", "CJ", "Translate a PIN from BDK to ZPK Encryption (DUKPT)" });
HsmCmd.AddOrReplace("CK", new String[] { "CK", "CL", "Verify a PIN Using the IBM Method (DUKPT)" });
HsmCmd.AddOrReplace("CM", new String[] { "CM", "CN", "Verify a PIN Using the VISA PVV Method (DUKPT)" });
HsmCmd.AddOrReplace("CO", new String[] { "CO", "CP", "Verify a PIN Using the Diebold Method (DUKPT)" });
HsmCmd.AddOrReplace("CQ", new String[] { "CQ", "CR", "Verify a PIN Using the Encrypted PIN Method (DUKPT)" });
HsmCmd.AddOrReplace("CS", new String[] { "CS", "CT", "Modify Keyblock Header" });
HsmCmd.AddOrReplace("CU", new String[] { "CU", "CV", "Verify & Generate a VISA PVV (of a customer selected PIN)" });
HsmCmd.AddOrReplace("CW", new String[] { "CW", "CX", "Generate a Card Verification Code/Value" });
HsmCmd.AddOrReplace("CY", new String[] { "CY", "CZ", "Verify a Card Verification Code/Value" });
HsmCmd.AddOrReplace("DA", new String[] { "DA", "DB", "Verify a Terminal PIN Using the IBM Method" });
HsmCmd.AddOrReplace("DC", new String[] { "DC", "DD", "Verify a Terminal PIN Using the VISA Method" });
HsmCmd.AddOrReplace("DE", new String[] { "DE", "DF", "Generate an IBM PIN Offset (of an LMK encrypted PIN)" });
HsmCmd.AddOrReplace("DG", new String[] { "DG", "DH", "Generate a VISA PIN Verification Value (of an LMK encrypted PIN)" });
HsmCmd.AddOrReplace("DI", new String[] { "DI", "DJ", "Generate and Export a KML" });
HsmCmd.AddOrReplace("DK", new String[] { "DK", "DL", "Import a KML" });
HsmCmd.AddOrReplace("DM", new String[] { "DM", "DN", "Verify Load Signature S1 and Generate Load Signature S2" });
HsmCmd.AddOrReplace("DO", new String[] { "DO", "DP", "Verify Load Completion Signature S3" });
HsmCmd.AddOrReplace("DQ", new String[] { "DQ", "DR", "Verify Unload Signature S1 and Generate Unload Signature S2" });
HsmCmd.AddOrReplace("DS", new String[] { "DS", "DT", "Verify Unload Completion Signature S3" });
HsmCmd.AddOrReplace("DU", new String[] { "DU", "DV", "Verify & Generate an IBM PIN Offset (of customer selected new PIN)" });
HsmCmd.AddOrReplace("DW", new String[] { "DW", "DX", "Translate a BDK from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("DY", new String[] { "DY", "DZ", "Translate a BDK from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("EA", new String[] { "EA", "EB", "Verify an Interchange PIN Using the IBM Method" });
HsmCmd.AddOrReplace("EC", new String[] { "EC", "ED", "Verify an Interchange PIN Using the VISA Method" });
HsmCmd.AddOrReplace("EE", new String[] { "EE", "EF", "Derive a PIN Using the IBM Method" });
HsmCmd.AddOrReplace("EG", new String[] { "EG", "EH", "Verify an Interchange PIN Using the Diebold Method" });
HsmCmd.AddOrReplace("EI", new String[] { "EI", "EJ", "Generate a Public/Private Key Pair" });
HsmCmd.AddOrReplace("EK", new String[] { "EK", "EL", "Load a Private Key" });
HsmCmd.AddOrReplace("EM", new String[] { "EM", "EN", "Translate a Private Key" });
HsmCmd.AddOrReplace("EO", new String[] { "EO", "EP", "Import a Public Key" });
HsmCmd.AddOrReplace("EQ", new String[] { "EQ", "ER", "Validate a Public Key" });
HsmCmd.AddOrReplace("ES", new String[] { "ES", "ET", "Validate a Certificate and Import the Public Key" });
HsmCmd.AddOrReplace("EU", new String[] { "EU", "EV", "Translate a Public Key" });
HsmCmd.AddOrReplace("EW", new String[] { "EW", "EX", "Generate an RSA Signature" });
HsmCmd.AddOrReplace("EY", new String[] { "EY", "EZ", "Validate an RSA Signature" });
HsmCmd.AddOrReplace("FA", new String[] { "FA", "FB", "Translate a ZPK from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("FC", new String[] { "FC", "FD", "Translate a TMK, TPK or PVK from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("FE", new String[] { "FE", "FF", "Translate a TMK, TPK or PVK from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("FG", new String[] { "FG", "FH", "Generate a Pair of PVKs" });
HsmCmd.AddOrReplace("FI", new String[] { "FI", "FJ", "Generate ZEK/ZAK" });
HsmCmd.AddOrReplace("FK", new String[] { "FK", "FL", "Translate a ZEK/ZAK from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("FM", new String[] { "FM", "FN", "Translate a ZEK/ZAK from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("FO", new String[] { "FO", "FP", "Generate a Watchword Key" });
HsmCmd.AddOrReplace("FQ", new String[] { "FQ", "FR", "Translate a Watchword Key from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("FS", new String[] { "FS", "FT", "Translate a Watchword Key from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("FU", new String[] { "FU", "FV", "Verify a Watchword Response" });
HsmCmd.AddOrReplace("FW", new String[] { "FW", "FX", "Generate a VISA PIN Verification Value (of a customer selected PIN)" });
HsmCmd.AddOrReplace("G0", new String[] { "G0", "G1", "Translate a PIN from BDK to ZPK Encryption (3DES DUKPT)" });
HsmCmd.AddOrReplace("GA", new String[] { "GA", "GB", "Derive a PIN Using the Diebold" });
HsmCmd.AddOrReplace("GC", new String[] { "GC", "GD", "Translate a ZPK from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("GE", new String[] { "GE", "GF", "Translate a ZMK" });
HsmCmd.AddOrReplace("GG", new String[] { "GG", "GH", "Form a ZMK from Three ZMK Components" });
HsmCmd.AddOrReplace("GI", new String[] { "GI", "GJ", "Import Key under an RSA Public Key" });
HsmCmd.AddOrReplace("GK", new String[] { "GK", "GL", "Export Key under an RSA Public Key" });
HsmCmd.AddOrReplace("GM", new String[] { "GM", "GN", "Hash a Block of Data" });
HsmCmd.AddOrReplace("GO", new String[] { "GO", "GP", "Verify a PIN Using the IBM Method (3DES DUKPT)" });
HsmCmd.AddOrReplace("GQ", new String[] { "GQ", "GR", "Verify a PIN Using the VISA PVV Method (3DES DUKPT)" });
HsmCmd.AddOrReplace("GS", new String[] { "GS", "GT", "Verify a PIN Using the Diebold Method (3DES DUKPT)" });
HsmCmd.AddOrReplace("GU", new String[] { "GU", "GV", "Verify a PIN Using the Encrypted PIN Method (3DES DUKPT)" });
HsmCmd.AddOrReplace("GW", new String[] { "GW", "GX", "Generate/Verify a MAC (3DES DUKPT)" });
HsmCmd.AddOrReplace("GY", new String[] { "GY", "GZ", "Form a ZMK from 2 to 9 ZMK Components" });
HsmCmd.AddOrReplace("HA", new String[] { "HA", "HB", "Generate a TAK" });
HsmCmd.AddOrReplace("HC", new String[] { "HC", "HD", "Generate a TMK, TPK or PVK" });
HsmCmd.AddOrReplace("IA", new String[] { "IA", "IB", "Generate a ZPK" });
HsmCmd.AddOrReplace("JA", new String[] { "JA", "JB", "Generate a Random PIN" });
HsmCmd.AddOrReplace("JC", new String[] { "JC", "JD", "Translate a PIN from TPK to LMK Encryption" });
HsmCmd.AddOrReplace("JE", new String[] { "JE", "JF", "Translate a PIN from ZPK to LMK Encryption" });
HsmCmd.AddOrReplace("JG", new String[] { "JG", "JH", "Translate a PIN from LMK to ZPK Encryption" });
HsmCmd.AddOrReplace("K0", new String[] { "K0", "K1", "Decrypt Encrypted Counters (EMV 4.x)" });
HsmCmd.AddOrReplace("K2", new String[] { "K2", "K3", "Verify Truncated Application Cryptogram (MasterCard CAP)" });
HsmCmd.AddOrReplace("KA", new String[] { "KA", "KB", "Generate a Key Check Value (Not Double-Length ZMK)" });
HsmCmd.AddOrReplace("KC", new String[] { "KC", "KD", "Translate a ZPK" });
HsmCmd.AddOrReplace("KQ", new String[] { "KQ", "KR", "ARQC Verification and/or ARPC Generation (EMV 3.1.1)" });
HsmCmd.AddOrReplace("KS", new String[] { "KS", "KT", "Data Authentication Code and Dynamic Number Verification (EMV 3.1.1)" });
HsmCmd.AddOrReplace("KU", new String[] { "KU", "KV", "Generate Secure Message (EMV 3.1.1)" });
HsmCmd.AddOrReplace("KW", new String[] { "KW", "KX", "ARQC Verification and/or ARPC Generation (EMV 4.x)" });
HsmCmd.AddOrReplace("KY", new String[] { "KY", "KZ", "Generate Secure Message (EMV 4.x)" });
HsmCmd.AddOrReplace("L0", new String[] { "L0", "L1", "Generate an HMAC Secret Key" });
HsmCmd.AddOrReplace("LA", new String[] { "LA", "LB", "Load Data to User Storage" });
HsmCmd.AddOrReplace("LC", new String[] { "LC", "LD", "Verify the Diebold Table in User Storage" });
HsmCmd.AddOrReplace("LE", new String[] { "LE", "LF", "Read Data from User Storage" });
HsmCmd.AddOrReplace("LG", new String[] { "LG", "LH", "Set HSM Response Delay" });
HsmCmd.AddOrReplace("LI", new String[] { "LI", "LJ", "Load a PIN Text String" });
HsmCmd.AddOrReplace("LK", new String[] { "LK", "LL", "Generate a Decimal MAC" });
HsmCmd.AddOrReplace("LM", new String[] { "LM", "LN", "Verify a Decimal MAC" });
HsmCmd.AddOrReplace("LO", new String[] { "LO", "LP", "Translate Decimalisation Table from Old to New LMK" });
HsmCmd.AddOrReplace("LQ", new String[] { "LQ", "LR", "Generate an HMAC on a Block of Data" });
HsmCmd.AddOrReplace("LS", new String[] { "LS", "LT", "Verify an HMAC on a Block of Data" });
HsmCmd.AddOrReplace("LU", new String[] { "LU", "LV", "Import an HMAC key under a ZMK" });
HsmCmd.AddOrReplace("LW", new String[] { "LW", "LX", "Export an HMAC key under a" });
HsmCmd.AddOrReplace("LY", new String[] { "LY", "LZ", "Translate a HMAC Key from Old LMK to New LMK" });
HsmCmd.AddOrReplace("M0", new String[] { "M0", "M1", "Encrypt Data Block" });
HsmCmd.AddOrReplace("M2", new String[] { "M2", "M3", "Decrypt Data Block" });
HsmCmd.AddOrReplace("M4", new String[] { "M4", "M5", "Translate Data Block" });
HsmCmd.AddOrReplace("M6", new String[] { "M6", "M7", "Generate MAC" });
HsmCmd.AddOrReplace("M8", new String[] { "M8", "M9", "Verify MAC" });
HsmCmd.AddOrReplace("MA", new String[] { "MA", "MB", "Generate a MAC" });
HsmCmd.AddOrReplace("MC", new String[] { "MC", "MD", "Verify a MAC" });
HsmCmd.AddOrReplace("ME", new String[] { "ME", "MF", "Verify and Translate a MAC" });
HsmCmd.AddOrReplace("MG", new String[] { "MG", "MH", "Translate a TAK from LMK to ZMK Encryption" });
HsmCmd.AddOrReplace("MI", new String[] { "MI", "MJ", "Translate a TAK from ZMK to LMK Encryption" });
HsmCmd.AddOrReplace("MK", new String[] { "MK", "ML", "Generate a Binary MAC" });
HsmCmd.AddOrReplace("MM", new String[] { "MM", "MN", "Verify a Binary MAC" });
HsmCmd.AddOrReplace("MO", new String[] { "MO", "MP", "Verify and Translate a Binary MAC" });
HsmCmd.AddOrReplace("MQ", new String[] { "MQ", "MR", "Generate MAC (MAB) for Large Message" });
HsmCmd.AddOrReplace("MS", new String[] { "MS", "MT", "Generate MAC (MAB) using ANSI X9.19 Method for a Large Message" });
HsmCmd.AddOrReplace("MY", new String[] { "MY", "MZ", "Verify and Translate MAC" });
HsmCmd.AddOrReplace("NC", new String[] { "NC", "ND", "Perform Diagnostics" });
HsmCmd.AddOrReplace("NE", new String[] { "NE", "NF", "NZ) Generate and Print a Key as Split Components" });
HsmCmd.AddOrReplace("NG", new String[] { "NG", "NH", "Decrypt an Encrypted PIN" });
HsmCmd.AddOrReplace("NI", new String[] { "NI", "NJ", "Return Network Information" });
HsmCmd.AddOrReplace("NK", new String[] { "NK", "NL", "Command Chaining" });
HsmCmd.AddOrReplace("NO", new String[] { "NO", "NP", "HSM Status" });
HsmCmd.AddOrReplace("OA", new String[] { "OA", "OB", "OZ) Print a PIN Solicitation Mailer" });
HsmCmd.AddOrReplace("OC", new String[] { "OC", "OD", "OZ) Generate and Print a ZMK Component" });
HsmCmd.AddOrReplace("OE", new String[] { "OE", "OF", "OZ) Generate and Print a TMK, TPK or PVK" });
HsmCmd.AddOrReplace("PA", new String[] { "PA", "PB", "Load Formatting Data to HSM" });
HsmCmd.AddOrReplace("PC", new String[] { "PC", "PD", "Load Additional Formatting Data to HSM" });
HsmCmd.AddOrReplace("PE", new String[] { "PE", "PF", "PZ) Print PIN/PIN and Solicitation Data" });
HsmCmd.AddOrReplace("PG", new String[] { "PG", "PH", "Verify PIN/PIN and Solicitation Mailer Cryptography" });
HsmCmd.AddOrReplace("PM", new String[] { "PM", "PN", "Verify a Dynamic CVV (dCVV)" });
HsmCmd.AddOrReplace("Q0", new String[] { "Q0", "Q1", "Translate Audit Record MAC key" });
HsmCmd.AddOrReplace("Q2", new String[] { "Q2", "Q3", "Retrieve Audit Record" });
HsmCmd.AddOrReplace("Q4", new String[] { "Q4", "Q5", "Archive (Print) Audit Record" });
HsmCmd.AddOrReplace("Q6", new String[] { "Q6", "Q7", "Delete Audit Record" });
HsmCmd.AddOrReplace("Q8", new String[] { "Q8", "Q9", "Audit Record Verification" });
HsmCmd.AddOrReplace("QA", new String[] { "QA", "QB", "Load Solicitation Data to User Storage" });
HsmCmd.AddOrReplace("QC", new String[] { "QC", "QD", "Final Load of Solicitation Data to User Storage" });
HsmCmd.AddOrReplace("RA", new String[] { "RA", "RB", "Cancel Authorised Activities" });
HsmCmd.AddOrReplace("RC", new String[] { "RC", "RD", "Verify Solicitation Mailer Cryptography" });
HsmCmd.AddOrReplace("RI", new String[] { "RI", "RJ", "Transaction Request With a PIN (T/AQ Key" });
HsmCmd.AddOrReplace("RK", new String[] { "RK", "RL", "Transaction Request Without a PIN" });
HsmCmd.AddOrReplace("RM", new String[] { "RM", "RN", "Administration Request Message" });
HsmCmd.AddOrReplace("RO", new String[] { "RO", "RP", "Transaction Response with Auth Para from Card Issuer" });
HsmCmd.AddOrReplace("RQ", new String[] { "RQ", "RR", "Generate Auth Para and Transaction Response" });
HsmCmd.AddOrReplace("RS", new String[] { "RS", "RT", "Confirmation" });
HsmCmd.AddOrReplace("RU", new String[] { "RU", "RV", "Transaction Request With a PIN (T/CI Key)" });
HsmCmd.AddOrReplace("RW", new String[] { "RW", "RX", "Translate KEYVAL" });
HsmCmd.AddOrReplace("RY", new String[] { "RY", "RZ", "Calculate Card Security Codes" });
public String sTag = String.Empty;
public Boolean bOpt = false;
public static class MyHsmCmdFmtDef
public static MyDictionary<String, String> HsmCmdRules { get; private set; }
public static void Load()
HsmCmdRules = new MyDictionary<String, String>();
HsmCmdRules.AddOrReplace("BA", "00|2|A|M#AL|5|L|M#AM|12|N|M");
HsmCmdRules.AddOrReplace("BB", "01|2|A|M#02|2|N|M#AN|5|N|M");
HsmCmdRules.AddOrReplace("CA", "00|2|A|M#AP|-1|H|M#AR|-1|H|M#AS|2|N|M#AO|16|H|M#AT|2|N|M#AU|2|N|M#AM|12|N|M");
HsmCmdRules.AddOrReplace("CB", "01|2|A|M#02|2|N|M#AW|2|N|M#AZ|16|H|M#AU|2|N|M");
HsmCmdRules.AddOrReplace("CC", "00|2|A|M#AP|-1|H|M#AR|-1|H|M#AS|2|N|M#AO|16|H|M#AT|2|N|M#AU|2|N|M#AM|12|N|M");
HsmCmdRules.AddOrReplace("CD", "01|2|A|M#02|2|N|M#AW|2|N|M#AZ|16|H|M#AU|2|N|M");
HsmCmdRules.AddOrReplace("CY", "00|2|A|M#AX|-1|H|M#AY|3|N|M#BA|-1|N|M#&;#BB|4|N|M#BC|3|N|M");
HsmCmdRules.AddOrReplace("CZ", "01|2|A|M#02|2|N|M");
HsmCmdRules.AddOrReplace("CW", "00|2|A|M#AA|-1|H|M#AB|-1|N|M#&;#AC|4|N|M#AD|3|N|M");
HsmCmdRules.AddOrReplace("CX", "01|2|A|M#02|2|N|M#AE|3|N|M");
HsmCmdRules.AddOrReplace("DG", "00|2|A|M#BH|32|H|M#AN|5|N|M#AM|12|N|M#BI|1|N|M");
HsmCmdRules.AddOrReplace("DH", "01|2|A|M#02|2|N|M#BJ|4|N|M");
HsmCmdRules.AddOrReplace("FA", "00|2|A|M#AF|-1|H|M#AP|-1|H|M#*;AJ,AI,BD#BD|1|A|O#AI|1|A|O#AJ|1|A|O");
HsmCmdRules.AddOrReplace("FB", "01|2|A|M#02|2|N|M#AR|33|H|M#AK|@AJ1,6:0,16:-1,16|N|M");
HsmCmdRules.AddOrReplace("HC", "00|2|A|M#AF|-1|H|M#*;BE,AI,BD#BE|1|A|O#AI|1|A|O#BD|1|A|O");
HsmCmdRules.AddOrReplace("HD", "01|2|A|M#02|2|N|M#BF|@BEX,33:U,33:-1,32|N|M#BG|@AIX,33:U,33:-1,32|H|M");
HsmCmdRules.AddOrReplace("JA", "00|2|A|M#AM|12|N|M");
HsmCmdRules.AddOrReplace("JB", "01|2|A|M#02|2|N|M#AN|5|N|M");
HsmCmdRules.AddOrReplace("JC", "00|2|A|M#AP|-1|H|M#AO|16|H|M#AT|2|N|M#AM|12|N|M");
HsmCmdRules.AddOrReplace("JD", "01|2|A|M#02|2|N|M#AN|5|N|M");
HsmCmdRules.AddOrReplace("JE", "00|2|A|M#AP|-1|H|M#AO|16|H|M#AT|2|N|M#AM|12|N|M");
HsmCmdRules.AddOrReplace("JF", "01|2|A|M#02|2|N|M#AN|5|N|M");
HsmCmdRules.AddOrReplace("JG", "00|2|A|M#AR|-1|H|M#AT|2|N|M#AM|12|N|M#AN|5|N|M");
HsmCmdRules.AddOrReplace("JH", "01|2|A|M#02|2|N|M#AZ|16|H|M");
HsmCmdRules.AddOrReplace("KA", "00|2|A|M#AF|-1|H|M#AG|2|N|M#*;AH,AJ,AI#AH|1|A|O#AI|1|A|O#AJ|1|A|O");
HsmCmdRules.AddOrReplace("KB", "01|2|A|M#02|2|N|M#AK|@AJ1,6:-1,16|N|M");
HsmCmdRules.AddOrReplace("NG", "00|2|A|M#AM|12|N|M#AN|5|N|M");
HsmCmdRules.AddOrReplace("NH", "01|2|A|M#02|2|N|M#AL|5|L|M#AV|12|N|M");
public static String[] F00 = { "00", "Command code" };
public static String[] F01 = { "01", "Response code" };
public static String[] F02 = { "02", "Error code" };
public static String[] FAA = { "AA", "CVK A / B" };
public static String[] FAB = { "AB", "Primary account number" };
public static String[] FAC = { "AC", "The card expiration date." };
public static String[] FAD = { "AD", "The card service code." };
public static String[] FAE = { "AE", "CVV to be encoded on card.." };
public static String[] FAF = { "AF", "Encrypted key /current,One of the following: ZMK, ZPK, TMK, TPK, PVK or TAK encrypted under the relevant LMK pair..." };
public static String[] FAG = { "AG", "Key type code , The key type identifier: 00 : ZMK 01 : ZPK 02 : TMK, TPK or PVK 03 : TAK" };
public static String[] FAH = { "AH", "Key scheme ZMK 1 A Optional. Key scheme for encrypting key under ZMK" };
public static String[] FAI = { "AI", "Key scheme LMK 1 A Optional. Key scheme for encrypting key under LMK." };
public static String[] FAJ = { "AJ", "Key check value type 1 A Optional. Key check value calculation method" };
public static String[] FAK = { "AK", "Key check value 16 H or 6 H The check value for the given key. Calculated by..." };
public static String[] FAL = { "AL", "PIN The clear text PIN left-justified and padded with X‘F to the encrypted PIN length L." };
public static String[] FAM = { "AM", "Account number The 12 right-most digits of the account number, excluding the check digit." };
public static String[] FAN = { "AN", "PIN The PIN encrypted under LMK pair 02-03." };
public static String[] FAP = { "AP", "Source TPK/ZPK The TPK/ZPK under which the PIN block is currently encrypted or lmk." };
public static String[] FAR = { "AR", "Destination ZPK The ZPK under which the PIN block is to be encrypted or lmk." };
public static String[] FAS = { "AS", "Maximum PIN Length Value '12'." };
public static String[] FAO = { "AO", "Source PIN block The source PIN block encrypted under the source TPK/ZPK." };
public static String[] FAT = { "AT", "Source PIN block format code The format code for the source PIN block " };
public static String[] FAU = { "AU", "Destination PIN block format code The format code for the destination PIN block " };
public static String[] FAV = { "AV", "Reference number The reference number derived by encrypting the accountnumber under the LMK." };
public static String[] FAW = { "AW", "PIN Length Length of the returned PIN." };
public static String[] FAZ = { "AZ", "Destination PIN block The destination PIN block encrypted under the destination ZPK." };
public static String[] FAX = { "AX", "CVK A / B The CVK A / B key, used to verify the supplied CVV/CVC" };
public static String[] FAY = { "AY", "CVV CVV for verification." };
public static String[] FBA = { "BA", "Primary account number for the card." };
public static String[] FBB = { "BB", "Expiration date ,The card Expiration date " };
public static String[] FBC = { "BC", "Service code, The card service code." };
public static String[] FBD = { "BD", "Reserved For all use" };
public static String[] FBE = { "BE", "Key Scheme (TMK) Optional. Key scheme for encrypting key under TMK" };
public static String[] FBF = { "BF", "New key under the current key The new key encrypted under the current key." };
public static String[] FBG = { "BG", "New key under LMK The new key under LMK pair 14-15." };
public static String[] FBH = { "BH", "PVK Pair The two PVKs, used to generate the PVV." };
public static String[] FBI = { "BI", "PVKI The PVKI (should be between '0' and '6')." };
public static String[] FBJ = { "BJ", "PVV result" };
public MyHsmFldVal(String sVal)
public MyHsmFldVal(Byte[] bVal)
public MyHsmFldVal(Int32 iVal)
this.sVal = iVal.ToString();
public class MyHsm : MyHsmCore
public MyHsm(Int32 iTimeOut, Boolean bCloseCnn) : base(iTimeOut, bCloseCnn) { }
public MyHsm(Int32 iTimeOut) : base(iTimeOut, false) { }
public void Config(String sHost, Int32 iPort)
tcp = new MyTcpSndRcv(sHost, iPort);
mHsmInFld = new MyDictionary<String, MyHsmFldVal>();
mHsmOuFld = new MyDictionary<String, MyHsmFldVal>();
public void AddCmd(String sVal)
mHsmInFld.AddOrReplace(MyHsmFldDef.F00[0], new MyHsmFldVal(MyHsmCmdDef.HsmCmd[sVal][0]));
mHsmOuFld.AddOrReplace(MyHsmFldDef.F01[0], new MyHsmFldVal(MyHsmCmdDef.HsmCmd[sVal][1]));
public void AddVal(String sTag, String sVal)
mHsmInFld.AddOrReplace(sTag, new MyHsmFldVal(sVal));
public void AddVal(String sTag, Byte[] bval)
mHsmInFld.AddOrReplace(sTag, new MyHsmFldVal(bval));
ParseHsmMsg(tcp.Process(GenHsmMsg(), iTimeOut, bCloseCnn));
public void SndRcv(Int32 iTxnTimeOut)
ParseHsmMsg(tcp.Process(GenHsmMsg(), iTxnTimeOut, bCloseCnn));
public T GetValue<T>(String sTag)
return (T)Convert.ChangeType(mHsmOuFld[sTag].sVal, typeof(T));
protected Int32 iTimeOut = 5;
protected Boolean bCloseCnn = false;
protected MyDictionary<String, MyHsmFldVal> mHsmInFld;
protected MyDictionary<String, MyHsmFldVal> mHsmOuFld;
private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private String sMsgToHsm = String.Empty;
private String sMsgfromHsm = String.Empty;
private String sHsmMsgInHdr = String.Empty;
private String sHsmMsgOutHdr = String.Empty;
protected MyHsmCore(Int32 iTimeOut, Boolean bCloseCnn)
this.iTimeOut = iTimeOut;
this.bCloseCnn = bCloseCnn;
protected Byte[] GenHsmMsg()
MyHsmCmdFld oField = null;
MyHsmFldVal oFldVal = null;
sHsmMsgInHdr = RandomString(4);
sMsgToHsm = "00" + sHsmMsgInHdr;
foreach (String str in MyHsmCmdFmtDef.HsmCmdRules[mHsmInFld[MyHsmFldDef.F00[0]].sVal].Split('#'))
sMsgToHsm += str.Substring(1);
foreach (String soptTag in str.Substring(2).Split(','))
if (!mHsmInFld.ContainsKey(soptTag))
if (mHsmInFld.ContainsKey(oField.sTag))
oFldVal = mHsmInFld[oField.sTag];
throw new Exception(MethodBase.GetCurrentMethod().Name + " Tag #" + oField.sTag + "# should be necessary ");
if (Array.IndexOf(new Char[] { 'A', 'C', 'H' }, oField.cType) >= 0)
if (oField.iLen != oFldVal.sVal.Length)
throw new Exception(MethodBase.GetCurrentMethod().Name + " Invalid Data");
oField.iLen = oFldVal.sVal.Length;
sMsgToHsm += oFldVal.sVal;
oField.iLen = oFldVal.sVal.Length;
if (Array.IndexOf(new Char[] { 'N' }, oField.cType) >= 0)
sMsgToHsm += oFldVal.sVal.ToString().PadLeft(oField.iLen, '0');
else if (Array.IndexOf(new Char[] { 'L' }, oField.cType) >= 0)
sMsgToHsm += oFldVal.sVal.ToString().PadRight(oField.iLen, 'F');
return Encoding.ASCII.GetBytes(sMsgToHsm);
protected void ParseHsmMsg(Byte[] bOut)
MyHsmCmdFld oField = null;
throw new Exception(MethodBase.GetCurrentMethod().Name + " Response is not present");
sMsgfromHsm = Encoding.ASCII.GetString(bOut, 2, bOut.Length - 2); iBffIdx += 4;
sHsmMsgOutHdr = sMsgfromHsm.Substring(0, 4);
if (sHsmMsgInHdr != sHsmMsgOutHdr)
throw new Exception(MethodBase.GetCurrentMethod().Name + " Response is Crashed");
foreach (String str in MyHsmCmdFmtDef.HsmCmdRules[mHsmOuFld[MyHsmFldDef.F01[0]].sVal].Split('#'))
if (iBffIdx == bOut.Length)
if (Array.IndexOf(new Char[] { 'A', 'C', 'H', 'N', 'L' }, oField.cType) >= 0)
mHsmOuFld.AddOrReplace(oField.sTag, new MyHsmFldVal(Encoding.ASCII.GetString(bOut.xSubArray(iBffIdx, oField.iLen)))); iBffIdx += oField.iLen;
private MyHsmCmdFld GetField(String sText)
String[] sFieldProps = sText.Split('|');
MyHsmCmdFld oField = new MyHsmCmdFld();
oField.sTag = sFieldProps[0];
if (sFieldProps[1][0] == '@')
oField.iLen = GetDependLen(sFieldProps[1].Substring(1));
oField.iLen = Convert.ToInt32(sFieldProps[1]);
oField.cType = sFieldProps[2][0];
oField.bOpt = sFieldProps[3] == "M" ? false : true;
private Int32 GetDependLen(String sText)
String[] rows = sText.Substring(2).Split(':');
String[][] matrix = new String[rows.Length][];
if (mHsmInFld.ContainsKey(sText.Substring(0, 2)))
sKey = mHsmInFld[sText.Substring(0, 2)].sVal;
foreach (String row in rows)
matrix[r] = row.Split(',');
if (matrix[r][0] == sKey)
return Convert.ToInt32(matrix[r][1]);
private string RandomString(int size)
var random = new Random((int)DateTime.Now.Ticks);
char[] buffer = new char[size];
for (int i = 0; i < size; i++)
buffer[i] = chars[random.Next(chars.Length)];
return new string(buffer);
private MySynClientSocket socket;
private String Ip = String.Empty;
public MyTcpSndRcv(String Ip, Int32 iPort)
socket = new MySynClientSocket(Ip, iPort);
public Byte[] Process(Byte[] bInpData, Int32 iTimeOut, Boolean bCloseCnn)
bInpData[0] = (Byte)(((bInpData.Length - 2) >> 8) & 0xFF);
bInpData[1] = (Byte)((bInpData.Length - 2) & 0xFF);
TimeSpan timeSpan1 = DateTime.Now.TimeOfDay;
socket.OpenClientSocket();
socket.SendSynMsg(bInpData);
MyTcpClientObject clObj = null;
while ((DateTime.Now.TimeOfDay - timeSpan1).Seconds < iTimeOut)
clObj = socket.RcvSynMsg();
socket.CloseClientSocket();
public class MySynClientSocket
private TcpClient client;
private NetworkStream stream;
private readonly String IpAddress;
private readonly Int32 Port;
MyTcpClientObject clobj = new MyTcpClientObject();
public bool ConnectedTo { get; private set; }
public MySynClientSocket(string IpAddress, int Port)
this.IpAddress = IpAddress;
public void OpenClientSocket()
client = new TcpClient();
client.Connect(IpAddress, Port);
stream = client.GetStream();
stream.ReadTimeout = 3000;
stream.WriteTimeout = 3000;
throw new Exception(MethodBase.GetCurrentMethod().Name + " " + ex.Message);
public void CloseClientSocket()
throw new Exception(MethodBase.GetCurrentMethod().Name + " " + ex.Message);
public void SendSynMsg(Byte[] Message)
stream.Write(Message, 0, Message.Length);
throw new Exception(MethodBase.GetCurrentMethod().Name + " " + ex.Message);
public MyTcpClientObject RcvSynMsg()
while (stream.DataAvailable)
Byte[] ReadBuff = new Byte[MyIODef.TCP_BUF_LEN];
bytes = stream.Read(ReadBuff, 0, MyIODef.TCP_BUF_LEN);
clobj.Buffer = new Byte[bytes];
Array.Copy(ReadBuff, 0, clobj.Buffer, 0, bytes);
if (client.Client.Poll(0, SelectMode.SelectRead))
if (client.Client.Receive(new byte[1], SocketFlags.Peek) == 0)
throw new Exception(MethodBase.GetCurrentMethod().Name + " " + ex.Message);
public class MyTcpClientObject
public const int TCP_BUF_LEN = 8192;
public const int FTP_BUF_LEN = 2048;
public static class MyExtensions
public static T[] xSubArray<T>(this T[] data, int index, int length)
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);