<?php
$algo = 'aes-256-gcm';
$iv = random_bytes(openssl_cipher_iv_length($algo));
echo(openssl_cipher_iv_length($algo));
echo("\n");
$key = random_bytes(32); // 256 bit
//$data = random_bytes(1024); // 1 Kb of random data
$data = "well hello there with insane length and soooooooooooooooooooooooooooooooooooooooo much data in it. Bigger better stronger D:";
$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));
echo("\n");
echo(mb_strlen($tag, '8bit'));
$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 ("\nDecryption %s\n", $data === $decrypt ? 'Ok' : 'Failed');
- Output for 7.1.7
- 12
UaAT34kuH5/3MpUb2PKC392pUQ9YTUBojmLwNSB34X/48zOdbdS+iXF56+LYcACxL6Hz77CI5Fi+NuMmZhsTTsl+0gDKws210tRRbaI17bnrf8Sw1i0YQ38KT6v74eLPBQgdmI0e+nnbQXr6+U5JoSw0t6+8A0CEEEiXGg==
0cn02TQKHVySXIjveEnAoQ==
0cn02TQKHVySXIjveEnAoQ==
16
Decryption Ok
preferences:
41.88 ms | 401 KiB | 6 Q