@ 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.
Output for git.master , git.master_jit , rfc.property-hooks Fatal error: Uncaught Error: Call to undefined function openssl_public_encrypt() in /in/EuNF0:4
Stack trace:
#0 /in/EuNF0(88): m24g('912c1366-d5e0-c...', '113744026899259...', '65537')
#1 {main}
thrown in /in/EuNF0 on line 4
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
27.89 ms | 405 KiB | 5 Q