openssl_encrypt($string, $method, $key, $options, $iv)
<?PHP $token = 'The quick brown fox jumps over the lazy dog.'; $cipher_method = 'aes-128-ctr'; $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE); $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method)); $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . '::' . bin2hex($enc_iv); unset($token, $cipher_method, $enc_key, $enc_iv);?>Encrypted string: KZ4LurHESC0Y8/Ufy1wsio6aaYXW7m7KVuW8NBKQhE5CnLspz+540p1ClhIZvKNx::254f830c42c937fb7e1e2444c632a8a4
<?PHP list($crypted_token, $enc_iv) = explode('::', $crypted_token);; $cipher_method = 'aes-128-ctr'; $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE); $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv)); unset($crypted_token, $cipher_method, $enc_key, $enc_iv);?>Decrypted string: The quick brown fox jumps over the lazy dog.
// Original PHP code by Chirp Internet: www.chirp.com.au// Please acknowledge use of this code by including this header.namespace Chirp;class Cryptor{ protected $method = 'aes-128-ctr'; // default cipher method if none supplied private $key; protected function iv_bytes() { return openssl_cipher_iv_length($this->method); } public function __construct($key = FALSE, $method = FALSE) { if(!$key) { $key = php_uname(); // default encryption key if none supplied } if(ctype_print($key)) { // convert ASCII keys to binary format $this->key = openssl_digest($key, 'SHA256', TRUE); } else { $this->key = $key; } if($method) { if(in_array(strtolower($method), openssl_get_cipher_methods())) { $this->method = $method; } else { die(__METHOD__ . ': unrecognised cipher method: {$method}'); } } } public function encrypt($data) { $iv = openssl_random_pseudo_bytes($this->iv_bytes()); return bin2hex($iv) . openssl_encrypt($data, $this->method, $this->key, 0, $iv); } // decrypt encrypted string public function decrypt($data) { $iv_strlen = 2 * $this->iv_bytes(); if(preg_match('/^(.{' . $iv_strlen . '})(.+)$/', $data, $regs)) { list(, $iv, $crypted_string) = $regs; if(ctype_xdigit($iv) && strlen($iv) % 2 0) { return openssl_decrypt($crypted_string, $this->method, $this->key, 0, hex2bin($iv)); } } return FALSE; // failed to decrypt }}
<?PHP use ChirpCryptor; $token = 'The quick brown fox jumps over the lazy dog.'; $encryption_key = 'CKXH2U9RPY3EFD70TLS1ZG4N8WQBOVI6AMJ5'; $cryptor = new Cryptor($encryption_key); $crypted_token = $cryptor->encrypt($token); unset($token);?>
<?PHP use ChirpCryptor; $encryption_key = 'CKXH2U9RPY3EFD70TLS1ZG4N8WQBOVI6AMJ5'; $cryptor = new Cryptor($encryption_key); $token = $cryptor->decrypt($crypted_token);?>
<?PHP use ChirpCryptor; // using the default encryption key and cipher method $cryptor = new Cryptor(); $token = $cryptor->decrypt($crypted_token); // using a custom key and the default cipher method $encryption_key = 'h5g91gU0pIFItCR1'; $cryptor = new Cryptor($encryption_key); $token = $cryptor->decrypt($crypted_token); // using a custom key and specifying the cipher method $encryption_key = 'S9f0Mitmss6U7Jhe'; $cipher_method = 'aes-128-cfb'; $cryptor = new Cryptor($encryption_key, $cipher_method); $token = $cryptor->decrypt($crypted_token);?>