Hello,
I have the following code which I am using to decode a base64encoded string which was encoded also using an AES cipher.
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.*;
public class EncryptionHelper {
private String algorithm = "AES";
private SecretKeySpec keySpec = null;
private byte[] key = "Wydopqswrlmesf==".getBytes();
private Cipher cipher = null;
public EncryptionHelper() throws NoSuchAlgorithmException, NoSuchPaddingException{
cipher = Cipher.getInstance(algorithm);
keySpec = new SecretKeySpec(key, algorithm);
}
public String decrypt(String passphrase) throws InvalidKeyException,BadPaddingException, IllegalBlockSizeException, IOException {
byte[] encryptionBytes = org.apache.commons.codec.binary.Base64.decodeBase64(passphrase);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes);
return recovered;
}
}
I receive an error though as follows
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at com.avaya.utility.updater.EncryptionHelper.decrypt(EncryptionHelper.java:56)
at com.avaya.utility.updater.Controller.main(Controller.java:92)
The key I've provided in "private byte[] key = "Wydopqswrlmesf==".getBytes();" is just a sample key I've added in this post, not the actual key.
Can any of you see where my errors may be occuring? the string I pass in is 16 characters in length.
Thanks in advance.
Open in new window