.NET Integration with HSM

Recently had a scenario where we need to encrypt Card PIN code with keys provided by HSM. In our case, HSM was using TripleDES symmetric key algorithm for encryption and decryption. First, I tried with .NET Framework TripleDESCryptoServiceProvider, but that was not working.

After lots of trial and error, the solution was using Thales Simulator Library. Following is sample code for encrypting PIN with ZPK (Zone Private Key). ZPK was communicated between parties in encrypted format and we need to decrypt it first using ZMK (Zone Master key)

 

public static string GetEncryptedPINwithZPK(string CardNumber, string PIN)
{

         HexKey key = new HexKey("ZMK");
         string ZPK = ThalesSim.Core.Cryptography.TripleDES.TripleDESDecrypt(key, "EncryptedZPK");
            
         string PINBlock1 = ("0" + PIN.Length + PIN).PadRight(16, 'F');
         string CNumber = CardNumber.Substring(0, CardNumber.Length - 1); // to skip right most check digit
         string PINBlock2 = "0000" + CNumber.Substring(CNumber.Length - 12);

         HexKey ZPKHex = new HexKey(ZPK);
         string EncryptedPIN =  ThalesSim.Core.Cryptography.TripleDES.TripleDESEncrypt
(ZPKHex, XORStrings(PINBlock1, PINBlock2)); return EncryptedPIN; }
Advertisements

11 Responses to “.NET Integration with HSM”

  1. | Says:

    It is definitely a very interesting library specially revealing the internals of an HSM; but it does not implement HSM fully, for instance PA-Load Formatting Data whose sole implementation task is to respond to the host application, or commands that have to do with the IBM verification method. http://izlooite.blogspot.com/2009/03/hsm-simulator.html

  2. dipesh Says:

    Dear Usman Shaheen,

    Thank you so much for your clear information on HSM..I just want to ask you that what if there are 2 ZMK, ZMK and ZMK2 and 1 encrypted ZPK..On such scenario how to get the clear ZPK from ZMK1 and ZMK2.

    Please reply….

  3. Kamran Shahid Says:

    What is XORStrings method. Not found in core C# and nuget packages

  4. Kamran Shahid Says:

    I were able to do it using

    public static string XORStrings(string key, string input)
    {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < input.Length; i++)
    sb.Append((char)(input[i] ^ key[(i % key.Length)]));
    String result = sb.ToString();

    return result;
    }

    But sample didn't work
    My tried code is

    private void button1_Click(object sender, EventArgs e)
    {
    var test = GetEncryptedPINwithZPK("1234567890123456", "1234");
    }

    public static string GetEncryptedPINwithZPK(string CardNumber, string PIN)
    {

    HexKey key = new HexKey("838652DF68A246046DAB6104583B201A");
    string ZPK = ThalesSim.Core.Cryptography.TripleDES.TripleDESDecrypt(key, "838652DF68A246046DAB6104583B201A");

    string PINBlock1 = ("0" + PIN.Length + PIN).PadRight(16, 'F');
    string CNumber = CardNumber.Substring(0, CardNumber.Length – 1); // to skip right most check digit
    string PINBlock2 = "0000" + CNumber.Substring(CNumber.Length – 12);

    HexKey ZPKHex = new HexKey(ZPK);
    string EncryptedPIN = ThalesSim.Core.Cryptography.TripleDES.TripleDESEncrypt(ZPKHex, XORStrings(PINBlock1, PINBlock2));

    return EncryptedPIN;
    }

    Thanks

  5. Sajid Khan Says:

    @Usman bhai,
    This could have saved me a week worth of time if I had found it earlier 🙂 I literally went through the trouble of writing every single piece via C# 😦

    Great Read 🙂

  6. Sajid Khan Says:

    @Kamran Shahid,follow following steps:
    //// create string Extensions methods
    public static class Extensions
    {
    public static byte[] HexToByte(this string hexString)
    {
    // After the operation of the byte length: 16 carry the number of string length / 2
    byte[] byteOUT = new byte[hexString.Length / 2];
    for (int i = 0; i < hexString.Length; i = i + 2)
    {
    // Each bit 2 digit is converted to a 10 carry integer
    byteOUT[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
    }
    return byteOUT;
    }

    public static string BToHex(this byte[] Bdata)
    {
    return BitConverter.ToString(Bdata).Replace("-", "");
    }

    // Remove the specified number of characters starting from the right side of the string
    public static string Right(this string str, int len)
    {
    return str.Substring(str.Length – len, len);
    }

    //Remove the specified number of characters from the right side of the string (skip a few characters)
    public static string Right(this string str, int len, int skiplen)
    {
    return str.Substring(str.Length – len – skiplen, len);
    }
    }
    //Posting Part 2…

    • Sajid Khan Says:

      //// Part 2, Note: This PIN Block complies with ISO-0 Standards only
      public class SSM
      {
      private static byte[] XOR(byte[] bHEX1, byte[] bHEX2)
      {
      byte[] bHEX_OUT = new byte[bHEX1.Length];
      for (int i = 0; i < bHEX1.Length; i++)
      {
      bHEX_OUT[i] = (byte)(bHEX1[i] ^ bHEX2[i]);
      }
      return bHEX_OUT;
      }

      public static string GenPinBlock(string CardNumber, string PIN, string ZMK, string encryptedZPK) //keyA, string KeyB)
      {
      string PinPrepare = string.Format("{0}{1}{2}{3}", "0", PIN.Length.ToString("X"), PIN, "".PadRight(16 – 2 – PIN.Length, 'F'));

      string CardNumberPrepare = string.Format("{0}{1}", "0000", CardNumber.Right(12, 1));
      // 3. Perform XOR operations
      byte[] ClearPinBlockByte = XOR(PinPrepare.HexToByte(), CardNumberPrepare.HexToByte());

      HexKey ZMK_Key = new HexKey(ZMK);
      string ZPK = ThalesSim.Core.Cryptography.TripleDES.TripleDESDecrypt(ZMK_Key, encryptedZPK);

      HexKey ZPKHex = new HexKey(ZPK);
      string PIN_Block = ThalesSim.Core.Cryptography.TripleDES.TripleDESEncrypt(ZPKHex, ClearPinBlockByte.BToHex());
      return PIN_Block;
      }
      }

      Enjoy Coding 🙂

  7. Kamran Shahid Says:

    Thanks a lot Sajid.
    Now checking how to get sample string ZMK, string encryptedZPK
    Can i generate it via Thales simulator?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: