fork download
  1. <?php
  2.  
  3. $out = fopen('php://stdout', "w");
  4.  
  5. define('ENCRYPTION_KEY', 'ab86d144e3f080b61c7c2e43');
  6.  
  7. // Encrypt
  8. $src = "Тестируем обратимое шифрование на php 7";
  9.  
  10. // Encrypt
  11. function encrypt($text,$key)
  12. {
  13. $ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
  14. $iv = openssl_random_pseudo_bytes($ivlen);
  15. $ciphertext_raw = openssl_encrypt($text, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
  16. $hmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
  17. $ciphertext = base64_encode( $iv.$hmac.$ciphertext_raw );
  18. $input = array("=", "b", "c", "d", "e", "f", "g", "j", "k", "y", "a");
  19. $output = array("!№", "#", "|", "%", "@", "-", "_", ")", "$", "^", "&:&");
  20. var_dump($ciphertext);
  21. $ciphertext = str_replace($input, $output, $ciphertext);
  22. var_dump($ciphertext);
  23. return $ciphertext;
  24. }
  25. // Decrypt
  26. function decrypt($hash,$key)
  27. {
  28. $input = array("=", "b", "c", "d", "e", "f", "g", "j", "k", "y", "a");
  29. $output = array("!№", "#", "|", "%", "@", "-", "_", ")", "$", "^", "&:&");
  30. var_dump($hash);
  31. $hash = str_replace($output, $input, $hash);
  32. var_dump($hash);
  33.  
  34. $c = base64_decode($hash);
  35. //var_dump($c);
  36. $ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
  37. $iv = substr($c, 0, $ivlen);
  38. $hmac = substr($c, $ivlen, $sha2len=32);
  39. $ciphertext_raw = substr($c, $ivlen+$sha2len);
  40. $plaintext = openssl_decrypt($ciphertext_raw, $cipher, $key, $options=OPENSSL_RAW_DATA, $iv);
  41. $calcmac = hash_hmac('sha256', $ciphertext_raw, $key, $as_binary=true);
  42. var_dump(hash_equals($hmac, $calcmac));
  43. if (hash_equals($hmac, $calcmac))
  44. {
  45. return $plaintext;
  46. }
  47. }
  48.  
  49. fwrite($out, sprintf("%s\n", $src));
  50. $encrypted = encrypt($src, ENCRYPTION_KEY);
  51. //fwrite($out, sprintf("%s\n", $encrypted));
  52.  
  53. $decrypted = decrypt($encrypted, ENCRYPTION_KEY);
  54. fwrite($out, sprintf("%s\n", $decrypted));
  55.  
  56.  
  57. fclose($out);
  58.  
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
Тестируем обратимое шифрование на php 7
string(172) "9LPPzKu3a5loy2Vfa6z02oD+xuQcE8GVM1uT+JAMKx968HnaMp0zHGKe4UNW3Q7zpCCgfuTbaQeiyz5IL0z0AEse4KWYNwQ1hPEttswxglOyc240cDxuzOMx/0sl6Dg9m/SGsTckAAPjs+USHvd1RnwfKXcuYK/QdMqnipN7RZ4="
string(183) "9LPPzKu3&:&5lo^2V-&:&6z02oD+xuQ|E8GVM1uT+JAMKx968Hn&:&Mp0zHGK@4UNW3Q7zpCC_-uT#&:&Q@i^z5IL0z0AEs@4KWYNwQ1hPEttswx_lO^|240|DxuzOMx/0sl6D_9m/SGsT|$AAP)s+USHv%1Rnw-KX|uYK/Q%MqnipN7RZ4!№"
string(183) "9LPPzKu3&:&5lo^2V-&:&6z02oD+xuQ|E8GVM1uT+JAMKx968Hn&:&Mp0zHGK@4UNW3Q7zpCC_-uT#&:&Q@i^z5IL0z0AEs@4KWYNwQ1hPEttswx_lO^|240|DxuzOMx/0sl6D_9m/SGsT|$AAP)s+USHv%1Rnw-KX|uYK/Q%MqnipN7RZ4!№"
string(172) "9LPPzKu3a5loy2Vfa6z02oD+xuQcE8GVM1uT+JAMKx968HnaMp0zHGKe4UNW3Q7zpCCgfuTbaQeiyz5IL0z0AEse4KWYNwQ1hPEttswxglOyc240cDxuzOMx/0sl6Dg9m/SGsTckAAPjs+USHvd1RnwfKXcuYK/QdMqnipN7RZ4="
bool(true)
Тестируем обратимое шифрование на php 7