.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; }
Posted in BizTalk, C#. 2 Comments »

2 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….

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: