@ 2017-07-13T07:33:44Z <?php
/**
* http://stackoverflow.com/questions/9262109/php-simplest-two-way-encryption/30189841#30189841
*
* This is not safe to use
*/
class UnsafeCrypto
{
const METHOD = 'aes-256-ctr';
/**
* Encrypts (but does not authenticate) a message
*
* @param string $message - plaintext message
* @param string $key - encryption key (raw binary expected)
* @param boolean $encode - set to TRUE to return a base64-encoded
* @return string (raw binary)
*/
public static function encrypt($message, $key, $encode = false)
{
$nonceSize = openssl_cipher_iv_length(self::METHOD);
$nonce = openssl_random_pseudo_bytes($nonceSize);
$ciphertext = openssl_encrypt(
$message,
self::METHOD,
$key,
OPENSSL_RAW_DATA,
$nonce
);
// Now let's pack the IV and the ciphertext together
// Naively, we can just concatenate
if ($encode) {
return base64_encode($nonce.$ciphertext);
}
return $nonce.$ciphertext;
}
/**
* Decrypts (but does not verify) a message
*
* @param string $message - ciphertext message
* @param string $key - encryption key (raw binary expected)
* @param boolean $encoded - are we expecting an encoded string?
* @return string
*/
public static function decrypt($message, $key, $encoded = false)
{
if ($encoded) {
$message = base64_decode($message, true);
if ($message === false) {
throw new Exception('Encryption failure');
}
}
$nonceSize = openssl_cipher_iv_length(self::METHOD);
$nonce = mb_substr($message, 0, $nonceSize, '8bit');
$ciphertext = mb_substr($message, $nonceSize, null, '8bit');
$plaintext = openssl_decrypt(
$ciphertext,
self::METHOD,
$key,
OPENSSL_RAW_DATA,
$nonce
);
return $plaintext;
}
}
$id = md5(uniqid());
var_dump($id);
$key = hex2bin($id);
var_dump($hex);
$message = 'Ready your ammunition; we attack at dawn.';
$encrypted = UnsafeCrypto::encrypt($message, $key);
$decrypted = UnsafeCrypto::decrypt($encrypted, $key);
var_dump($encrypted, $decrypted);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.2.6 0.004 0.011 16.81 7.2.0 0.008 0.004 19.63 7.1.10 0.000 0.012 18.39 7.1.7 0.007 0.010 17.05 7.1.6 0.034 0.004 35.36 7.1.5 0.031 0.013 34.95 7.1.4 0.020 0.013 34.60 7.1.3 0.022 0.013 34.79 7.1.2 0.030 0.006 34.98 7.1.1 0.006 0.012 16.63 7.1.0 0.012 0.009 16.81 7.0.20 0.011 0.005 16.52 7.0.19 0.009 0.012 16.92 7.0.18 0.006 0.010 16.51 7.0.17 0.006 0.012 16.48 7.0.16 0.003 0.014 16.49 7.0.15 0.008 0.009 16.29 7.0.14 0.007 0.013 16.58 7.0.13 0.007 0.013 16.57 7.0.12 0.010 0.010 16.61 7.0.11 0.012 0.006 16.39 7.0.10 0.009 0.006 16.29 7.0.9 0.006 0.010 16.15 7.0.8 0.006 0.012 16.61 7.0.7 0.009 0.012 16.42 7.0.6 0.007 0.013 16.41 7.0.5 0.009 0.012 16.31 7.0.4 0.006 0.010 16.54 7.0.3 0.003 0.013 16.51 7.0.2 0.006 0.010 16.47 7.0.1 0.007 0.010 16.66 7.0.0 0.007 0.010 16.64
preferences:dark mode live preview
31.34 ms | 401 KiB | 5 Q