3

Is it possible to encrypt entire folders with Java's JCE library. The folders will contain other folders/files if that is a problem. For those who don't know what JCE is, it stands for Java Cryptography Extension and it is used for encryption in Java.

2
  • You could ZIP the folders and then encrypt that data? Commented May 21, 2013 at 20:32
  • But is it possible to just encrypt the folder? If I have to encrypt a zip its another step in the decrypting process to access the zip. Commented May 21, 2013 at 21:39

1 Answer 1

4

No. You can't encrypt entire folders with only Java's JCE library. It's not hard to zip it first though using a stream wrapper like ZipInputStream and ZipOutputStream. Since you don't care about file size reduction, you could also use jtar. That will make it easy to tar up the folder after which it can be encrypted with JCE like any other file.

I would make a class called something like public class FolderEncrypter with methods public byte[] encryptFolder(File folder) and public File decryptFolder(public byte[]). Then it is easy to reuse throughout your code whenever you need it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I have another quick question about JCE, you can encrypt .png's and .ogg files right? They are just files and not folders/zips
@ExoNaut You can encrypt anything that you can express as a byte array. So yes, PNGs and OGGs are fine.
Your suggested method signatures don't look right to me. encryptFolder should return a byte[] and decryptFolder should accept a byte[] as a parameter.
@DuncanJones I agree and I made the change. I was thinking that the method would encrypt the file in place and return a reference to the new File, but it would be better design to let the calling code do what they want with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.