@ 2017-07-13T07:33:59Z <?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($key);
$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.0 0.003 0.010 19.28 7.1.10 0.003 0.009 18.10 7.1.7 0.010 0.007 17.46 7.1.6 0.031 0.015 35.35 7.1.5 0.028 0.025 35.11 7.1.4 0.033 0.013 34.83 7.1.3 0.022 0.019 34.76 7.1.2 0.091 0.009 33.11 7.1.1 0.002 0.025 16.79 7.1.0 0.012 0.010 16.87 7.0.20 0.014 0.007 16.89 7.0.19 0.007 0.017 16.70 7.0.18 0.011 0.011 16.27 7.0.17 0.009 0.011 16.48 7.0.16 0.100 0.003 14.71 7.0.15 0.008 0.016 16.46 7.0.14 0.005 0.017 16.46 7.0.13 0.008 0.018 16.68 7.0.12 0.015 0.006 16.75 7.0.11 0.011 0.014 16.29 7.0.10 0.012 0.013 16.32 7.0.9 0.011 0.011 16.32 7.0.8 0.018 0.004 16.20 7.0.7 0.010 0.013 16.53 7.0.6 0.007 0.016 16.12 7.0.5 0.007 0.017 16.30 7.0.4 0.014 0.008 16.64 7.0.3 0.006 0.018 16.36 7.0.2 0.014 0.005 16.47 7.0.1 0.003 0.021 16.58 7.0.0 0.019 0.006 16.53
preferences:dark mode live preview
31.71 ms | 400 KiB | 5 Q