I'm creating a application in JavaFX that first Zip the selected folder and then crypt it. The proces of Zip the folder and crypt it its Ok, the problem is when i try to uncrypt it: appears a warning with IVParameterSpec.
I'm thinking that the problem is that I need to save somwhere the IVS. Maybe at the beggining of the encrypted file? How can i do it?
I'm following this tutorial for the AES with SHA-256 encryption with this modifications: http://karanbalkar.com/2014/02/tutorial-76-implement-aes-256-encryptiondecryption-using-java/
Crypt & UnCrypt:
private static String password;
private static String salt;
private static int pswdIterations = 65536 ;
private static int keySize = 256;
private byte[] ivBytes;
public void encryptToFile(byte[] bytes, File out) throws Exception {
byte[] saltBytes = salt.getBytes("UTF-8");
System.out.println("Salt bfre:" +salt);
// Derive the key
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(
password.toCharArray(),
saltBytes,
pswdIterations,
keySize
);
SecretKey secretKey = factory.generateSecret(spec);
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
//encrypt the message
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out, true), cipher);
os.write(bytes);
os.close();
}
public byte[] decryptToFile(File in) throws Exception {
byte[] saltBytes = salt.getBytes("UTF-8");
// Derive the key
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(
password.toCharArray(),
saltBytes,
pswdIterations,
keySize
);
SecretKey secretKey = factory.generateSecret(spec);
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
// Decrypt the message
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));
FileInputStream is = new FileInputStream(in);
byte[] encBytes = new byte[(int) in.length()];
is.read(encBytes);
is.close();
byte[] decryptedBytes = null;
try {
decryptedBytes = cipher.doFinal(encBytes);
} catch (IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
return decryptedBytes;
}
And now in the main:
@FXML private void btDoIt (ActionEvent event){
if (tests()){
HashClass hash = new HashClass(tfPassword.getText());
SimetricWithSha256 aes256 = new SimetricWithSha256();
aes256.setPassword(tfPassword.getText());
aes256.setSalt(hash.sumCalcToString("SHA-256"));
if (rbCrypt.isSelected()){
AppZip appZip = new AppZip(tfPath.getText(),destCrypt);
ByteArrayOutputStream baos = appZip.zip();
try {
aes256.encryptToFile(baos.toByteArray(), new File (tfPath.getText()+destCrypt) );
baos.close();
} catch (Exception ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}else if (rbUncrypt.isSelected() ){
try {
byte[] decrypted = aes256.decryptToFile(new File (tfPath.getText() ));
FileOutputStream fos = new FileOutputStream(tfPath.getText()+destDeCrypt);
fos.write(decrypted);
fos.close();
} catch (Exception ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
If I do the proces of decryption inmediatly after of de encryption it works fine. The problem is when I do it in the second option: else if (rbUncrypt.isSelected() )
I recive this error in the line of the uncrypt function where "cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));":
java.lang.NullPointerException at javax.crypto.spec.IvParameterSpec.(IvParameterSpec.java:53) at encriptarusb.SimetricWithSha256.decryptToFile(SimetricWithSha256.java:137)
Maybe is because i must to save the ivBytes??
Thanks!