Search code examples
c#c++encryptiondestripledes

How many keys does Triple DES encryption need?


I am porting some C# code to C++, and trying to encrypt a textfile with Triple DES encryption. But I am confused; some encryption APIs only require one key for Triple DES (C# for example: How to implement Triple DES in C# (complete example) ), while others require 2 or 3 keys (in several C++ implementations I've found).

Why is that?


Solution

  • The TDEA keying is maybe better understood here considering the key length over just a simple key. Depending on the keying option used, it can be a single key length, double key length, or triple key length. All parts are required and would constitute the "key bundle".

    The TDEA is basically three applications of the DES cipher. Each part of the "key bundle" is used with one or more executions of the DES cipher algorithm (see also the Feistel cipher);

    • for a single key, it is used three times (equates to classic DES but is no longer recommended), K1 = K2 = K3;
    • for a double key, the first part of the key is used twice, K1 and K2 are independent and K3 = K1;
    • and for the triple key length, each key part is used once, all parts are independent.

    What you are seeing as "two" (or "three") keys is most likely the double (or triple) key length being used, each part being provided separately.

    The documentation for each API should provide details on how the keys are provided/expected.

    A few test cases to check interoperability never hurt either.


    Some background/context on how TDEA works; source Wikipedia;

    Triple DES uses a "key bundle" that comprises three DES keys, K1, K2 and K3, each of 56 bits (excluding parity bits)...

    The encryption algorithm is:

    ciphertext = EK3(DK2(EK1(plaintext)))

    I.e., DES encrypt with K1, DES decrypt with K2, then DES encrypt with K3.

    Decryption is the reverse:

    plaintext = DK1(EK2(DK3(ciphertext)))

    I.e., decrypt with K3, encrypt with K2, then decrypt with K1.

    Each triple encryption encrypts one block of 64 bits of data.