@ 2024-05-15T22:37:34Z <?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 = gmp_import(hex2bin($modulus));
$exponent = gmp_import(hex2bin($exponent));
$rsa = [
'n' => gmp_strval($modulus, 16),
'e' => gmp_strval($exponent, 16),
];
$keyDetails = [
'kty' => 'RSA',
'n' => base64url_encode(hex2bin($rsa['n'])),
'e' => base64url_encode(hex2bin($rsa['e'])),
];
$pem = jwkToPem($keyDetails);
return $pem;
}
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function jwkToPem(array $jwk) {
$components = array(
'modulus' => base64url_decode($jwk['n']),
'publicExponent' => base64url_decode($jwk['e'])
);
$pem = "-----BEGIN PUBLIC KEY-----\n" .
chunk_split(base64_encode(createPublicKey($components)), 64, "\n") .
"-----END PUBLIC KEY-----\n";
return $pem;
}
function base64url_decode($data) {
return base64_decode(strtr($data, '-_', '+/'));
}
function createPublicKey($components) {
$modulus = $components['modulus'];
$publicExponent = $components['publicExponent'];
$sequence = new \FG\ASN1\Universal\Sequence();
$sequence->addChild(new \FG\ASN1\Universal\Integer($modulus));
$sequence->addChild(new \FG\ASN1\Universal\Integer($publicExponent));
$bitString = new \FG\ASN1\Universal\BitString($sequence->getBinary());
$outerSequence = new \FG\ASN1\Universal\Sequence();
$outerSequence->addChild(new \FG\ASN1\Universal\Sequence());
$outerSequence->addChild($bitString);
return $outerSequence->getBinary();
}
// 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.11 0.004 0.004 16.97 8.3.10 0.007 0.010 17.17 8.3.9 0.008 0.000 26.77 8.3.8 0.006 0.003 18.80 8.3.7 0.048 0.003 16.84 8.3.6 0.055 0.006 16.75 8.3.5 0.010 0.015 16.95 8.3.4 0.038 0.013 17.73 8.3.3 0.035 0.013 17.88 8.3.2 0.028 0.008 17.38 8.3.1 0.008 0.012 17.63 8.3.0 0.020 0.003 17.49 8.2.23 0.010 0.000 20.94 8.2.22 0.010 0.010 37.54 8.2.21 0.011 0.004 26.77 8.2.20 0.006 0.006 18.54 8.2.19 0.022 0.011 16.75 8.2.18 0.025 0.007 16.59 8.2.17 0.014 0.009 17.88 8.2.16 0.021 0.007 17.50 8.2.15 0.033 0.007 17.38 8.2.14 0.014 0.010 17.60 8.2.13 0.019 0.004 17.63 8.2.12 0.018 0.006 17.50 8.2.11 0.022 0.004 17.75 8.2.10 0.027 0.003 17.73 8.2.9 0.037 0.007 17.63 8.2.8 0.032 0.006 17.75 8.2.7 0.030 0.006 17.75 8.2.6 0.028 0.009 17.75 8.2.5 0.030 0.003 17.50 8.2.4 0.026 0.013 17.38 8.2.3 0.042 0.000 17.75 8.2.2 0.036 0.004 17.63 8.2.1 0.034 0.009 17.63 8.2.0 0.038 0.007 17.50 8.1.29 0.010 0.000 30.84 8.1.28 0.036 0.006 16.58 8.1.27 0.031 0.006 17.25 8.1.26 0.029 0.009 17.50 8.1.25 0.017 0.010 17.35 8.1.24 0.019 0.005 17.38 8.1.23 0.024 0.000 17.63 8.1.22 0.018 0.007 17.48 8.1.21 0.028 0.006 17.25 8.1.20 0.034 0.007 17.38 8.1.19 0.026 0.004 17.35 8.1.18 0.024 0.007 17.25 8.1.17 0.022 0.003 17.47 8.1.16 0.038 0.008 17.49 8.1.15 0.028 0.012 17.25 8.1.14 0.032 0.007 17.22 8.1.13 0.032 0.007 17.47 8.1.12 0.036 0.004 17.59 8.1.11 0.029 0.010 17.38 8.1.10 0.027 0.003 17.35 8.1.9 0.025 0.004 17.11 8.1.8 0.022 0.009 17.48 8.1.7 0.026 0.010 17.38 8.1.6 0.034 0.003 17.38 8.1.5 0.023 0.008 17.50 8.1.4 0.026 0.000 17.60 8.1.3 0.026 0.004 17.50 8.1.2 0.030 0.010 17.59 8.1.1 0.027 0.010 17.50 8.1.0 0.029 0.006 17.71
preferences:dark mode live preview
30.83 ms | 403 KiB | 5 Q