Reputation: 4448
We have some files that were encrypted using Java Bouncy Castle's "PBEWITHSHA256AND128BITAES-CBC-BC" algorithm.
What is the best library to use in PHP to decrypt these files using PHP?
Upvotes: 0
Views: 2245
Reputation: 7401
The AES decryption can be done via this:
function decrypt($encrypted, $key, $iv) {
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
$padSize = ord(substr($decrypted, -1));
return substr($decrypted, 0, $padSize*-1);
}
$key
, $iv
and $encrypted
should be binary strings.
The substr and pad size detection are necessary because 128BITAES-CBC-BC
uses PKCS5 padding which PHP's mcrypt
functions doesn't support (PHP always pads with zeros).
The harder part is going to be calculating the encryption key. Bouncy Castle's PBEWITHSHA256
uses PKCS12 with SHA256 to derive the key. The issue here is that I don't know of (and haven't been able to find) any implementation of this algorithm in PHP, and honestly I don't think you'll find one. If all your data is encrypted with a single key, I would recommend debugging the Bouncy Castle library and extracting the key bytes at the start of the encryption/decryption process. You should be able to catch the key if you set a breakpoint in the generateWorkingKey(byte[], boolean)
method of the org.bouncycastle.crypto.engines.AESFastEngine
class.
Upvotes: 3