Skip to content

Instantly share code, notes, and snippets.

@rtio
Created June 22, 2017 20:07
Show Gist options
  • Select an option

  • Save rtio/79d014f2ada72ccc3ea9e16724f2382c to your computer and use it in GitHub Desktop.

Select an option

Save rtio/79d014f2ada72ccc3ea9e16724f2382c to your computer and use it in GitHub Desktop.
<?php
const SECRET_KEY = '2ff58dd1339f33ea688b89753d50e8cfefe82f6fdfb429f4f1dad4e45c04d499'; // Example key
const ENCRYPT_METHOD = 'AES-256-CBC'; // Encrypt method
const IV = '2ff58dd1339f33ea'; // Random hash with 16 bits. Generate a new IV for each new message
$nonEncryptedMessage = 'this message is secret';
// example to encode a message
// encrypt and encode the message
$encryptedMessage = openssl_encrypt($nonEncryptedMessage, ENCRYPT_METHOD, SECRET_KEY, 0, IV);
$encondedMessage = base64_encode($encryptedMessage);
print $encondedMessage;
// example to decode a message
// use the same sent secret key and iv
// decode and decrypt message
$decodedMessage = base64_decode($encondedMessage);
$decryptedMessage = openssl_decrypt($decodedMessage, ENCRYPT_METHOD, SECRET_KEY, 0, IV);
print $decryptedMessage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment