0

I am trying to write Java code which can compress any file in .7z format and encrypt it with AES 256. I need to compress and encrypt a file through Java code and send it to a user. They then decompress the file on their Windows machine using 7zip software and give a password that was used to encrypt the file. I was able to achieve the first step which is compression as .7z, but I tried lot of different options for AES 256 encryption. The compressed file is encrypted, but I was not able to decompress and decrypt it in 7zip Windows software. I have given my Java code which I used to compress. How can I add the AES 256 encryption logic which can be later decompressed and decrypted in 7zip software?

import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.commons.compress.archivers.sevenz.*;

public class LZMACompressionExample {
    
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        
        String inputFilePath = "D:\\f\\test.txt";
        String outputFilePath = "D:\\c\\test.7z";

        System.out.println("File compression completed.");

        // create 7z archive with original file name
        SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File(outputFilePath));
        SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(new File(inputFilePath), new File(inputFilePath).getName());
        sevenZOutput.putArchiveEntry(entry);

        // write compressed data to 7z archive
        FileInputStream compressedFileInputStream = new FileInputStream(inputFilePath);
        byte[] compressedBuffer = new byte[1024];
        int compressedLen;
        while ((compressedLen = compressedFileInputStream.read(compressedBuffer)) > 0) {
            sevenZOutput.write(compressedBuffer, 0, compressedLen);
        }

        // close streams
        sevenZOutput.closeArchiveEntry();
        compressedFileInputStream.close();
        sevenZOutput.close();

        System.out.println("File compressed and saved in .7z format with original file name inside.");
    }
}

I tried lot of AES 256 encryption Java logic. The file is encrypting, but I'm not able to open it in 7zip windows software. It throws an error.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • I think you have missed the error stacktrace – Abhinaba Basu Apr 28 '23 at 07:46
  • 3
    https://commons.apache.org/proper/commons-compress/examples.html says _"7z can read archives with many compression and encryption algorithms supported by 7z but doesn't support encryption when writing archives."_ You'll need to carefully read the decryption logic and try to reverse it, or find another solution. I'd hope the 7z file format has sufficient documenation on the encryption scheme. – Petr Janeček Apr 28 '23 at 08:09
  • TBH, I don't see any encryption logic in the question, but just trying some stuff won't work. If you want to implement it (in the library?) then you need to get the protocol that 7zip uses and implement that. Otherwise the chance of success is about 0%. – Maarten Bodewes Apr 28 '23 at 09:03
  • Maybe you can use the JNI Wrapper mentioned here(https://stackoverflow.com/questions/21975613/how-can-i-encrypt-java-7z-archive) to achieve your goal. – roediGERhard Apr 28 '23 at 09:12

0 Answers0