I am using Java to write AES-256 to encrypt various files with different extension, the problem is, where am I suppose to store the file extension so the if the data is decrypted, the file extension would be restored ?

For example,

If I encrypt a "test.pdf" file, I will first read it as a byte array and then encrypt it using AES-256, but when the data is decrypted, I can manually save the decrypted data as "xyz.pdf", but this is because I know the original file extension was "pdf".

What If I don't know what was the extension of the original file before encryption, If I save the extension inside the encrypted text, then It would only be decrypted with my AES algorithm and wouldn't be decrypted with any another AES-256 algorithm even if the user has a correct key.

So, Where Am I supposed to store the extension of a file?

  • 2
    $\begingroup$ If you need to keep the file extension secret, I think the only solution is to simply add this file information to the message you encrypt. Possibly make some kind of header in the message saying what type of file it is. I am not sure, but I would assume there is some type of standard format for doing that. $\endgroup$ – Guut Boy Nov 24 '14 at 16:03
  • 1
    $\begingroup$ If you don't need to keep the extension secret, the obvious alternative would be to keep that as a part of the filename. For example, you might encrypt the file 'test.pdf' as 'test.pdf.encrypt'. $\endgroup$ – poncho Nov 24 '14 at 16:08
  • $\begingroup$ You can store the file in a format that encodes the full name before encrypting. For example 'gzip' places the file name in the compressed file or use 'zip' to zip up your files first. You would just need to remember to gunzip or unzip your file after decryption. If you do not want compression but just the file metadata then the 'tar' and 'cpio' formats would work. $\endgroup$ – John Meacham Nov 25 '14 at 6:52

If you are encrypting one file to one file, simply save the correct extension. Example: open test.pdf, encrypt the content, and save as test1.pdf, or test1.pdf.enc (so that you know that the file is encrypted and any pdf won't try to open it when you double click).

If you are encrypting more than one file together (and, in the end, you have one big chunk of data that contains more than one file inside), you're already having to deal with "which byte belongs to which file", and then storing the original filename is just part of this file manipulation.


AES is secure in such a way that you cannot find the key even if you know (part of) the plaintext. So it is not possible (feasible) to deduce the key based on the file prefix to decrypt the complete file. I suspect this is the attack that you anticipate. Like woliveirajr said, you can just use the existing filename with a suffix like .enc.

What you need to check for your use case is whether the filenames actually leak some undesired information. For example having a file Hidden_Cayman_Islands_Accounts.xls.enc is still bad. So it is good to also encrypt the filename. While you're at it you can throw the extension in there too. I suggest having some kind of header of the data before you encrypt the whole package.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.