如何解压缩 AES-256 加密的 zip 文件?

发布于 2024-10-10 06:38:31 字数 108 浏览 0 评论 0原文

我正在开发一个 Android 应用程序,需要解压缩 AES-256 加密的 zip 文件,是否有任何库可以用来完成此任务?

我非常感谢任何指导或帮助。

I am developing an android application which requires to decompress an AES-256 encrypted zip files, is there any libraries out there that I can use to accomplish that?

I am greatly appreciative of any guidance or help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沫尐诺 2024-10-17 06:38:31

zip4j,处理 Zip 文件的 java 库(开源,Apache License v2.0)。

http://www.lingala.net/zip4j/

  • 创建、添加、提取、更新、删除 Zip 文件中的文件
  • 阅读/写入受密码保护的 Zip 文件
  • 支持 AES 128/256 加密
  • 支持标准 Zip 加密

您可以下载二进制文件、源代码和示例。

zip4j, java library to handle Zip files (Open source, Apache License v2.0).

http://www.lingala.net/zip4j/

  • Create, Add, Extract, Update, Remove files from a Zip file
  • Read/Write password protected Zip files
  • Supports AES 128/256 Encryption
  • Supports Standard Zip Encryption

You can download binary, sources and examples.

£噩梦荏苒 2024-10-17 06:38:31

我最终使用了外部库 http://code.google.com/p/winzipaes/

它仅限于压缩/解压使用 AES-256 加密的 Zip 文件

,但至少它符合我的需要。

I ended up using an external library at http://code.google.com/p/winzipaes/

it's limited to compression/decompression Zip files encrypted with AES-256 ONLY

but at least it fits my need.

内心荒芜 2024-10-17 06:38:31

这取决于您对加密 zip 文件的编码。请更具体一些。
如果它压缩然后加密,那么您解压缩然后使用 java.util.zip.GZIPInputStream

It depends on you encoding of encrypted zip files. Be more specific please.
If its compress then encrypt then you decompress then decrypt the file using java.util.zip.GZIPInputStream

深爱不及久伴 2024-10-17 06:38:31

据我所知,AES 加密的 ZIP 文件是在几年前由 WinZip 首次引入的。
WinZip 主页上有 AES 加密 ZIP 文件与标准 ZIP 文件有何不同的详细说明:

http: //www.winzip.com/aes_info.htm

As far as I remember AES encrypted ZIP files were introduced some years the first time by WinZip.
On the WinZip home page there is a detailed explanation how the AES encrypted ZIP files differs from a standard ZIP file:

http://www.winzip.com/aes_info.htm

苏别ゝ 2024-10-17 06:38:31

我正在使用普通的 JRE。使用 http://www.lingala.net/zip4j/ 以下代码可以解密 zip文件:

ZipFile zipFile = new ZipFile(zipFile);
zipFile.setPassword(password);
for (Object fileHeaderObj : zipFile.getFileHeaders()) {
  FileHeader fileHeader = (FileHeader) fileHeaderObj;
  String fileName = fileHeader.getFileName();
  ZipInputStream zipIn = zipFile.getInputStream(fileHeader);
  // do whatever with the input stream
}

I am working on a normal JRE. Using http://www.lingala.net/zip4j/ the following code works to decrypt a zip file:

ZipFile zipFile = new ZipFile(zipFile);
zipFile.setPassword(password);
for (Object fileHeaderObj : zipFile.getFileHeaders()) {
  FileHeader fileHeader = (FileHeader) fileHeaderObj;
  String fileName = fileHeader.getFileName();
  ZipInputStream zipIn = zipFile.getInputStream(fileHeader);
  // do whatever with the input stream
}
月光色 2024-10-17 06:38:31

我面临类似的问题,并进行了大量搜索并测试了所有这些解决方案,但它们对我没有帮助。
最后,我找到了一个解决方案,我想在这里分享它也许可以帮助其他人。

就我而言,我有一个 AES-256 加密的 zip 文件。

当我使用 zip4j 的最新版本时,我遇到了这个异常:

Zip4j 不支持强加密

zip4j 代码:

    net.lingala.zip4j.ZipFile zipFile = new ZipFile("sourceAddress", "password".toCharArray());
    zipFile.extractAll("destinationAddress");

之后我测试了 winzipaes,遇到了这个错误:

额外字段的长度为 0 - 这可能不是 WinZip AES 加密条目

