I have to encrypt my file using AES -256 cipher with AES 256 key and 16 bytes IV and I want to save key and IV in one file and reuse it for Decryption. But currently i can save it individually. Can any one help us how to store key and IV in a single file.
here is my code
SecureRandom srandom = new SecureRandom();
byte[] iv = new byte[16];
srandom.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);
FileOutputStream ivOutFile = new FileOutputStream("C:\\iv.key");
ivOutFile.write(iv);
ivOutFile.close();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256);
SecretKey skey = kgen.generateKey();
FileOutputStream out = new FileOutputStream("C:\\AES.key");
byte[] keyb = skey.getEncoded();
out.write(keyb);
out.close();
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.ENCRYPT_MODE, skey, ivspec);
FileEncryptionUtils fileEncryptionUtils =new FileEncryptionUtils();
fileEncryptionUtils.processFile(ci, inFile, outFile);