kakatoji

encryptdandecript model openssl

Feb 24th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. //encrypt dcrypt sederhana
  4. //keisengan untuk keisengan
  5. //created by kakatoji
  6.  
  7. function encrypt_decrypt($action, $string) {
  8. $output = false;
  9. $encrypt_method = "AES-256-CBC";
  10. $secret_key = 'key_one';
  11. $secret_iv = 'key_two';
  12. $key = hash('sha256', $secret_key);
  13. $iv = substr(hash('sha256', $secret_iv), 0, 16);
  14. if ( $action == 'encrypt' ) {
  15. $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
  16. $output = base64_encode($output);
  17. } else if( $action == 'decrypt' ) {
  18. $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
  19. }
  20. return $output;
  21. }
  22. // Contoh Penggunaan
  23. $plain_txt = "hai salam kenal saya kakatoji";
  24. $encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
  25. $decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
  26. echo $encrypted_txt;
Add Comment
Please, Sign In to add comment