2

I'm using OpenSSL to encode a string using the following command:

openssl enc -des3 -md md5 -pass pass:mypass -out outfile <<< mytext

It results with salted encoded strings, i can decode it with following command:

openssl enc -d -des3 -md md5 -pass pass:mypass -in outfile

But PHP fails to decode it:

<?php
$secret='mypass';
$key = md5($secret, true);
$key .= substr($key, 0, 8);
echo openssl_decrypt(file_get_contents('outfile'), 'des', $key, OPENSSL_ZERO_PADDING | OPENSSL_RAW_DATA);

Can anyone can provide me a sample code to decode encrypted file given as above using PHP?

Many thanks

Kemal
  • 21
  • 3
  • What is this for `$key .= substr($key, 0, 8);`??? – AbraCadaver Jan 08 '19 at 18:13
  • @AbraCadaver after i failed to decrypt it, i stumbled across https://github.com/iam-raihan/3DES-ECB-Cryptography-in-PHP/blob/master/Crypt_openssl.php and saw that it appends this to key and tried it. sadly didnt work at all – Kemal Jan 08 '19 at 18:17
  • 2
    DES != DES3. Both are 20th century encryption algorithms. Note: We are well into the 21st century. – President James K. Polk Jan 08 '19 at 18:27
  • @JamesKPolk indeed its not safe but i need to use it. by the way i enumerated all the ciphers (`foreach (openssl_get_cipher_methods() as $s) {/*openssl_decrypt(blabla..)`) still couldnt get the decrypted text – Kemal Jan 08 '19 at 18:31
  • 1
    You can keep trying forever until you find the openssl proprietary `EVP_BytesToKey` key derivation function. Note that the resulting ciphertext starts with `Salted__` followed by the 8 byte salt needed for that function. You can find an implementation of the function [in PHP here](https://stackoverflow.com/a/51885310/589259). Don't forget to vote up that poor sap that posted the answer. – Maarten Bodewes Jan 08 '19 at 19:19
  • @MaartenBodewes i just found it too, trying to implement salt now. Thank you so much. edit: i need 15 reputation to upvote :/ – Kemal Jan 08 '19 at 19:22
  • 1
    @Kemal Post your implementation here of your own question as an answer, somebody like me will upvote it, and then you can upvote the other answer. Fixed. I cannot upvote the question itself any more :) – Maarten Bodewes Jan 08 '19 at 19:40

0 Answers0