Menu

AES256 decrypt : org.tukaani.xz.CorruptedInputException: Compressed data is corrupt exception

Rajarshi
2017-07-19
2017-07-19
  • Rajarshi

    Rajarshi - 2017-07-19

    I'm a beginner to compression. I am trying to unpack a password protected AES 256 encoded 7z file but getting below error. It is working find without password protected 7z file.

    Am I missing anything?

    Error
    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)

    There is the code I have written
    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();
            }
        }
    
    }
    
     

Log in to post a comment.