Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Fix issue with decryption for standard jre.
Browse files Browse the repository at this point in the history
  • Loading branch information
clienthax committed Nov 27, 2017
1 parent 8cf7b4a commit 824f05b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ task fatJar(type: Jar) {
'Main-Class': 'moe.clienthax.crunched.Crunched'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
from { configurations.compile.collect
{
it.isDirectory() ? it : zipTree(it)
}
}
{
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}

Expand All @@ -31,4 +40,5 @@ dependencies {
compile group: 'me.tongfei', name: 'progressbar', version: '0.5.5'
compile group: 'org.apache.commons', name: 'commons-exec', version: '1.3'
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.58'
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package moe.clienthax.crunched.subtitles;

import org.apache.commons.io.IOUtils;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.URL;
import java.security.MessageDigest;
import java.util.Base64;
Expand All @@ -22,6 +34,32 @@
@SuppressWarnings("SameParameterValue")
class SubtitleDecrypter {


//Decrypt with BC to get past the jce limited bits on lower java versions... ugh
public static byte[] lwDecrypt(byte[] iv, byte[] key, byte[] encrypted) throws Exception {

BufferedBlockCipher aes = new BufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(false, ivAndKey);

byte[] decryptedBytes = cipherData(aes, encrypted);
return decryptedBytes;

}

private static byte[] cipherData(BufferedBlockCipher cipher, byte[] data)
throws Exception {
int minSize = cipher.getOutputSize(data.length);
byte[] outBuf = new byte[minSize];
int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
int length2 = cipher.doFinal(outBuf, length1);
int actualLength = length1 + length2;
byte[] result = new byte[actualLength];
System.arraycopy(outBuf, 0, result, 0, result.length);
return result;
}


//TODO make this use bouncy castle because javas aes impl is limited by default for dumb reasons..
static void decryptAndConvertSubtitle(File folder, SubtitleInfo subtitle) {
//Here be dragons...
Expand Down Expand Up @@ -50,10 +88,13 @@ static void decryptAndConvertSubtitle(File folder, SubtitleInfo subtitle) {
byte[] ivBytes = Base64.getDecoder().decode(ivString);
byte[] dataBytes = Base64.getDecoder().decode(dataString);

/*
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptionKey, "AES"), new IvParameterSpec(ivBytes));
byte[] zlibData = cipher.doFinal(dataBytes);
*/
byte[] zlibData = lwDecrypt(ivBytes, decryptionKey, dataBytes);
byte[] decompressed = decompressZlib(zlibData);

String xml = new String(decompressed);
Expand Down

0 comments on commit 824f05b

Please sign in to comment.