简体   繁体   English

如何在 C# 中拆分 3DES 密钥并计算 KCV?

[英]How to split 3DES key and calculate KCV in C#?

I have a 128 bit 3DES key 1915372928A30803A25B0659A4DD6525, how could I split the key into 3 components and calculate the KCV for each component?我有一个 128 位 3DES 密钥 1915372928A30803A25B0659A4DD6525,我如何将密钥分成 3 个组件并计算每个组件的 KCV? 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 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我用来生成 3DES 密钥的代码

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?拿到key后,如何将key拆分为3个部分,计算出KCV?

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).以与您现在相同的方式生成两个单独的 DES 128 位密钥,它们是组件 1 和 2。然后将这些密钥与您当前的(主密钥)一起进行异或。 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.要计算 KCV,只需使用生成的组件来加密设置为零的 8 字节块。 You can use ECB mode or CBC mode (without padding) if a direct block encrypt is not available.如果直接块加密不可用,您可以使用 ECB 模式或 CBC 模式(无填充)。 For CBC you need to set the IV to all zeros as well.对于 CBC,您还需要将 IV 设置为全零。 Then take the leftmost bytes of the result and encode as hexadecimals.然后取结果的最左边的字节并编码为十六进制。

1- to calculate the KCV you have to encrypt 16 bytes of zero's with generated key 1- 要计算 KCV,您必须使用生成的密钥加密 16 个字节的零

(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): 2- 将密钥拆分为 3 个组件从密钥404142434445464748494A4B4C4D4E4F开始创建 2 个相同长度的随机数(在此示例中为 16 个字节):

Rand 1 : 988A59D7273186B8C9C9922B6D40BA75 and Rand 2: 8936E5269ADFABE7D4829B2EFB3BF5D9 (the random numbers will become Component1 and Component2) now XOR the 3 numbers. Rand 1 : 988A59D7273186B8C9C9922B6D40BA75Rand 2: 8936E5269ADFABE7D4829B2EFB3BF5D9 (随机数将成为 Component1 和 Component2)现在对 3 个数字进行异或。 ie XOR Key1, Component1 and Component2 together:即对 Key1、Component1 和 Component2 进行异或运算:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM