Search code examples
javacryptographyencryptiondes

finding a key for DES


working on a problem of encrypyting and decrypting using the DES given in java. Ive already figured out how to encrypt and decrypt pretty easy but now im stuck. For the current problem i am having I have the plaintext and the coorisponding cipher text (which is in the format of 8 hex pairs ex: A5 33 1F ..) but i also have the first 4 hexidecimal bits of the key. Im not really asking for code but more of an idea how i would go about tackling this problem! anything will help! this is my decryption code (just included it to show i am workin hard :) ). thanks guys!

public static void decrypt(){
    Cipher cipher;
    SecretKeySpec key;
    byte [] keyBytes;
    byte [] pt;
    byte [] ct;
    String plaintxt;

    keyBytes = new byte [] {(byte)0xFE, (byte)0xDC, (byte)0xBA, (byte)0x98, (byte)0x76, (byte)0x54, (byte)0x32, (byte)0x10};
    key = new SecretKeySpec(keyBytes, "DES");
    ct = new byte [] {(byte) 0x2C, (byte) 0xE6, (byte) 0xDD, (byte) 0xA4, (byte) 0x98, (byte) 0xCA, (byte) 0xBA, (byte) 0xB9};

    try{
        cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        pt = cipher.doFinal(ct);
        printByteArray(pt);
        plaintxt = byteToHex(pt);
        hexToAscii(plaintxt);
    }
    catch(Exception e){
        e.printStackTrace();
    }

}

Solution

  • Brute force.

    Enumerate over every key that it could be (given the fixed bytes) until you get a decryption that makes the plaintext and ciphertext match. It'll take edit: 2^37 attempts on average, though, so don't expect it to happen fast :)

    There are some properties of DES that let you crack it faster, but they're very difficult to implement and I doubt you'd be expected to learn them. But if you are interested, http://en.wikipedia.org/wiki/Data_Encryption_Standard#Security_and_cryptanalysis