Huymada

Keychain

Aug 22nd, 2019 (edited)
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.61 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Keychain
  4.  * @version 10-11-2024
  5.  */
  6. function keychain($ct, $mt, $pw = '') {
  7.  
  8.     /**
  9.      * Encrypt
  10.      */
  11.     if($mt === true) {
  12.         $iv = openssl_random_pseudo_bytes(16);
  13.         $pw = hash_pbkdf2('sha256', $pw, $iv, 1000, 32, true);
  14.         $en = openssl_encrypt($ct, 'AES-256-CBC', $pw, OPENSSL_RAW_DATA, $iv);
  15.         return base64_encode($iv . $en);
  16.     }
  17.  
  18.     /**
  19.      * Decrypt
  20.      */
  21.     if($mt === false) {
  22.         $ct = base64_decode($ct);
  23.         $iv = substr($ct, 0, 16);
  24.         $pw = hash_pbkdf2('sha256', $pw, $iv, 1000, 32, true);
  25.         $ct = substr($ct, 16);
  26.         return openssl_decrypt($ct, 'AES-256-CBC', $pw, OPENSSL_RAW_DATA, $iv);
  27.     }
  28. }
Add Comment
Please, Sign In to add comment