3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; } ?>
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.21, 8.3.0 - 8.3.9
Warning: hex2bin(): Hexadecimal input string must have an even length in /in/X2v6d on line 12 Warning: hex2bin(): Hexadecimal input string must have an even length in /in/X2v6d on line 13 Warning: hex2bin(): Hexadecimal input string must have an even length in /in/X2v6d on line 22 Warning: hex2bin(): Hexadecimal input string must have an even length in /in/X2v6d on line 23 Fatal error: Uncaught Error: Class "FG\ASN1\Universal\Sequence" not found in /in/X2v6d:56 Stack trace: #0 /in/X2v6d(42): createPublicKey(Array) #1 /in/X2v6d(26): jwkToPem(Array) #2 /in/X2v6d(3): m23h(Object(GMP), Object(GMP)) #3 /in/X2v6d(75): m24g('912c1366-d5e0-c...', '113744026899259...', '65537') #4 {main} thrown in /in/X2v6d on line 56
Process exited with code 255.

preferences:
72.08 ms | 404 KiB | 67 Q