This is my encryption method which i have created to enter the salt, iv, encrypted text into a mysql database for future reference. the values of salt, iv and encrypted text are stored in blob, blob and longblob datatype respectively (in mysql).
package enigma;
import java.sql.*;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author
*/
public class encryptedtexttomysql {
/**
*
* @author USER
* @param plaintext
*/
public void enc(String plaintext){
try{
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","student");
Statement stmt = con.createStatement();
// password to encrypt the file
String password = "student";
// salt is used for encoding
byte[] salt = new byte[8];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(salt);
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536,
128);
SecretKey secretKey = factory.generateSecret(keySpec);
SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
//
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
AlgorithmParameters params = cipher.getParameters();
byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
cipher.init(Cipher.ENCRYPT_MODE, secret , new IvParameterSpec(iv));
//file encryption method
byte[] output = cipher.doFinal((plaintext).getBytes());
String query = "INSERT INTO enc values(\""+salt +"\",\""+ iv+"\",\""+ output+"\");";
stmt.executeUpdate(query);
System.out.println("File Encrypted.");
stmt.close();
con.close();
System.out.println("connection successful !");
}
catch( ClassNotFoundException | SQLException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e){
System.out.println(e);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(encryptedtexttomysql.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
This method is created to accept the values from the database and then use it to decrypt the encrypted text. the values of salt,iv,encrytedtext in the database is stored in blob, blob, longblob datatype
package enigma;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author
*/
public class decryptetextfrommysql {
public String dec(){
byte [] salt;
byte [] enctext ;
byte[] output = null;
//this is to get values from the mysql database
try{
Class.forName("java.sql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","student");
Statement stmt = con.createStatement();
String sql = ("SELECT * FROM enc;");
ResultSet rs = stmt.executeQuery(sql);
byte [] iv = null;
while(rs.next()) {
salt = rs.getBytes("salt");
iv = rs.getBytes("iv");
enctext= rs.getBytes("encryptedtext");
}
String password = "student";
salt = new byte[8];
enctext = new byte[64];
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
SecretKey tmp = factory.generateSecret(keySpec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
// file decryption method
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
iv = new byte[16];
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
output = cipher.update(enctext);
output = cipher.doFinal();
//printing out the confirmation
System.out.println("File Decrypted! hurray !");
System.out.println("File Decrypted.");
stmt.close();
con.close();
rs.close();
System.out.println("connection successful !");
}
catch(ClassNotFoundException | SQLException ex){
System.out.println(ex);
}
catch(NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(decryptetextfrommysql.class.getName()).log(Level.SEVERE, null, ex);
System.out.println(ex);
}
return new String(output);
}
}
Here is how i have called the methods and used them.
encryptedtexttomysql obj4 = new encryptedtexttomysql();
decryptetextfrommysql obj5 = new decryptetextfrommysql();
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String plaintext = ta1.getText();
obj4.enc(plaintext);
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
String a = obj5.dec();
System.out.println(a);
here is the output exception:
javax.crypto.BadPaddingException: Given final block not properly padded
SEVERE: null
�� A� I� wd�� 彟� 6�� A� I� wd�� 彟� 6�� A� I� wd�� 彟� 6
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java: 966)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java: 824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java: 436)
at javax.crypto.Cipher.doFinal(Cipher.java: 2048)
basically there is a problem in the doFinal method.