0

Given that i know that aes-128-cbc was used to generate ciphertext, no salting was used and the IV are all zeros my goal was to write a program to find out the key used to encrypt the plain text (i was also given a dictionary of possible keys used).

So I have implemented the following

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class myClassName123 {

/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    try {
        BufferedReader in = new BufferedReader(new FileReader("/Users/username/Documents/f12/words.txt"));
        String str;
        while ((str = in.readLine()) != null && str.getBytes().length<16) { //clue given in spec

                System.out.println(decrypt(getBytesFromFile(new File("/Users/username/Documents/f21/some.aes-128-cbc")),str));


        }
        in.close();
    } catch (IOException e) {
    }
}

/**
* This method decrypts the input byte [] using AES Key byte [] 
* 
* @param byte []
* @param byte [] 
* @return byte []
* @throws Exception
*/
public static byte[] decrypt(byte[] text, String key) throws Exception {
    Cipher cipher;
    byte[] bytes = null;


    Provider provider = new BouncyCastleProvider();
    MessageDigest digester = MessageDigest.getInstance("SHA-256", provider);
    digester.update(key.getBytes("UTF-8"));
    //byte[] key = digester.digest();
    SecretKeySpec spec = new SecretKeySpec(digester.digest(), "AES");

    //SecretKeySpec spec = new SecretKeySpec(toByteArray(key.toCharArray()), "AES");
     byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);
    try {
            // Instantiate the cipher
            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, spec, ivspec);

             bytes = cipher.doFinal(text);

             String value = new String(bytes, "UTF-8");

                System.out.println("DEBUG HERE: "+value);

    }catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        //  throw new Exception(e);
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    //  throw new Exception(e);
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        //throw new Exception(e);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
        //throw new Exception(e);
    } catch (BadPaddingException e) {
        e.printStackTrace();
        //throw new Exception(e);
    }
    return bytes;
}

// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    long length = file.length();

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}   
}

However when i run this I get the following:

DEBUG HERE: (M��1�����-��e�}�p����+�� [B@4ed1e89e

I think I am not converting to String correctly but I am currently stuck with this. any help is much appreciated, thank you very much.

ssedano
  • 8,322
  • 9
  • 60
  • 98
user1265230
  • 61
  • 1
  • 5

1 Answers1

0

If the problem is that the characters are not being displayed properly, it maybe because of the result of the decryption contains non-printable chars.

Even it the orignal was plaint text it makes sense this behaviour, as you are trying with different keys. If you choose a wrong one, you will probably get this kind of result.

You can also display the string as hex (http://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java), which may make it a bit more readable.

hope it helps

richardtz
  • 4,993
  • 2
  • 27
  • 38
  • thanks for replying. tried with hex but just prints out a series of numbers really so no much more info there. the key is in the dictionary file so i should get at least 1 readable result. Still stuck :S thank you – user1265230 Sep 28 '12 at 13:57