0

thanks so much for reading.

I did so much research on this topic but did not get any further.

I have to decode data in PHP. This is the specification of the encryption (and there is no more specification to get from the encryptor):

keylength = 256
algorithm = AES/CBC/PKCS5Padding
keyspec = PBKDF2withHmacSHA1
iterations = 5000

What I also got, is a key, 49 characters long for decryption.

Each encrypted message is provided in an Array and it's base64 encoded

Example data:

$data = [
    "iv" => "DoJQNS0WZRtWB...",
    "salt" => "zkcHInm4ewweKG81...",
    "encrypted_data" => "30MTuQEW4sVc3...",
];

I tried this:

$password = "supersecretkey";
$salt     = base64_decode($data['salt']);
$iterations = 5000;

$key_length = 32;
$is_raw_output = true;


$key = hash_pbkdf2("sha1", $password, $salt, $iterations, $key_length, true);


$iv = base64_decode($data['iv']);
$encstr = base64_decode($data['encrypted_data']);

$output = openssl_decrypt($encstr, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING, $iv);

var_dump($output);
var_dump(openssl_error_string());

what I get here is

bool(false)

string(94) "error:0606508A:digital envelope routines:EVP_DecryptFinal_ex:data not multiple of block length"

I'm not sure if this is the correct approach. I tried also decoding directly without using the hash_pbkdf2 function but all I receive then is garbage.

So if I just use this

$key = "supersecretkey";
$iv = base64_decode($data['iv']);
$encstr = base64_decode($data['encrypted_data']);
$output = openssl_decrypt($encstr, 'AES-256-CBC', $key, OPENSSL_ZERO_PADDING| OPENSSL_RAW_DATA, $iv);

then I just get garbage data like

*+�l��_�y9�{(kNF7��gص��[Se!Y

I would appreciate any help on this topic as I already spent a lot of hours and read tons of documentation.

Unfortunately I do not get any more support from the entity that sends the encrypted data.

Thanks in advance!

chingo81
  • 61
  • 5
  • https://stackoverflow.com/questions/45567962/convert-encrypt-and-decrypt-c-sharp-function-to-php-function/45574121#45574121 something like that ? – FatFreddy Jun 15 '22 at 11:07
  • Only `OPENSSL_RAW_DATA` may be set as `$options` (4th parameter) because 1. the ciphertext is explicitly Base64 decoded and 2. according to the description PKCS#7 (or PKCS#5) padding is used. Presumably (with respect to your test) this choice results in a decryption error or empty output, which is caused by inconsistent data and/or a wrong specification of the algorithm parameters. For an analysis either the encryption code is needed or a *complete* set of test data: password, salt, IV, plaintext and ciphertext. – Topaco Jun 15 '22 at 14:18
  • Thanks for your answers. If I use the answer of FatFreddy, where to place the "salt"? And Topaco, do you think I need ```hash_pbkdf2```, is this the right approach? They do not provide the encryption code and I cannot post the real data for Privacy reasons. The encryptor is not very supportive and says the provided information is enough for decryption – chingo81 Jun 15 '22 at 15:47
  • *PBKDF2withHmacSHA1* corresponds to `hash_pbkdf2()` with `'sha1'` as 1st parameter, i.e. this is fine. Apart from the `$options` (i.e. the 4th) parameter, which should be `OPENSSL_RAW_DATA`, I can't see any error in the code, see also [here](https://paiza.io/projects/UDOtWszBlCm7zYzGwHjK4w), so the possibility of inconsistent data and/or algorithm description should also be considered. Btw, the linked code from the comment uses SHA256 as key derivation and no PBKDF2, which does not fit to the algorithm description. – Topaco Jun 15 '22 at 16:13

1 Answers1

0

Hi thanks all for helping me out.

The correct solution to this problem is below.

Obviously we were sent an incorrect encryption key. They generated a new one - but they insisted that the old one was correct - and then it worked like charm!

Thanks again.

$salt     = base64_decode($datasend['salt']);
$iterations = 5000;

$key_length = 32;

$key = hash_pbkdf2("sha1", $e2ekey, $salt, $iterations, $key_length, true);

$iv = base64_decode($datasend['iv']);
$encstr = base64_decode($datasend['encryptedMessage']);

$datasend = openssl_decrypt($encstr, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);

$datasend = json_decode($datasend,true);

chingo81
  • 61
  • 5