java - 使用 apache compress/org.tukaani.xz 在 java 中解压/解密受密码保护 (AES 256) 7z 文件的问题

标签 java apache 7zip compression

尝试解密受密码保护的 (AES 256) 7z 文件时出现org.tukaani.xz.CorruptedInputException:压缩数据已损坏错误。而没有密码保护的 7z 文件解压没有任何问题。这两种情况都压缩相同的 xls 文件。

我正在使用 Apache commons compress 和 org.tukaani.xz。

示例代码供引用。

package com.concept.utilities.zip;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

public class DecryptionUtil {


    static {
        try {
            Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
            field.setAccessible(true);
            field.set(null, java.lang.Boolean.FALSE);
        } catch (Exception ex) {
        }
    }


    public void SevenZFile(String directory, String encryptCompressFileName, String password) {

        SevenZFile sevenZFile = null;
        SevenZArchiveEntry entry = null;

        try {

            File file = new File(directory+encryptCompressFileName);
            byte[] inputData = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
            fis.read(inputData);
            fis.close();

            // SeekableInMemoryByteChannel inMemoryByteChannel = new SeekableInMemoryByteChannel(inputData);
            if(null != password){
                byte[] pass = password.getBytes("UTF16");
                sevenZFile = new SevenZFile(file, pass);
            }else{
                sevenZFile = new SevenZFile(file);
            }

            // Go through all entries
            while (null != (entry = sevenZFile.getNextEntry())) {

                // Maybe filter by name. Name can contain a path.
                String processingFileName = entry.getName();
                if (entry.isDirectory()) {
                    System.out.println(String.format("Found directory entry %s", processingFileName));

                } else {

                    // If this is a file, we read the file content into a ByteArrayOutputStream ...
                    System.out.println(String.format("Unpacking start %s ...", processingFileName));
                    ByteArrayOutputStream contentBytes = new ByteArrayOutputStream();

                    // ... using a small buffer byte array.
                    byte[] buffer = new byte[2048];
                    int bytesRead;

                    while ((bytesRead = sevenZFile.read(buffer)) != -1) {
                        contentBytes.write(buffer, 0, bytesRead);
                    }


                    if (processingFileName.endsWith("xls")) {
                        // Writing into xls
                        Workbook wb = new HSSFWorkbook();
                        //String safeName = WorkbookUtil.createSafeSheetName(processingFileName);
                        //Sheet sheet = wb.createSheet(safeName);
                        FileOutputStream fileOut = new FileOutputStream(directory+processingFileName);
                        fileOut.write(contentBytes.toByteArray());
                        fileOut.flush();
                        wb.write(fileOut);
                        fileOut.close();
                        wb.close();
                    }else{ //regular file
                        System.out.println(contentBytes.toString("UTF-8"));
                    }
                    System.out.println(String.format("Unpacking finish %s ...", processingFileName));   
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                sevenZFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) {

        DecryptionUtil decrypt = new DecryptionUtil();
        decrypt.SevenZFile("H:\\archives\\", "StudentsWoPassword.7z", null);
        decrypt.SevenZFile("H:\\archives\\", "StudentsWithPassAES256.7z", "test");

    }

}

StudentsWoPassword.7z 成功解压,但 StudentsWithPassAES256.7z 抛出异常。

Unpacking start Students.xls ...
Unpacking finish Students.xls ...
org.tukaani.xz.CorruptedInputException: Compressed data is corrupt
    at org.tukaani.xz.rangecoder.RangeDecoderFromStream.<init>(Unknown Source)
    at org.tukaani.xz.LZMAInputStream.initialize(Unknown Source)
    at org.tukaani.xz.LZMAInputStream.initialize(Unknown Source)
    at org.tukaani.xz.LZMAInputStream.<init>(Unknown Source)
    at org.apache.commons.compress.archivers.sevenz.LZMADecoder.decode(LZMADecoder.java:43)
    at org.apache.commons.compress.archivers.sevenz.Coders.addDecoder(Coders.java:76)
    at org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecoderStack(SevenZFile.java:933)
    at org.apache.commons.compress.archivers.sevenz.SevenZFile.buildDecodingStream(SevenZFile.java:909)
    at org.apache.commons.compress.archivers.sevenz.SevenZFile.getNextEntry(SevenZFile.java:222)
    at com.concept.utilities.zip.DecryptionUtil.SevenZFile(DecryptionUtil.java:50)
    at com.concept.utilities.zip.DecryptionUtil.main(DecryptionUtil.java:107)

我错过了什么吗?还有其他方法可以提取 AES256 7z 吗?

最佳答案

您的代码很好,您只是在从密码中提取字节时使用了错误的字符集/编码。 SevenZFile 类需要小端字节序的 UTF-16,因此您必须使用 UTF-16LE 而不是 UTF-16 (它将使用大端字节序)编码数据时)。

关于java - 使用 apache compress/org.tukaani.xz 在 java 中解压/解密受密码保护 (AES 256) 7z 文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190811/

相关文章:

php - 使用 echo 或 Die 进行调试和记录

java - 如何从 Volley 请求响应中解析大型 JsonArray

java - JSONObject 未在可执行 jar 中创建

Java,从单独的 JSON 值到字符串的日期

apache - 在服务器 b.example.com 上配置 Apache 以允许在没有警告的情况下从 example.com 请求 b.example.com 上的安全资源?

regex - .htaccess 重写 URL 启动/重定向

windows - 从多个 7-zip 文件中提取特定的文件扩展名

windows - 用于压缩子目录的批处理文件

Python 使用命令行创建 7zip 存档

java - 如何获取列的最后一个值