Where to store the file extension to retrieve it correctly after decryption

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?

• 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. – Guut Boy Nov 24 '14 at 16:03
• 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'. – poncho Nov 24 '14 at 16:08
• 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. – John Meacham Nov 25 '14 at 6:52

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.