java - AES Encryption Input Length Error javax.crypto.IllegalBlockSizeException -
this code:
public class encryption { private static final string algo = "aes/cbc/pkcs5padding"; private static final byte[] keyvalue = new byte[]{'f','o','r','t','y','t','w','o','f','o','r','t','y','t','w','o'}; private static key key = generatekey(); public static string encrypt(string data) throws exception { cipher c = cipher.getinstance(algo); c.init(cipher.encrypt_mode,key,new ivparameterspec(c.getparameters().getparameterspec(ivparameterspec.class).getiv())); byte[] encval = c.dofinal(data.getbytes()); string encryptedvalue = new base64encoder().encode(encval); return encryptedvalue; } public static string decrypt(string data) throws exception { system.out.println(data.length()); cipher c = cipher.getinstance(algo); c.init(cipher.decrypt_mode, key, new ivparameterspec(c.getparameters().getparameterspec(ivparameterspec.class).getiv())); byte[] dencval = c.dofinal(data.getbytes()); string decryptedvalue = new base64encoder().encode(dencval); return decryptedvalue; } public static key generatekey() { key key = new secretkeyspec(keyvalue,"aes"); return key; } } and i'm getting error:
error:exception in thread "main" javax.crypto.illegalblocksizeexception: input length must multiple of 16 when decrypting padded cipher @ com.sun.crypto.provider.ciphercore.dofinal(ciphercore.java:913) @ com.sun.crypto.provider.ciphercore.dofinal(ciphercore.java:824) @ com.sun.crypto.provider.aescipher.enginedofinal(aescipher.java:436) @ javax.crypto.cipher.dofinal(cipher.java:2165) @ encryption.decrypt(encryption.java:30) @ encryptiontest.main(encryptiontest.java:9) what doing wrong?
aes/cbc/pkcs5padding "block cipher" meaning in works on blocks of fixed size, 16 bytes in case. inputs of smaller size must padded out bytes encryption work.
decryption, because it's working out output of encryption operation, must multiple of block size. exception telling you have not provided correctly encrypted data because input not multiple of block size.
decoding should opposite of encoding if encode doing "encrypt base64-encode" decoding must "base64-decode decrypt". method "decrypt base64-encode" pretty mistake.
Comments
Post a Comment