1

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);
user1616601
  • 21
  • 1
  • 7
  • 3
    Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the [help] and read [ask], and especially read [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – kvantour Feb 13 '19 at 08:14
  • I agree to the previous comment by kvantour. Apart from that, there are several possibilities to store several streams in one file - an obvious one being `java.util.zip.ZipFile`. – Christian Gawron Feb 13 '19 at 08:29
  • You don't need to save the IV anywhere. Just hardware it into both ends. – user207421 Feb 13 '19 at 09:16
  • 1
    As Luke writes, you should provide the IV in the encrypted file, not in a file together with the key. The IV is only relevant for that specific encrypted file, and common practice is to keep those together. As for the keys, how do you distribute these to the receiver without compromising it ? If you need a new key for each file, and need to distribute this, it seems that you should utilize Hybrid encryption, where you encrypt the unique (symmetric) encryption key using the receivers public key. – Ebbe M. Pedersen Feb 13 '19 at 09:22
  • Refer to this. It shows how to send the IV with the cipher text: https://stackoverflow.com/a/53015144/1235935 – Saptarshi Basu Feb 13 '19 at 13:52

2 Answers2

7

You're approach to using IV's is incorrect. IV's aren't secret and shouldn't be reused. Generate a new one every single time you encrypt and just store it alongside the ciphertext, not with the key!

See the examples in this repository for best practices when it comes to symmetric encryption.

Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44
  • Hi Luke Joshua Park, Thanks for the prompt response. I have encrypted my file using newly generated IV and key every single time. i need to send these encrypted file to third party along with key and IV to decrypt. But instead of send key and IV separately they want to combine in single 48byte file[key 32byte +16 byte IV ] Can you please help me how to achieve this? – user1616601 Feb 13 '19 at 08:22
  • If they aren't secret why shouldn't they be reused? – user207421 Feb 13 '19 at 09:17
  • @user207421 Because being able to *predict* the next IV in a sequence of encryption operations can set up certain circumstances that lead to plaintext exposure or at least knowledge of the underlying data. If that was your downvote, I'd encourage you to further research the topic and remove it :) – Luke Joshua Park Feb 13 '19 at 09:19
  • 2
    @user207421 Nope, they aren't secret. It is safe for anyone to know the IV corresponding to a particular ciphertext. It just isn't safe for them to be able to predict what the next one will be. That is why a fixed IV is unsafe but randomly generating them each time *is* safe. Once more, I encourage you to research this topic yourself rather than arguing with me about something you don't understand. – Luke Joshua Park Feb 13 '19 at 09:21
  • 1
    Don't know why the negative vote. This is correct answer. @user207421 – Saptarshi Basu Feb 13 '19 at 13:16
0
I found a way to store in one file and used that file for decryption and its working. here is my approach
 while writing IV and key in 2 different files, i have written in one file. And for Decryption i read the file like first 16bytes for IV and  last 32 bytes for secretkey. 


FileOutputStream OutFile = new FileOutputStream("C:\\SecretFile.key");
      OutFile.write(iv); 
      KeyGenerator kgen = KeyGenerator.getInstance("AES");
      kgen.init(256);
      SecretKey skey = kgen.generateKey();
      byte[] keyb = skey.getEncoded();
      OutFile.write(keyb);
      OutFile.close(); 
user1616601
  • 21
  • 1
  • 7