package admin.server.decryptlog;


import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Base64;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class Decrupt {


    public ShiperInter shiperKey = new ShiperKey();

    public void decryptor(String inputZipFile, String outputZipFile, String key) throws Exception {

        byte[] iv = new byte[16]; // Initialization Vector
        SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

        try (ZipInputStream zipIn = new ZipInputStream(new CipherInputStream(new FileInputStream(inputZipFile), cipher));
             ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outputZipFile))) {
            ZipEntry entry;
            while ((entry = zipIn.getNextEntry()) != null) {
                zipOut.putNextEntry(new ZipEntry(entry.getName()));
                byte[] buffer = new byte[1024];
                int len;
                while ((len = zipIn.read(buffer)) > 0) {
                    zipOut.write(buffer, 0, len);
                }
                zipOut.closeEntry();
            }
        }
    }
}
