<?php
// Original PHP code by Chirp Internet: www.chirp.com.au
// Please acknowledge use of this code by including this header.
class Cryptor
{
protected $method = 'AES-128-CTR'; // default
private $key;
protected function iv_bytes()
{
return openssl_cipher_iv_length($this->method);
}
public function __construct($key = FALSE, $method = FALSE)
{
if(!$key) {
// if you don't supply your own key, this will be the default
$key = gethostname() . "|" . ip2long($_SERVER['SERVER_ADDR']);
}
if(ctype_print($key)) {
// convert key to binary format
$this->key = openssl_digest($key, 'SHA256', TRUE);
} else {
$this->key = $key;
}
if($method) {
if(in_array($method, openssl_get_cipher_methods())) {
$this->method = $method;
} else {
die(__METHOD__ . ": unrecognised encryption method: {$method}");
}
}
}
public function encrypt($data)
{
$iv = openssl_random_pseudo_bytes($this->iv_bytes());
$encrypted_string = bin2hex($iv) . openssl_encrypt($data, $this->method, $this->key, 0, $iv);
return $encrypted_string;
}
// 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;
$decrypted_string = openssl_decrypt($crypted_string, $this->method, $this->key, 0, hex2bin($iv));
return $decrypted_string;
} else {
return FALSE;
}
}
}
$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);
echo $crypted_token;
preferences:
56.98 ms | 402 KiB | 5 Q