1

I have a 128 bit 3DES key 1915372928A30803A25B0659A4DD6525, how could I split the key into 3 components and calculate the KCV for each component? I'd like to do similarly to the online tool below

https://www.emvlab.org/keyshares/?combined=1915372928A30803A25B0659A4DD6525&combined_kcv=2082A4&one=B9FFAF926385DBED0FBC087F5DC674C3&one_kcv=C69561&two=EA3CD5B063E0BF73F6C5ECB5F7D32080&two_kcv=33D908&three=4AD64D0B28C66C9D5B22E2930EC83166&three_kcv=03DCA8&numcomp=three&parity=ignore&action=Generate+128+bit

The code i used to generate 3DES key

public byte[] GenerateThreeDesKey()
{
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    byte[] tripleDesKey = new byte[16];
    rng.GetBytes(tripleDesKey);
    for (var i = 0; i < tripleDesKey.Length; ++i)
    { 
        int keyByte = tripleDesKey[i] & 0xFE; 
        var parity = 0; 
        for (int b = keyByte; b != 0; b >>= 1) 
            parity ^= b & 1;
        tripleDesKey[i] = (byte)(keyByte | (parity == 0 ? 1 : 0)); 
    }
    return tripleDesKey;
}

After getting the key, how to split the key into 3 components and calculate the KCV?

Asvey
  • 11
  • 3

2 Answers2

1

Generate two separate DES 128 bit keys the same way as you are doing now, these are components 1 and 2. Then XOR these keys together with your current (master key). The result is the third component. You can adjust the parity of that key as well if you want.

To calculate the KCV's, simply use the generated components to encrypt a block of 8 bytes set to zero. You can use ECB mode or CBC mode (without padding) if a direct block encrypt is not available. For CBC you need to set the IV to all zeros as well. Then take the leftmost bytes of the result and encode as hexadecimals.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • Thanks Maarten, I tried the codes below and the KC1, KC2, and KC3 does not look correct as the EMVLab tool above generate ` byte[] tripleDesKey = GenerateThreeDesKey(); byte[] KC1 = GenerateThreeDesKey(); byte[] KC2 = GenerateThreeDesKey(); byte[] xor1 = exclusiveOR(tripleDesKey, KC1); byte[] KC3 = exclusiveOR(xor1, KC2); byte[] exclusiveOR(byte[] arr1, byte[] arr2) { byte[] result = new byte[arr1.Length]; for (int i = 0; i < arr1.Length; ++i) result[i] = (byte)(arr1[i] ^ arr2[i]); return result; } ` – Asvey Apr 09 '17 at 01:54
  • never mind, the codes above seem to work. The KC1, KC2, and KC3 iare randomly generated so they cannot be the same as the one that EMVLab generates. Thanks – Asvey Apr 09 '17 at 02:01
0

1- to calculate the KCV you have to encrypt 16 bytes of zero's with generated key

(data: 00000000000000000000000000000000, key: 404142434445464748494A4B4C4D4E4F) = 8BAF473F2F8FD0948BAF473F2F8FD094 (last three bytes is KCV (8BAF47))

2- to split key into 3 component Start with the Key 404142434445464748494A4B4C4D4E4F create 2 random number of the same length (16 bytes in this example):

Rand 1 : 988A59D7273186B8C9C9922B6D40BA75 and Rand 2: 8936E5269ADFABE7D4829B2EFB3BF5D9 (the random numbers will become Component1 and Component2) now XOR the 3 numbers. i.e. XOR Key1, Component1 and Component2 together:

XOR(0123456789ABCDEFFEDCBA9876543210, 988A59D7273186B8C9C9922B6D40BA75, 8936E5269ADFABE7D4829B2EFB3BF5D9) = 109FF9963445E0B0E397B39DE02F7DBC (the result will be key Component3)
EngBashir
  • 21
  • 4