Reputation: 23
im getting this error while trying to encrypt using AES in Java. Im using a 128 bits key. My goal is to encrypt certain data that later will be inserted into the db
Im new to encryption so any help will be useful.
This is how the code looks:
public class AES2 {
private static final String algo = "AES/CBC/PKCS5Padding";
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
//KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
//KeyGen.init(128);
//SecretKey SecKey = KeyGen.generateKey();
Key key=generateKey();
//generar IV
byte[] iv = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
//cifrar
Cipher c = Cipher.getInstance(algo);
c.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
byte[] iv = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher c = Cipher.getInstance(algo);
c.init(Cipher.DECRYPT_MODE, key,ivParameterSpec);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, "AES");
return key;
}
}
Main class:
public class aesMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
String texto = "hola mama";
String encryptedText=AES2.encrypt(texto);
String decryptedText=AES2.decrypt(encryptedText);
System.out.println("Plain text : " + texto);
System.out.println("Encrypted Text : " + encryptedText);
System.out.println("Decrypted Text : " + decryptedText);
}
}
Upvotes: 2
Views: 3065
Reputation: 112857
The thing about padding errors is that they are usually not errors in the padding but the wrong key or IV during decryption. The plaintext before the removal of padding will result in random bytes and is then likely reported as bad padding.
In this case a different IV is used for encryption and decryption. The IV must however be identical during encryption and decryption. A common procedure is to create a random IV during encryption and prefix it to the ciphertext. Before decryption the IV is retrieved and stripped from the ciphertext.
See Cipher Block Chaining to understand how an incorrect IV will affect decryption.
Upvotes: 1