OntologicalSin
OntologicalSin

Reputation: 144

javax.crypto.IllegalBlockSizeException upon decryption

the current decryption algorithm I wrote goes as follows,

    public String decrypt(String enc) throws Exception
    {
        Key key = k;
        Cipher crypt = Cipher.getInstance("AES");
        crypt.init(Cipher.DECRYPT_MODE,key);
        byte[] decrypt = crypt.doFinal(enc.getBytes());
        return new String(decrypt);
    }

The error that I get is at the line

 byte[] decrypt = crypt.doFinal(enc.getBytes());

I have seen similar questions as this posted, but I am using a 128 bit key, so I am pretty certain there is no padding.

This is how I generate the key

  public static SecretKey getKey() throws Exception
  {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128); 
    return keyGen.generateKey();
  }

Additionally, decoding using base64 gives the same exact error

    public String decrypt(String enc) throws Exception
    {
        Key key = k;
        Cipher crypt = Cipher.getInstance("AES");
        crypt.init(Cipher.DECRYPT_MODE,key);
        byte[] decrypt = crypt.doFinal(Base64.getMimeDecoder().decode(enc));
        return new String(decrypt);
    }

Upvotes: 0

Views: 56

Answers (1)

user207421
user207421

Reputation: 311023

public String decrypt(String enc)

The problem has already happened by the time you get here. The problem is that you are passing around ciphertext in a String. String isn't a container for binary data. Use a byte[].

Upvotes: 1

Related Questions