Java - Converting string into DES key

kouri picture kouri · Aug 11, 2011 · Viewed 8.7k times · Source

I have been given a key as a string and an encrypted file using DES. That is all I know. I don't know how the key was encoded.

There is also a des.exe that I can use to decrypt, this is all I found on the Internet: http://knowledge-republic.com/CRM/2011/07/how-to-decrypt-extract-recreate-thecus-storage-firmware/

Using des.exe, the only command it works with is "-D", not "-d".

My goal is to use Java to do the same thing. I copied and pasted this from somewhere

    String key = "blah";
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = skf.generateSecret(dks);
    System.out.println(desKey);

    Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE

    if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }

and it doesn't work.

What are some other options in converting a string into a key?

Should probably add I'm a complete newb at cryptography.

Answer

senecaso picture senecaso · Aug 11, 2011

The SunOS man page for des (which seems to be what your des.exe is based on?) indicates that they key is generated like this:

The DES algorithm requires an 8 byte key whose low order bits are assumed to be odd-parity bits. The ASCII key supplied by the user is zero padded to 8 bytes and the high order bits are set to be odd-parity bits. The DES algorithm then ignores the low bit of each ASCII character, but that bit's information has been preserved in the high bit due to the parity.

It also mentions that the initial IV is always zero'd out, no matter what mode you are running in

The CBC mode of operation always uses an initial value of all zeros for the initialization vector, so the first 8 bytes of a file are encrypted the same whether in CBC or ECB mode.

It also mentions that the padding used is such that the last byte is always a value from 0-7, indicating the number of padding bytes used. This is similar to PKCS5Padding, so perhaps that would work

Since the CBC and ECB modes of DES require units of 8 bytes to be encrypted, files being encrypted by the des command have 1 to 8 bytes appended to them to cause them to be a multiple of 8 bytes. The last byte, when decrypted, gives the number of bytes (0 to 7) which are to be saved of the last 8 bytes. The other bytes of those appended to the input are randomized before encryption.

Based on the options you indicated you are using, it sounds like you are using DES/CBC/PKCS5Padding for the cipher.

I think that just leaves determining how to actually derive the key. I found this sample code on exampledepot which might work for you. I think you would just need to convert your string password into 8 bytes (1 byte per character, so no UTF encodings) then stuff it through the code in the example to derive the key. Its worth a shot anyway.