<?php
$res = openssl_pkey_new([
'digest_alg' => 'sha256',
'private_key_bite' => 2048,
'private_key_type' => OPENSSL_TYPE_RSA
]);
openssl_pkey_export($res, $privateKey);
$publicKey = openssl_pkey_get_details($res)['key'];
$message = 'Prime Numbers Rock!';
$aesKey = random_bytes(32);
$keyE = hash_hmac('sha256', 'Encryption Key', $aesKey, true);
$keyA = hash_hmac('sha256', 'Authentication Key', $aesKey, true);
$iv = random_bytes(12);
$ciphertext = $iv . openssl_encrypt($message, 'aes-256-ctr', $keyE, OPENSSL_RAW_DATA, $iv);
$mac = hash_hmac('sha256', $ciphertext, $keyA, true);
$combined = $mac . $ciphertext;
$rsaCipher = '';
openssl_public_encrypt($key, $rsaCihper, $publicKey, OPENSSL_PKCS1_OAEP_PADDING);
$sendMe = $rsaCipher . $combined;
var_dump(strlen($rsaCipher), strlen($combined), strlen($sendMe));
preferences:
30.75 ms | 406 KiB | 5 Q