Untitled

 avatar
user_5113453
php
19 days ago
852 B
3
Indexable
Never
<?php
    $key = "SNGINE_ENCRYPT_MESSAGE";

    function encryptMessage($message, $key) {
      $cipher = "aes-256-cbc";
      $ivlen = openssl_cipher_iv_length($cipher);
      $iv = openssl_random_pseudo_bytes($ivlen);
      $ciphertext = openssl_encrypt($message, $cipher, $key, $options=0, $iv);
      return base64_encode($iv.$ciphertext);
    }
    
    function decryptMessage($encryptedMessage, $key) {
      $cipher = "aes-256-cbc";
      $data = base64_decode($encryptedMessage);
      $ivlen = openssl_cipher_iv_length($cipher);
      $iv = substr($data, 0, $ivlen);
      $ciphertext = substr($data, $ivlen);
      return openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv);
    }
    
    // exampe
    $msg = "Hello World!";
    $encryptedMessage = encryptMessage($msg, $key);
    $decryptMessage = decryptMessage($msg, $key);

?>
Leave a Comment