AES-256 decryption with part of a key
<p>This is homework. Our professor encrypted a message with AES-256 in CBC mode (key is 256bits). Then he gave us the IV he used (<code>randomized</code> in my code), the encrypted message and second half of a key (<code>keySuffix</code>), key is in hexadecimal, IV too. We have to use brute-force and decrypt that message. The message is a few blocks of length 8 made of ones and zeroes with spaces in between (<code>cryptogram</code>). What I do is getting rid of the spaces, transforming cryptogram to <code>byteArray</code> (you can see in code how I do that). On the key I just do <code>getBytes()</code>, on IV I use <code>DatatypeConverter.parseHexBinary()</code> otherwise it's throwing an error about wrong IV length. When I get a message i return it as a String by using <code>new String(myByteArray)</code>. Then I check if that message has all the normal characters (not some 'bushes').</p>

<p>Point is that I get no results. I have no idea what may be wrong here, my guess is that some conversions are done not the way they were supposed to be. I've been trying some things with unicodes etc., but brute-force takes so long time to complete and testing like this is troublesome. Can someone point my in the right direction, like how to make those conversions. Generating all possible Strings for the other side of the key works fine.</p>

<p>btw. PKCS5Padding is giving an error.</p>

<pre class="lang-java prettyprint-override"><code>public class Main {
    private String randomized = "21232d0960a7b522d3e25141e54ecee6";
    private String keySuffix = "1dad418a";
    private String cryptogram = "00110001 01111000 01111101 01111100 01100001 11011110 10010010 01011011";
    private byte[] cryptogramBytes;
    private String pattern = "[a-zA-Z1-9\\s]*";
    private IvParameterSpec ivSpec = null;
    private Cipher cipher = null;

    public static void main(String... args){
        char[] elements = { 'a', 'b', 'c', 'd', 'e', 'f', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
        char[] buff = new char[8];
        Main main = new Main ();
        byte[] convertedRandomized = DatatypeConverter.parseHexBinary(main.randomized);
        main.ivSpec = new IvParameterSpec(convertedRandomized);
        main.cryptogram = main.cryptogram.replaceAll("\\s", "");
        BigInteger bigint = new BigInteger(main.cryptogram, 2);
        main.cryptogramBytes = bigint.toByteArray();
        main.cipher = Cipher.getInstance("AES/CBC/NoPadding");
        main.permGen(elements, 0, 8, buff);
    }

    public void permGen(char[] s, int i, int k, char[] buff){
        if (i &lt; k) {
            for (int j = 0; j &lt; s.length; j++) {
                buff[i] = s[j];
                permGen(s, i + 1, k, buff);
            }
        } else {
            String result = decrypt(String.valueOf(buff) + keySuffix);
            if (result.matches(pattern))
                System.out.println("Key is: " + String.valueOf(buff) + keySuffix);
        }
    }

    public String decrypt(String key){
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
        return new String(cipher.doFinal(cryptogramBytes));
    }
}
</code></pre>