How ciphertext was generated in card reader using DUKPT encryption?

后端 未结 4 831
眼角桃花
眼角桃花 2021-02-02 03:17

For

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

The ciphertext generated was below

\"C25C1D         


        
4条回答
  •  太阳男子
    2021-02-02 03:32

    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)

提交回复
热议问题