java


javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes


I have an ArrayList which contains some objects. Object is a container for login/pass.
I try to decode them because i have to serialize them into local file for recreation after another launch.
Problem is during encryption I recieve
javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1039)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:983)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
And I completely don't understand why. In my opinion Base64 should deal with this. But maybe I don't understand properly its meaning.
I use to encrypt/deccode
public class Move
{
private static Move instance;
String key = "pT5IkWNR90gJo5YM";
String initVector = "RandomInitVector";
Cipher cipher;
private Move()
{
// try
// {
// cipher = Cipher.getInstance("AES/CBC/NoPadding");
// }
// catch (NoSuchAlgorithmException | NoSuchPaddingException e)
// {
// e.printStackTrace();
// }
}
public void saveData(ArrayList<Account> dataToSave)
{
try
{
FileOutputStream fileOut = new FileOutputStream(Config.SERIAL_FILE);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(encrypt(dataToSave));
out.close();
fileOut.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
public ArrayList<Account> loadData()
{
ArrayList<Account> loadedData = new ArrayList<Account>();
File f = new File(Config.SERIAL_FILE);
if (f.exists())
{
try
{
FileInputStream fileIn = new FileInputStream(Config.SERIAL_FILE);
ObjectInputStream in = new ObjectInputStream(fileIn);
loadedData = (ArrayList<Account>) in.readObject();
in.close();
fileIn.close();
}
catch (IOException | ClassNotFoundException e)
{
e.printStackTrace();
}
loadedData = decrypt(loadedData);
}
else
{
loadedData = new ArrayList<Account>();
}
return loadedData;
}
private ArrayList<Account> encrypt(List<Account> decrypted)
{
ArrayList<Account> encrypted = new ArrayList<Account>();
try
{
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
for (int i = 0; i < decrypted.size(); i++)
{
try
{
byte[] login = cipher.doFinal(Base64.getDecoder().decode(decrypted.get(i).getLogin().getBytes()));
encrypted.add(new Account(login.toString(), "pass"));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
catch (InvalidKeyException | InvalidAlgorithmParameterException | UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException e)
{
e.printStackTrace();
}
return encrypted;
}
private ArrayList<Account> decrypt(List<Account> encrypted)
{
ArrayList<Account> decrypted = new ArrayList<Account>();
try
{
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
for (int i = 0; i < encrypted.size(); i++)
{
byte[] login = cipher.doFinal(Base64.getDecoder().decode(encrypted.get(i).getLogin()));
decrypted.add(new Account(new String(login), "pass"));
}
}
catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException e)
{
e.printStackTrace();
}
return decrypted;
}
public static Move getMove()
{
if (instance == null)
{
instance = new Move();
}
return instance;
}
}
AES block size is always 128bit, it must receive input in multiples of this number.
Smaller input must be padded to 16 bytes, and the type of padding specified to the algorithm.
Using "AES/CBC/PKCS5Padding" will do the trick.
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
If you use NoPadding, then you must implement your own padding for encryption, and make sure to remove it from the resulting string in decryption.

Related Links

Java no abstract and does not override error
How to prevent Gradle from including the resource files in the JAR
ProgressMonitorDialog and user interaction
Java upload file to server php
Apache Camel - Spring Web App - Camel Context not started on #PostConstruct
How to validate Integer received from a socket stream in java
Java Hashtable set all keys with same value
Apache Mina UDP Client: java.net.PortUnreachableException
(Process String) in Java program
Failed to instantiate com.google.android.gms.common.SignInButton
KeyListener added as own class is not working [duplicate]
rmi java program not expected output
How to catch 404 (NotFoundException) without being depndant on a JAX-RS implemenation?
Need to schedule jobs for the script to be run at aspecific time
Clicking my JButton but not getting the action shown before i click another button in my JFrame using ActionListener?
Convert date in proper format in android

Categories

HOME
deployment
blogger
omnet++
lodash
session
fme
bookshelf.js
ip
win32gui
icloud
enterprise-library-5
in-app-purchase
upload
vault
node-pdfkit
floating-action-button
handsontable
correlation
normalizr
openedx
custom-wordpress-pages
xlsxwriter
captiveportal
android-widget
apache-commons-io
bootstrap-duallistbox
react-css-modules
entitlements
rst2pdf
strncpy
maquette
log4js-node
primitive
procdump
create-table
lxd
asset-pipeline
subset-sum
uiswipegesturerecognizer
rotational-matrices
wpf-controls
overriding
tasker
broadcastreceiver
firebase-admin
arena-simulation
netcdf4
scorm
cubic-spline
jsch
fault
alphabet
togetherjs
typed-lambda-calculus
zendesk-app
flutterwave
sonarlint-vs
nested-sets
log4c
url-masking
bstr
gstreamer-0.10
ubuntu-10.04
make-install
revolution-r
freedesktop.org
execute
prerequisites
msys
jms2
comobject
muse
apache-commons-fileupload
streambase
npapi
csquery
block-device
tablelayout
cdt
winddk
asp.net-web-api-odata
flask-cors
elliptic-curve
reactfx
undefined-reference
cos
factory-method
itmstransporter
pvrtc
quickdialog
javaspaces
wse3.0
osql
adk
stage
zpt
mod-auth
calling-convention
radcombobox
gallio
infrastructure
audio-capture
anti-piracy
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App