Search code examples
javajspencryptionziparchive

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?


I'm essentially trying to do the following on a Java/JSP-driven web site:

  • User supplies a password
  • Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a text file as well as a number of binary files that are stored on the server. It's essentially a backup of the user's files and settings.
  • Later, the user can upload the file, provide the original password, and the site will decrypt and unpack the archive, save the extracted binary files to the appropriate folder on the server, and then read the text file so the site can restore the user's old settings and metadata about the binary files.

It's the building/encrypting the archive and then extracting its contents that I'm trying to figure out how to do. I really don't care about the archive format, other than that it is very secure.

The ideal solution to my problem will be very easy to implement, and will require only tried-and-tested libraries with free and nonrestrictive licenses (e.g. apache, berkeley, lgpl).

I'm aware of the TrueZIP and WinZipAES libraries; the former seems like massive overkill and I can't tell how stable the latter is... Are there other solutions out there that would work well?


Solution

  • If you know how to create a zip file using the java.util.zip package, you can create a PBE Cipher and pass that to a CipherOutputStream or a CipherInputStream (depending on if you're reading or writing).

    The following should get you started:

    public class ZipTest {
    
        public static void main(String [] args) throws Exception {
            String password = "password";
            write(password);
            read(password);
        }
    
        private static void write(String password) throws Exception {
            OutputStream target = new FileOutputStream("out.zip");
            target = new CipherOutputStream(target, createCipher(Cipher.ENCRYPT_MODE, password));
            ZipOutputStream output = new ZipOutputStream(target);
    
            ZipEntry e = new ZipEntry("filename");
            output.putNextEntry(e);
            output.write("helloWorld".getBytes());
            output.closeEntry();
    
            e = new ZipEntry("filename1");
            output.putNextEntry(e);
            output.write("helloWorld1".getBytes());
            output.closeEntry();
    
            output.finish();
            output.flush();
        }
    
        private static Cipher createCipher(int mode, String password) throws Exception {
            String alg = "PBEWithSHA1AndDESede"; //BouncyCastle has better algorithms
            PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(alg);
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
    
            Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
            cipher.init(mode, secretKey, new PBEParameterSpec("saltsalt".getBytes(), 2000));
    
            return cipher;
        }
    
        private static void read(String password) throws Exception {
            InputStream target = new FileInputStream("out.zip");
            target = new CipherInputStream(target, createCipher(Cipher.DECRYPT_MODE, password));
            ZipInputStream input = new ZipInputStream(target);
            ZipEntry entry = input.getNextEntry();
            while (entry != null) {
                System.out.println("Entry: "+entry.getName());
                System.out.println("Contents: "+toString(input));
                input.closeEntry();
                entry = input.getNextEntry();
            }
        }
    
        private static String toString(InputStream input) throws Exception {
            byte [] data = new byte[1024];
            StringBuilder result = new StringBuilder();
    
            int bytesRead = input.read(data);
            while (bytesRead != -1) {
                result.append(new String(data, 0, bytesRead));
                bytesRead = input.read(data);
            }
    
            return result.toString();
        } 
    }