Skip to content

Instantly share code, notes, and snippets.

@zguillez
Created June 14, 2023 16:45
Show Gist options
  • Save zguillez/dafe81185aacc3e5bdabfe76f821f26e to your computer and use it in GitHub Desktop.
Save zguillez/dafe81185aacc3e5bdabfe76f821f26e to your computer and use it in GitHub Desktop.
<?php
$plaintext = "test@test.com";
$key = "secret";
$method = "AES-256-CBC";
$iv_length = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($iv_length);
$ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv);
$ciphertext = base64_encode($iv . $ciphertext);
echo "cifrado: " . $ciphertext . "\n";
$ciphertext = base64_decode($ciphertext);
$iv_decoded = substr($ciphertext, 0, $iv_length);
$ciphertext_decoded = substr($ciphertext, $iv_length);
$plaintext_decoded = openssl_decrypt($ciphertext_decoded, $method, $key, OPENSSL_RAW_DATA, $iv_decoded);
echo "descifrado: " . $plaintext_decoded . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment