I am fairly new to encryption/decryption and have to encrypt some data files, but am not entirely sure I am going about it the right way. Right now, I have a script to encrypt all the files, which is not included in my repo, but the decrypter is included in the repo and the encryption key is read in as an environment variable. I'm assuming even including the decrypter in the repo is bad practice because the algorithm/mode/padding is visible within the code as you can see below.
Aside from that, after some research, it looks like if an attacker knows the initialization vector, they would be able to decrypt the first block in the encrypted file. Am I understanding that right? Also, I have read that the vector should be randomly generated each time but I do not understand how I can do this considering the encrypter would only be run once unless I change the way I go about encrypting files.
One last question...would it make sense to have another environment variable to store the initialization vector being used? I assume not, because of the randomization of the vector being sought after.
private static byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
private static IvParameterSpec ivspec = new IvParameterSpec(iv);
private static byte[] decodedKey = Base64.getDecoder().decode(System.getenv("SECRET_KEY"));
private static SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
//any encrypted input file that is not a database will be read in as an input stream
public static CipherInputStream decryptInputStream(InputStream inputStream) {
try{
// Initialize the cipher by specifying the algorithm/mode/padding
Cipher aes2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
//
aes2.init(Cipher.DECRYPT_MODE, originalKey, ivspec);
CipherInputStream in = new CipherInputStream(inputStream, aes2);
return in;
} catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e){
logger.info(e.getMessage());
return null;
}
}