@ 2024-05-15T22:38:37Z <?php
function m24g($bArr, $modulus, $exponent) {
$publicKey = m23h($modulus, $exponent);
if (openssl_public_encrypt($bArr, $encryptedData, $publicKey, OPENSSL_PKCS1_PADDING)) {
return $encryptedData;
} else {
throw new Exception('Encryption failed: ' . openssl_error_string());
}
}
function m23h($modulus, $exponent) {
$modulus = ensure_even_length($modulus);
$exponent = ensure_even_length($exponent);
$rsa = [
'n' => base64url_encode(hex2bin($modulus)),
'e' => base64url_encode(hex2bin($exponent)),
];
$keyDetails = [
'kty' => 'RSA',
'n' => $rsa['n'],
'e' => $rsa['e'],
];
return jwkToPem($keyDetails);
}
function ensure_even_length($hex) {
return (strlen($hex) % 2 != 0) ? '0' . $hex : $hex;
}
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function base64url_decode($data) {
return base64_decode(strtr($data, '-_', '+/'));
}
function jwkToPem(array $jwk) {
$modulus = base64url_decode($jwk['n']);
$publicExponent = base64url_decode($jwk['e']);
$components = [
'modulus' => $modulus,
'publicExponent' => $publicExponent,
];
$asn1Sequence = createPublicKey($components);
$pem = "-----BEGIN PUBLIC KEY-----\n" .
chunk_split(base64_encode($asn1Sequence), 64, "\n") .
"-----END PUBLIC KEY-----\n";
return $pem;
}
function createPublicKey($components) {
$modulus = $components['modulus'];
$publicExponent = $components['publicExponent'];
$modulus = "\x00" . $modulus; // Ensure positive number
$sequence = pack('Ca*a*', 0x30, createLengthPrefix(strlen($modulus) + strlen($publicExponent) + 4),
pack('Ca*', 0x02, createLengthPrefix(strlen($modulus)) . $modulus) .
pack('Ca*', 0x02, createLengthPrefix(strlen($publicExponent)) . $publicExponent));
return pack('Ca*a*', 0x30, createLengthPrefix(strlen($sequence) + 13),
pack('H*', '300D06092A864886F70D0101010500') .
pack('Ca*', 0x03, createLengthPrefix(strlen($sequence) + 1) . "\x00" . $sequence));
}
function createLengthPrefix($length) {
if ($length < 128) {
return chr($length);
}
$temp = ltrim(pack('N', $length), "\x00");
return chr(0x80 | strlen($temp)) . $temp;
}
// Usage example
try {
$modulus = '113744026899259137585420519641795445506522315843166624587737104823905995993308512746423780711220091509477248711684203860721990053300351123157272120027826616226336455748256059222713368430653450802331260182403534624073361236909650202772541076543992882238347687352553725072578344237693663853480535496066481835463'; // Hexadecimal string of modulus
$exponent = '65537'; // Hexadecimal string of exponent
$dataToEncrypt = "912c1366-d5e0-c63a-e91d-7d6a495da509|147963";
$encryptedData = m24g($dataToEncrypt, $modulus, $exponent);
echo "Encrypted Data: " . base64_encode($encryptedData) . "\n";
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage() . "\n";
}
?>
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) 8.3.12 0.008 0.000 20.74 8.3.11 0.006 0.003 20.94 8.3.10 0.000 0.008 16.89 8.3.9 0.004 0.011 26.77 8.3.8 0.004 0.004 18.55 8.3.7 0.034 0.010 16.58 8.3.6 0.041 0.007 16.74 8.3.5 0.012 0.003 16.75 8.3.4 0.024 0.012 17.59 8.3.3 0.026 0.003 17.61 8.3.2 0.023 0.012 17.50 8.3.1 0.025 0.007 17.48 8.3.0 0.026 0.007 17.63 8.2.24 0.003 0.006 17.57 8.2.23 0.006 0.010 22.58 8.2.22 0.004 0.004 24.06 8.2.21 0.004 0.011 26.77 8.2.20 0.006 0.003 18.18 8.2.19 0.025 0.004 16.58 8.2.18 0.026 0.005 16.63 8.2.17 0.027 0.003 17.50 8.2.16 0.023 0.000 17.47 8.2.15 0.021 0.009 17.63 8.2.14 0.017 0.011 17.38 8.2.13 0.022 0.003 17.38 8.2.12 0.023 0.000 17.73 8.2.11 0.013 0.010 17.63 8.2.10 0.015 0.011 17.63 8.2.9 0.023 0.003 17.63 8.2.8 0.024 0.000 17.75 8.2.7 0.016 0.012 17.50 8.2.6 0.022 0.006 17.62 8.2.5 0.018 0.006 17.50 8.2.4 0.025 0.005 17.23 8.2.3 0.018 0.006 17.38 8.2.2 0.020 0.003 17.75 8.2.1 0.021 0.000 17.50 8.2.0 0.021 0.000 17.75 8.1.30 0.009 0.000 18.12 8.1.29 0.009 0.000 30.84 8.1.28 0.018 0.008 16.58 8.1.27 0.018 0.005 17.48 8.1.26 0.014 0.009 17.38 8.1.25 0.023 0.005 17.50 8.1.24 0.019 0.005 17.38 8.1.23 0.014 0.010 17.36 8.1.22 0.017 0.006 17.38 8.1.21 0.023 0.006 17.25 8.1.20 0.019 0.005 17.23 8.1.19 0.016 0.008 17.23 8.1.18 0.021 0.004 17.10 8.1.17 0.021 0.000 17.35 8.1.16 0.010 0.013 17.23 8.1.15 0.020 0.004 17.25 8.1.14 0.017 0.007 17.34 8.1.13 0.015 0.007 17.11 8.1.12 0.017 0.006 17.38 8.1.11 0.016 0.004 17.46 8.1.10 0.010 0.010 17.35 8.1.9 0.015 0.008 17.47 8.1.8 0.028 0.000 17.47 8.1.7 0.017 0.004 17.47 8.1.6 0.024 0.009 17.59 8.1.5 0.036 0.004 17.25 8.1.4 0.039 0.003 17.38 8.1.3 0.020 0.007 17.59 8.1.2 0.032 0.009 17.60 8.1.1 0.033 0.011 17.46 8.1.0 0.029 0.015 17.00
preferences:dark mode live preview
26.76 ms | 403 KiB | 5 Q