Virajsinh

encrypt or decrypt a string

Dec 16th, 2019
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. function my_simple_crypt( $string, $action = 'e' ) {
  2.     // you may change these values to your own
  3.     $secret_key = 'my_simple_secret_key';
  4.     $secret_iv = 'my_simple_secret_iv';
  5.  
  6.     $output = false;
  7.     $encrypt_method = "AES-256-CBC";
  8.     $key = hash( 'sha256', $secret_key );
  9.     $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 );
  10.  
  11.     if( $action == 'e' ) {
  12.         $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) );
  13.     }
  14.     else if( $action == 'd' ){
  15.         $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv );
  16.     }
  17.  
  18.     return $output;
  19. }
  20. //encrypte string
  21. $encrypted = my_simple_crypt( 'Hello World!', 'e' );
  22.  
  23. //decrypted string
  24. $decrypted = my_simple_crypt( 'RTlOMytOZStXdjdHbDZtamNDWFpGdz09', 'd' );
Add Comment
Please, Sign In to add comment