代码之家  ›  专栏  ›  技术社区  ›  DanM

如何在Java中打包/加密/解压缩/解密一堆文件?

  •  3
  • DanM  · 技术社区  · 16 年前

    我试图弄清楚如何构建/加密档案,然后提取其内容。我真的不在乎档案格式,除了它非常安全。

    4 回复  |  直到 16 年前
        1
  •  6
  •   Kevin    16 年前

    如果你知道如何使用创建zip文件 java.util.zip 包,您可以创建PBE Cipher

    以下内容应该能让你开始:

    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();
        } 
    }
    
        2
  •  4
  •   BalusC    16 年前

    答案已经给出了(正如Kevin指出的那样,使用密码),所以我只是对你的主题中似乎缺少的一个重要问题提出建议。开始:确保你使用 HTTPS 而不是 HTTP Tomcat SSL HOW-TO .

    希望这能有所帮助。

        3
  •  0
  •   PP.    16 年前

    truecrypt 可能有用。您的Web服务器可以创建一个加密容器,将zip文件复制到其中。然后可以下载加密的容器。可能有点混乱,但加密应该很强,下载的图像可以安装在各种操作系统上。

        4
  •  0
  •   jarnbjo    16 年前

    这里肯定有一些关于如何解决你的问题的建议,但我在回复中错过了一个非常大的但。对于“强加密”的任何合理定义,您都不能同时满足“基于密码”和“强加密“的要求。

    推荐文章