winzipaes 代码:

    final File dbFile = new File("destinationFile");
    AesZipFileDecrypter ze = null;
    try {
        ze = new AesZipFileDecrypter(new File("sourceAddress"), new AESDecrypterBC());
        ExtZipEntry entry = ze.getEntry(dbFile.getName());
        ze.extractEntry(entry, dbFile, "password");
    } catch (DataFormatException | IOException e) {
        e.printStackTrace();
    } finally {
        if (ze != null) {
            try {
                ze.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

最后,我找到了 Sevenzipjbind,它对我有用。

最终代码:

    RandomAccessFile randomAccessFile = null;
    IInArchive inArchive = null;
    try {
        randomAccessFile = new RandomAccessFile(fileAddress, "r");
        inArchive = SevenZip.openInArchive(null, // autodetect archive type
                new RandomAccessFileInStream(randomAccessFile));

        // Getting simple interface of the archive inArchive
        ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

        System.out.println("   Hash   |    Size    | Filename");
        System.out.println("----------+------------+---------");

        for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
            final int[] hash = new int[] { 0 };
            if (!item.isFolder()) {
                ExtractOperationResult result;

                result = item.extractSlow(new ISequentialOutStream() {
                    public int write(byte[] data) throws SevenZipException {
                        InputStream myInputStream = new ByteArrayInputStream(data);

                        try {
                            byte[] buffer = new byte[myInputStream.available()];
                            myInputStream.read(buffer);
                            File targetFile = new File(destinationAddress + item.getPath());
                            OutputStream outStream = new FileOutputStream(targetFile);
                            outStream.write(buffer);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        return data.length; // Return amount of consumed data
                    }
                },password);

                if (result == ExtractOperationResult.OK) {
                    System.out.printf("%9X | %s%n",
                            hash[0], item.getPath());
                } else {
                    System.err.println("Error extracting item: " + result);
                }
            }
        }
    } catch (Exception e) {
        System.err.println("Error occurs: " + e);
    } finally {
        if (inArchive != null) {
            try {
                inArchive.close();
            } catch (SevenZipException e) {
                System.err.println("Error closing archive: " + e);
            }
        }
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                System.err.println("Error closing file: " + e);
            }
        }
    }

I face a similar problem and searched a lot and tested all these solutions but they didn't help me.
Finally, I found a solution and I want to share it here maybe it can help others.

In my case, I have a AES-256 Encrypted zip file.

When I used the last version of zip4j, I faced this exception:

Zip4j does not support Strong Encryption

zip4j code:

    net.lingala.zip4j.ZipFile zipFile = new ZipFile("sourceAddress", "password".toCharArray());
    zipFile.extractAll("destinationAddress");

After that I tested winzipaes and I faced this error:

extra field is of length 0 - this is probably not a WinZip AES encrypted entry

winzipaes code:

    final File dbFile = new File("destinationFile");
    AesZipFileDecrypter ze = null;
    try {
        ze = new AesZipFileDecrypter(new File("sourceAddress"), new AESDecrypterBC());
        ExtZipEntry entry = ze.getEntry(dbFile.getName());
        ze.extractEntry(entry, dbFile, "password");
    } catch (DataFormatException | IOException e) {
        e.printStackTrace();
    } finally {
        if (ze != null) {
            try {
                ze.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Finally, I find sevenzipjbind and it works for me.

Final code:

    RandomAccessFile randomAccessFile = null;
    IInArchive inArchive = null;
    try {
        randomAccessFile = new RandomAccessFile(fileAddress, "r");
        inArchive = SevenZip.openInArchive(null, // autodetect archive type
                new RandomAccessFileInStream(randomAccessFile));

        // Getting simple interface of the archive inArchive
        ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

        System.out.println("   Hash   |    Size    | Filename");
        System.out.println("----------+------------+---------");

        for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
            final int[] hash = new int[] { 0 };
            if (!item.isFolder()) {
                ExtractOperationResult result;

                result = item.extractSlow(new ISequentialOutStream() {
                    public int write(byte[] data) throws SevenZipException {
                        InputStream myInputStream = new ByteArrayInputStream(data);

                        try {
                            byte[] buffer = new byte[myInputStream.available()];
                            myInputStream.read(buffer);
                            File targetFile = new File(destinationAddress + item.getPath());
                            OutputStream outStream = new FileOutputStream(targetFile);
                            outStream.write(buffer);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        return data.length; // Return amount of consumed data
                    }
                },password);

                if (result == ExtractOperationResult.OK) {
                    System.out.printf("%9X | %s%n",
                            hash[0], item.getPath());
                } else {
                    System.err.println("Error extracting item: " + result);
                }
            }
        }
    } catch (Exception e) {
        System.err.println("Error occurs: " + e);
    } finally {
        if (inArchive != null) {
            try {
                inArchive.close();
            } catch (SevenZipException e) {
                System.err.println("Error closing archive: " + e);
            }
        }
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                System.err.println("Error closing file: " + e);
            }
        }
    }
一笑百媚生 2024-10-17 06:38:31

我还没有测试过,但前段时间我发现了这个

我希望它能帮助你

I don't have tested it yet, but some time ago I found this.

I hope it can help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文