Ciao, dopo aver letto e aver visto diversi esempi, sono a chiedere un consiglio. Il mio scopo è quello di proteggere con una password il database SQLite.
Con il codice che sono riuscito a creare, cripto e decripto il db per proteggerlo con una password. tutto funziona correttamente....penso però che forse esiste un altro modo per proteggere un file con una password. Nel mio caso ogni volta che cripto e decripto creo un nuovo file e questo mi preoccupa, più che altro per il rischio di perdita dei dati e perchè mi sembra una operazione poco pulita. Avete qualche consiglio/suggerimento per raggiungere il mio scopo??
classe che si occupa di criptare e decriptare:
codice:
public class CryptoUtils {
private static final String ALGORITHM = "AES";
private static String ENCRYPTION_KEY = "";
private static final String ENCRYPTION_IV = "4e5Wa71fYoT7MFEX";
public static void encrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public static void decrypt(String key, File inputFile, File outputFile)
throws CryptoException {
doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private static void doCrypto(int cipherMode, String key, File inputFile,
File outputFile) throws CryptoException {
try {
// Derive the ke
ENCRYPTION_KEY = key;
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(cipherMode, makeKey(), makeIv());
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
System.out.println("Encryption/Decryption Completed Succesfully");
} catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException | IOException ex) {
throw new CryptoException("Error encrypting/decrypting file" + ex.getMessage(), ex);
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(CryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
}
}
static AlgorithmParameterSpec makeIv() {
try {
return new IvParameterSpec(ENCRYPTION_IV.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
static Key makeKey() {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] key = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
return new SecretKeySpec(key, ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
criptare:
codice:
private void btnEncryptActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
CryptoUtils.encrypt(jTextFieldPasswordToEncrypt.getText(), inputFile, encryptedFile);
JOptionPane.showMessageDialog(rootPane, "Encrypted Succesfully");
} catch (CryptoException ex) {
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
ex.printStackTrace();
}
}
});
t.start();
}
decriptare:
codice:
private void btnDecryptActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
CryptoUtils.decrypt(jTextFieldPasswordToDecrypt.getText(), encryptedFile, decryptedFile);
JOptionPane.showMessageDialog(rootPane, "Decrypted Succesfully");
} catch (CryptoException ex) {
System.out.println(ex.getMessage());
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
ex.printStackTrace();
}
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});
t.start();
}