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

标签 java jsp encryption zip archive

我实际上是在尝试在 Java/JSP 驱动的网站上执行以下操作:

  • 用户提供密码
  • 密码用于构建一个高度加密的存档文件(zip 或其他任何文件),其中包含一个文本文件以及存储在服务器上的许多二进制文件。它本质上是用户文件和设置的备份。
  • 稍后,用户可以上传文件,提供原始密码,站点将解密和解压缩存档,将提取的二进制文件保存到服务器上的适当文件夹中,然后读取文本文件,以便站点可以恢复用户的旧设置和有关二进制文件的元数据。

这是构建/加密存档,然后提取其内容,我正在尝试弄清楚如何做。我真的不关心存档格式,除了它非常安全。

我的问题的理想解决方案将非常容易实现,并且只需要经过试验和测试的库,这些库具有免费和非限制性许可(例如 apache、berkeley、lgpl)。

我知道 TrueZIP 和 WinZipAES 库;前者似乎有点矫枉过正,我不知道后者有多稳定……还有其他可行的解决方案吗?

最佳答案

如果您知道如何使用 java.util.zip 创建 zip 文件包,您可以创建一个 PBE Cipher并将其传递给 CipherOutputStream 或 CipherInputStream(取决于您是读还是写)。

以下应该让你开始:

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();
    } 
}

关于java - 如何在 Java 中打包/加密/解包/解密一堆文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1730237/

相关文章:

java - 如何在Java中获取文本文件的随机行?

java - 使用 jsp 分割 blackslash-path

eclipse - 当通过 Netbeans 或 Eclipse 运行时,jsp 页面究竟部署在哪里?

java - JSP菜单设计建议

encryption - RSA 的公钥和私钥可以互换吗?

node.js - 使用 openpgp 在转换后加密流

java - 在哪里可以找到 CTR 或 GCM 分组密码模式的 Java/Kotlin 实现?

Java Hashset 基于值而不是哈希码存储项目

java - 调用类实例时线程访问无效

运行 Spark Java 程序时出现 java.lang.NoSuchMethodError