2

According to this tutorial https://www.the-art-of-web.com/php/two-way-encryption/ I try to encrypt and decrypt a string.

The encryption works well:

  $token = "The quick brown fox jumps over the lazy dog.";

  $cipher_method = 'aes-128-ctr';
  $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
  $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
  $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
  unset($token, $cipher_method, $enc_key, $enc_iv);

  var_dump($crypted_token);

The output is KZ4LurHESC0Y8/Ufy1wsio6aaYXW7m7KVuW8NBKQhE5CnLspz+540p1ClhIZvKNx::254f830c42c937fb7e1e2444c632a8a4

But when I want to decrypt it again:

$crypted_token = "KZ4LurHESC0Y8/Ufy1wsio6aaYXW7m7KVuW8NBKQhE5CnLspz+540p1ClhIZvKNx::254f830c42c937fb7e1e2444c632a8a4";

  list($crypted_token, $enc_iv) = explode("::", $crypted_token);;
  $cipher_method = 'aes-128-ctr';
  $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
  $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
  unset($crypted_token, $cipher_method, $enc_key, $enc_iv);

  var_dump($token);

I get the output:

"m PHĝ��Jt�nx���l����$�۩!Z��� [b���f�" 

As I am expecting:

The quick brown fox jumps over the lazy dog.
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
peace_love
  • 6,229
  • 11
  • 69
  • 157

2 Answers2

1

Nothing wrong in code. php_uname() disabled. enable php_uname() and try again.

0

This means that the response is in binary format. Try to convert it using hex2bin() and see what you get.

IhariR
  • 21
  • 4