0

i try to decrypt a encrypted text and use this code but get this error to me:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size

and decryption code is:

String key = "ffce885876a617e7";
    String vector = "9ee153a3df56965e7baf13a7fa1075cc";


    IvParameterSpec ivSpec = new IvParameterSpec(key.getBytes());
    SecretKeySpec keySpec = new SecretKeySpec(vector.getBytes(), "AES");


    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); //error occured in this line
0

2 Answers 2

1

.getBytes() will not automagically convert a "hex string" to the matching bytes.

Instead, try this utility method:

private static byte[] hexStringToBytes(final String input)
{
    final int len = input.length();
    if (len % 2 != 0)
        throw new IllegalArgumentException();

    final byte[] ret = new byte[len / 2];
    int offset = 0;

    for (int i = 0; i < ret.length; i++) {
        ret[i] = (byte) Integer.parseInt(input.substring(offset, offset+2), 16);
        offset += 2;
    }

    return ret;
}

Then in your code use hexStringToBytes(key) etc.

Sign up to request clarification or add additional context in comments.

5 Comments

now, get this err.<br/>Exception in thread "main" java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long
i use my method o i write in above in android and work as well, but i don't know why doesn't work in java console!
@ArazJafaripur Perhaps the security provider in Android is more forgiving. Either way, you must pass a sixteen byte IV value. Is it a mistake that you make a key from vector and an IV from key?
android use standard java, any way now how to decrypt with my key and vector with AES, this code must to correctly run but get error!
Hmm, what do you call "standard Java"? Android uses its own VM, for one
0

I uses this one to decrypt AES. But in Android env instead of Java vanila.

public String decrypt(String enc) throws Exception {
    try {
        String key = "ffce885876a617e7";
        String vector = "9ee153a3df56965e7baf13a7fa1075cc";

        IvParameterSpec ivSpec = new IvParameterSpec(key.getBytes());
        SecretKeySpec keySpec = new SecretKeySpec(vector.getBytes(),
                "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

        return new String(cipher.doFinal(hexToByte(enc)), "UTF-8");

    } catch (Exception localException) {
        throw new Exception("[decrypt] " + localException.getMessage());
    }
}

private static byte[] hexToByte(String enc) {
    int j = enc.length() / 2;
    byte[] arrayOfByte = new byte[j];

    if (j >= 2) {
        for (int i = 0; i < j; i++) {
            arrayOfByte[i] = ((byte) Integer.parseInt(
                    enc.substring(i * 2, 2 + i * 2), 16));
        }
    }
    return arrayOfByte;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.