Skip to content

Instantly share code, notes, and snippets.

@romanman
Created February 16, 2015 15:34
Show Gist options
  • Save romanman/b1a379339afe49e5c65b to your computer and use it in GitHub Desktop.
Save romanman/b1a379339afe49e5c65b to your computer and use it in GitHub Desktop.
private static byte[] decrypt(byte[] data, byte[] key)
{
// 16 bytes is the IV size for AES256
try
{
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
byte[] ivBytes = new byte[16];
System.arraycopy(data, 0, ivBytes, 0, ivBytes.length); // Get iv from data
byte[] dataonly = new byte[data.length - ivBytes.length];
System.arraycopy(data, ivBytes.length, dataonly, 0, data.length - ivBytes.length);
cipher.init(false, new ParametersWithIV(new KeyParameter(key), ivBytes));
byte[] decrypted = new byte[cipher.getOutputSize(dataonly.length)];
int len = cipher.processBytes(dataonly, 0, dataonly.length, decrypted,0);
len += cipher.doFinal(decrypted, len);
return decrypted;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment