0

I try to implement Crypt::encrypt function in php and this code is here:

$key = "ygXa6pBJOWSAClY/J6SSVTjvJpMIiPAENiTMjBrcOGw=";
$iv = random_bytes(16);
$value = \openssl_encrypt(serialize('123456'), 'AES-256-CBC', $key, 0, $iv);
bIv = base64_encode($iv);
$mac = hash_hmac('sha256', $bIv.$value, $key);
$c = ['iv'=>$bIv,'value'=>$value,'mac'=>$mac];
$json = json_encode($c);
$b = base64_encode($json);  

But result is wrong.
I am thinking i should do something on $key before set in openssl_encrypt function.
Please help.
Thank you.

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
  • dear can you explain your Question it not clear what you asking about ? – Ramzan Mahmood Dec 26 '16 at 06:50
  • Crypt::encrypt('123456'); i want implement this laravel function in pure php. @recoverymen – reza_khalafi Dec 26 '16 at 06:53
  • 1
    see the answer below with 37 rate http://stackoverflow.com/questions/15194663/encrypt-and-decrypt-md5 – Ramzan Mahmood Dec 26 '16 at 06:57
  • Result must be something like this: eyJpdiI6Im1RT2pLeHEyNXRmRkFialdnYlFBU0E9PSIsInZhbHVlIjoiOGduRnRjSkFjbWhvQlNIWlNKZU1jdz09IiwibWFjIjoiNzdmM2FlNTJhZjU3NmYxYWNkZDQ4NmFjM2MzYjEyZTNjZDI5MGI0OWVhYmMzNmQ4Mzg4NTRhOGU3MzA1ZTE4NyJ9 – reza_khalafi Dec 26 '16 at 07:06
  • $key before using in encrypt function convert to something like this: ���I9d� V?'��U8�&���B$̌�8l���I9d� V?'��U8�&���B$̌�8l i don't know what is this function that change $key. – reza_khalafi Dec 26 '16 at 07:10
  • http://stackoverflow.com/questions/40945983/how-to-implement-laravel-function-cryptencrypt-in-objective-c – reza_khalafi Dec 31 '16 at 06:59

2 Answers2

3

SOLVED:
We can implement this method like this:

$text = '123456';
$key = "ygXa6pBJOWSAClY/CFEdOTjvJpMIiPAMQiTMjBrcOGw=";
$key = (string)base64_decode($key);
$iv = random_bytes(16);
$value = \openssl_encrypt(serialize($text), 'AES-256-CBC', $key, 0, $iv);  
$bIv = base64_encode($iv);
$mac = hash_hmac('sha256', $bIv.$value, $key); 
$c_arr = ['iv'=>$bIv,'value'=>$value,'mac'=>$mac];
$json = json_encode($c_arr);
$crypted = base64_encode($json);
echo $crypted;  

This work tor me.
enjoy :)
Be Successful

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
0

Here is the implementation, directly from the official source code.

public function encrypt($value)
{
    $iv = random_bytes(16);
    $value = \openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv);
    if ($value === false) {
        throw new EncryptException('Could not encrypt the data.');
    }
    // Once we have the encrypted value we will go ahead base64_encode the input
    // vector and create the MAC for the encrypted value so we can verify its
    // authenticity. Then, we'll JSON encode the data in a "payload" array.
    $mac = $this->hash($iv = base64_encode($iv), $value);
    $json = json_encode(compact('iv', 'value', 'mac'));
    if (! is_string($json)) {
        throw new EncryptException('Could not encrypt the data.');
    }
    return base64_encode($json);
}
  • $iv should be the same as in the source
  • $this->key is the encryption key you set in your .env file, encoded in b64
  • $this->cipher should be the one you configured in your laravel configurations and compatible to your key-length.

In your example, you have set your $key to the value after the "base64:"-string, which is not the key. You need to encode the key with base64 before passing it.

So the the $key to the base64 encode of ygXa6pBJOWSAClY/J6SSVTjvJpMIiPAENiTMjBrcOGw=, which is eWdYYTZwQkpPV1NBQ2xZL0o2U1NWVGp2SnBNSWlQQUVOaVRNakJyY09Hdz0K

manniL
  • 7,157
  • 7
  • 46
  • 72