java - Given final block not properly padded AES256 -
i have key, , want decrypt data using key, given final block not padded error appears.
cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.decrypt_mode, key, new ivparameterspec(new byte[cipher.getblocksize()])); byte[] encryptedbytes_updates = cipher.update(encryptedbytes); string decryptedtext = new string(cipher.dofinal(encryptedbytes_updates)); i tried advices forums, no luck.
may can help?
if message you're decoding less 16 bytes, 1 of issues may you're using wrong iv. specifically, line incorrect:
cipher.init(cipher.decrypt_mode, key, new ivparameterspec(new byte[cipher.getblocksize()])); you're creating ivparameterspec byte array of 0's (because created it, didn't fill data). if you're doing same thing while encrypting, reason encryption/decryption works locally you.
what need create ivparameterspec using byte array containing iv server sends you. if server isn't sending iv in separate field, may iv has been prepended or appended encrypted data receive (this common practice). trying pulling off first block (16 bytes) of encrypted data , use iv. if doesn't work, try last block of encrypted data. or better yet, ask whoever runs server or read manual determine iv from.
also, looks incorrect well:
byte[] encryptedbytes_updates = cipher.update(encryptedbytes); string decryptedtext = new string(cipher.dofinal(encryptedbytes_updates)); why take encryptedbytes_updates , feed in cipher? update decrypts data, you're trying decrypt again using dofinal. instead:
string decryptedtext = new string(cipher.dofinal(encryptedbytes));
Comments
Post a Comment