Skip to content

Instantly share code, notes, and snippets.

@VladRez
Created July 20, 2021 16:28
Show Gist options
  • Save VladRez/a609bbe7e37946bdad041a5560597898 to your computer and use it in GitHub Desktop.
Save VladRez/a609bbe7e37946bdad041a5560597898 to your computer and use it in GitHub Desktop.
import org.apache.commons.codec.binary.Base64
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
import org.apache.commons.io.IOUtils
import javax.crypto.Cipher
import javax.crypto.CipherInputStream
import javax.crypto.NoSuchPaddingException
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.security.InvalidAlgorithmParameterException
import java.security.InvalidKeyException
import java.security.Key
import java.security.NoSuchAlgorithmException
import java.util.zip.GZIPInputStream
static void decryptFile( String inputFilePath, String outputFilePath) {
try {
String key = "";
String initVector = "";
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"),
new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8)));
Path inputPath = Paths.get(inputFilePath);
FileInputStream fileInputStream = new FileInputStream(inputPath.toFile())
byte[] readFileAsBytes = IOUtils.toByteArray(fileInputStream)
CipherInputStream cis = new CipherInputStream(new ByteArrayInputStream(readFileAsBytes), cipher);
File inFile = new File(inputFilePath);
File outFile = new File(outputFilePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = new FileInputStream(inFile);
OutputStream os = new FileOutputStream(outFile);
for (int b; (b = cis.read()) != -1; ) {
bos.write(b);
}
byte[] data = Base64.decodeBase64(bos.toString())
OutputStream stream = os;
// stream.write(data);
InputStream inputStream = new ByteArrayInputStream(data);
ByteArrayOutputStream out = new ByteArrayOutputStream();
GzipCompressorInputStream gzip = new GzipCompressorInputStream(inputStream)
IOUtils.copy(gzip, out);
byte[] bytesArray = out.toByteArray();;
stream.write(bytesArray)
stream.flush();
cis.close();
os.flush();
os.close();
}
catch (Exception e) {
print(e.getMessage())
}
}
String inputFilePath = "C:\\inputfile"
String outputFilePathGz = "C:\\outputfile"
String outputFilePathtxt = "C:\\outputfile.txt"
decryptFile(inputFilePath, outputFilePathtxt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment