Skip to content

Instantly share code, notes, and snippets.

@alsanchez
Created October 3, 2015 12:01
Show Gist options
  • Save alsanchez/5a524b53fcc5733e4a92 to your computer and use it in GitHub Desktop.
Save alsanchez/5a524b53fcc5733e4a92 to your computer and use it in GitHub Desktop.
package com.company;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.Security;
public class Main
{
public static void main(String[] args) throws Exception
{
try
{
String PASSWORD = "JB_MR1.1";
String IV = "A70DC03BF2D64B036A845F15531A7E74";
String SALT = "3BC5AC2EB90BA29C02A0CA0286B0CFCB";
String TRANSFORMATION = "AES/CBC/PKCS5Padding";
String ALGORITHM = "PBEwithSHA256and256bitAES-CBC-BC";
byte[] iv = toByteArray(IV);
byte[] salt = toByteArray(SALT);
Security.addProvider(new BouncyCastleProvider());
PBEKeySpec ks = new PBEKeySpec(PASSWORD.toCharArray(), salt, 1000, 256);
SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey secretKey = skf.generateSecret(ks);
SecretKeySpec newKey = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, newKey, new IvParameterSpec(iv));
byte[] data = loadFile();
//data = Arrays.copyOfRange(data, 32, data.length);
byte[] decData = cipher.doFinal(data);
saveFile(decData);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
private static void saveFile(byte[] bytes) throws IOException
{
FileOutputStream fos = new FileOutputStream("/home/alex/data.decrypted");
fos.write(bytes);
fos.close();
}
private static byte[] loadFile() throws IOException
{
RandomAccessFile f = new RandomAccessFile("/home/alex/Downloads/data.bin", "r");
byte[] b = new byte[(int)f.length()];
f.read(b);
return b;
}
public static byte[] toByteArray(String s) {
return DatatypeConverter.parseHexBinary(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment