How ciphertext was generated in card reader using DUKPT encryption?
Asked Answered
A

4

8

For

`BDK = "0123456789ABCDEFFEDCBA9876543210"` `KSN = "FFFF9876543210E00008"` 

The ciphertext generated was below

"C25C1D1197D31CAA87285D59A892047426D9182EC11353C051ADD6D0F072A6CB3436560B3071FC1FD11D9F7E74886742D9BEE0CFD1EA1064C213BB55278B2F12"`

which I found here. I know this cipher-text is based on BDK and KSN but how this 128 length cipher text was generated? What are steps involved in it or algorithm used for this? Could someone explain in simple steps. I found it hard to understand the documents I got while googled.

Alienable answered 28/6, 2013 at 10:25 Comment(4)
Do you know the BDK and KSN used by the card reader?Prophecy
What is your question exactly? If you want to know how DUKPT works, surely you can look in the relevant specification? Can you be more specific about what you need that isn't solved by a "RTFM" response.Ziwot
Hi @PaulG,@DuncanJones Yes I know the BDK and KSN used in my card reader. But I'm new to this project. I was totally clueless how the data was encrypted in cardreader and how result ciphertext was generated while swiping? Yes they used DUKPT technique.Alienable
@PaulG,@DuncanJones I edited the question can u provide any links or explain the steps involved?Alienable
D
16

Regarding DUKPT , there are some explanations given on Wiki. If that doesn't suffice you, here goes some brief explanation.

Quoting http://www.maravis.com/library/derived-unique-key-per-transaction-dukpt/

What is DUKPT?

Derived Unique Key Per Transaction (DUKPT) is a key management scheme. It uses one time encryption keys that are derived from a secret master key that is shared by the entity (or device) that encrypts and the entity (or device) that decrypts the data. Why DUKPT? Any encryption algorithm is only as secure as its keys. The strongest algorithm is useless if the keys used to encrypt the data with the algorithm are not secure. This is like locking your door with the biggest and strongest lock, but if you hid the key under the doormat, the lock itself is useless. When we talk about encryption, we also need to keep in mind that the data has to be decrypted at the other end. Typically, the weakest link in any encryption scheme is the sharing of the keys between the encrypting and decrypting parties. DUKPT is an attempt to ensure that both the parties can encrypt and decrypt data without having to pass the encryption/decryption keys around. The Cryptographic Best Practices document that VISA has published also recommends the use of DUKPT for PCI DSS compliance.

How DUKPT Works

DUKPT uses one time keys that are generated for every transaction and then discarded. The advantage is that if one of these keys is compromised, only one transaction will be compromised. With DUKPT, the originating (say, a Pin Entry Device or PED) and the receiving (processor, gateway, etc) parties share a key. This key is not actually used for encryption. Instead, another one time key that is derived from this master key is used for encrypting and decrypting the data. It is important to note that the master key should not be recoverable from the derived one time key. To decrypt data, the receiving end has to know which master key was used to generate the one time key. This means that the receiving end has to store and keep track of a master key for each device. This can be a lot of work for someone that supports a lot of devices. A better way is required to deal with this. This is how it works in real-life: The receiver has a master key called the Base Derivation Key (BDK). The BDK is supposed to be secret and will never be shared with anyone. This key is used to generate keys called the Initial Pin Encryption Key (IPEK). From this a set of keys called Future Keys is generated and the IPEK discarded. Each of the Future keys is embedded into a PED by the device manufacturer, with whom these are shared. This additional derivation step means that the receiver does not have to keep track of each and every key that goes into the PEDs. They can be re-generated when required.

enter image description here

The receiver shares the Future keys with the PED manufacturer, who embeds one key into each PED. If one of these keys is compromised, the PED can be rekeyed with a new Future key that is derived from the BDK, since the BDK is still safe.

Encryption and Decryption

When data needs to be sent from the PED to the receiver, the Future key within that device is used to generate a one time key and then this key is used with an encryption algorithm to encrypt the data. This data is then sent to the receiver along with the Key Serial Number (KSN) which consists of the Device ID and the device transaction counter. enter image description here

Based on the KSN, the receiver then generates the IPEK and from that generates the Future Key that was used by the device and then the actual key that was used to encrypt the data. With this key, the receiver will be able to decrypt the data.

Source

Dicotyledon answered 7/7, 2013 at 17:16 Comment(0)
Y
5

First, let me quote the complete sourcecode you linked and of which you provided only 3 lines...

require 'bundler/setup'
require 'test/unit'
require 'dukpt'

class DUKPT::DecrypterTest < Test::Unit::TestCase

      def test_decrypt_track_data
        bdk = "0123456789ABCDEFFEDCBA9876543210"
        ksn = "FFFF9876543210E00008"
        ciphertext = "C25C1D1197D31CAA87285D59A892047426D9182EC11353C051ADD6D0F072A6CB3436560B3071FC1FD11D9F7E74886742D9BEE0CFD1EA1064C213BB55278B2F12"
        plaintext = "%B5452300551227189^HOGAN/PAUL ^08043210000000725000000?\x00\x00\x00\x00"

        decrypter = DUKPT::Decrypter.new(bdk, "cbc")
        assert_equal plaintext, decrypter.decrypt(ciphertext, ksn)
      end
end

Now, you're asking is how the "ciphertext" was created...

Well, first thing we know is that it is based on "plaintext", which is used in the code to verify if decryption works.

The plaintext is 0-padded - which fits the encryption that is being tested by verifying decryption with this DecrypterTest TestCase.

Let's look at the encoding code then...

I found the related encryption code at https://github.com/Shopify/dukpt/blob/master/lib/dukpt/encryption.rb.

As the DecrypterTEst uses "cbc", it becomes apparent that the encrypting uses:

 @cipher_type_des = "des-cbc"
 @cipher_type_tdes = "des-ede-cbc"

A bit more down that encryption code, the following solves our quest for an answer:

ciphertext = des_encrypt(...

Which shows we're indeed looking at the result of a DES encryption.

Now, DES has a block size of 64 bits. That's (64/8=) 8 bytes binary, or - as the "ciphertext" is a hex-encoded text representation of the bytes - 16 chars hex.

The "ciphertext" is 128 hex chars long, which means it holds (128 hex chars/16 hex chars=) 8 DES blocks with each 64 bits of encrypted information.

Wrapping all this up in a simple answer:

When looking at "ciphertext", you are looking at (8 blocks of) DES encrypted data, which is being represented using a human-readable, hexadecimal (2 hex chars = 1 byte) notation instead of the original binary bytes that DES encryption would produce.

As for the steps involved in "recreating" the ciphertext, I tend to tell you to simply use the relevant parts of the ruby project where you based your question upon. Simply have to look at the sourcecode. The file at "https://github.com/Shopify/dukpt/blob/master/lib/dukpt/encryption.rb" pretty much explains it all and I'm pretty sure all functionality you need can be found at the project's GitHub repository. Alternatively, you can try to recreate it yourself - using the preferred programming language of your choice. You only need to handle 2 things: DES encryption/decryption and bin-to-hex/hex-to-bin translation.

Yonit answered 7/7, 2013 at 17:11 Comment(0)
H
2

Since this is one of the first topics that come up regarding this I figured I'd share how I was able to encode the ciphertext. This is the first time I've worked with Ruby and it was specifically to work with DUKPT

First I had to get the ipek and pek (same as in the decrypt) method. Then unpack the plaintext string. Convert the unpacked string to a 72 byte array (again, forgive me if my terminology is incorrect).

I noticed in the dukpt gem author example he used the following plain text string

"%B5452300551227189^HOGAN/PAUL ^08043210000000725000000?\x00\x00\x00\x00"

I feel this string is incorrect as there shouldn't be a space after the name (AFAIK).. so it should be

"%B5452300551227189^HOGAN/PAUL^08043210000000725000000?\x00\x00\x00\x00"

All in all, this is the solution I ended up on that can encrypt a string and then decrypt it using DUKPT

class Encrypt
include DUKPT::Encryption
attr_reader :bdk

def initialize(bdk, mode=nil)
  @bdk = bdk
  self.cipher_mode = mode.nil? ? 'cbc' : mode
end

def encrypt(plaintext, ksn)
  ipek = derive_IPEK(bdk, ksn)
  pek = derive_PEK(ipek, ksn)
  message =  plaintext.unpack("H*").first
  message = hex_string_from_unpacked(message, 72)
  encrypted_cryptogram = triple_des_encrypt(pek,message).upcase
  encrypted_cryptogram
end
def hex_string_from_unpacked val, bytes
  val.ljust(bytes * 2, "0")
end

end

boomedukpt FFFF9876543210E00008 "%B5452300551227189^HOGAN/PAUL^08043210000000725000000?"

(my ruby gem, the KSN and the plain text string)

2542353435323330303535313232373138395e484f47414e2f5041554c5e30383034333231303030303030303732353030303030303f000000000000000000000000000000000000

(my ruby gem doing a puts on the unpacked string after calling hex_string_from_unpacked)

C25C1D1197D31CAA87285D59A892047426D9182EC11353C0B82D407291CED53DA14FB107DC0AAB9974DB6E5943735BFFE7D72062708FB389E65A38C444432A6421B7F7EDD559AF11

(my ruby gem doing a puts on the encrypted string)

%B5452300551227189^HOGAN/PAUL^08043210000000725000000?

(my ruby gem doing a puts after calling decrypt on the dukpt gem)

Hazen answered 19/6, 2015 at 9:30 Comment(0)
K
0

Look at this: https://github.com/sgbj/Dukpt.NET, I was in a similar situation where i wondered how to implement dukpt on the terminal when the terminal has its own function calls which take the INIT and KSN to create the first key, so my only problem was to make sure the INIT key was generated the same way on the terminal as it is in the above mentioned repo's code, which was simple enough using ossl encryption library for 3des with ebc and applying the appropriate masks.

Kynewulf answered 23/12, 2015 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.