karthik selvaraj
karthik selvaraj

Reputation: 499

AES/CFB decryption

i am trying to decrypt using AES/CFB mode by using below code,

final static public String ENCRYPT_KEY = "4EBB854BC67649A99376A7B90089CFF1";
final static public String IVKEY = "ECE7D4111337A511F81CBF2E3E42D105";
private static String deCrypt(String key, String initVector, String encrypted) {
    try {
       IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skSpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
        int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");

        Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, skSpec, iv);
        byte[] original = cipher.doFinal(encrypted.getBytes());

        return new String(original);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

and throw below error,

Wrong IV length: must be 16 bytes long.

Above ENCRYPT_KEY and IVKEY are valid one. can any one help in that ?

Upvotes: 0

Views: 853

Answers (1)

Kayaman
Kayaman

Reputation: 73558

You're calling "ECE7D4111337A511F81CBF2E3E42D105".getBytes("UTF-8"); which will result in byte[] of size 32, not to mention a completely wrong IV.

You need to parse the String into a byte[] instead, for example by borrowing the DatatypeConverter from javax.xml.bind.

IvParameterSpec iv = new IvParameterSpec(
    javax.xml.bind.DatatypeConverter.parseHexBinary(initVector));

Upvotes: 2

Related Questions