@ 2017-07-10T09:46:11Z <?php
$algo = 'aes-256-gcm';
$iv = random_bytes(openssl_cipher_iv_length($algo));
$key = random_bytes(32); // 256 bit
//$data = random_bytes(1024); // 1 Kb of random data
$data = "well hello there";
$ciphertext = openssl_encrypt(
$data,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
// Change 1 bit in ciphertext
// $i = rand(0, mb_strlen($ciphertext, '8bit') - 1);
// $ciphertext[$i] = $ciphertext[$i] ^ chr(1);
echo(base64_encode($ciphertext));
echo("\n");
echo(base64_encode($tag));
$i = rand(0, mb_strlen($tag, '8bit') - 1);
$tag[$i] = $tag[$i] ^ chr(1);
echo("\n");
echo(base64_encode($tag));
$decrypt = openssl_decrypt(
$ciphertext,
$algo,
$key,
OPENSSL_RAW_DATA,
$iv,
$tag
);
if (false === $decrypt) {
throw new Exception(sprintf(
"OpenSSL error: %s", openssl_error_string()
));
}
printf ("Decryption %s\n", $data === $decrypt ? 'Ok' : 'Failed');
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.25 , 7.3.0 - 7.3.12 , 7.4.0 Fatal error: Uncaught Error: Call to undefined function openssl_cipher_iv_length() in /in/Z6jTA:3
Stack trace:
#0 {main}
thrown in /in/Z6jTA on line 3
Process exited with code 255 . Output for 5.6.0 - 5.6.40 Fatal error: Call to undefined function random_bytes() in /in/Z6jTA on line 3
Process exited with code 255 . preferences:dark mode live preview
145.32 ms | 401 KiB | 154 Q