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"; } ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 16
Branch analysis from position: 16
2 jumps found. (Code = 107) Position 1 = 17, Position 2 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X2v6d
function name:  (null)
number of ops:  23
compiled vars:  !0 = $modulus, !1 = $exponent, !2 = $dataToEncrypt, !3 = $encryptedData, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   ASSIGN                                                   !0, '113744026899259137585420519641795445506522315843166624587737104823905995993308512746423780711220091509477248711684203860721990053300351123157272120027826616226336455748256059222713368430653450802331260182403534624073361236909650202772541076543992882238347687352553725072578344237693663853480535496066481835463'
   72     1        ASSIGN                                                   !1, '65537'
   73     2        ASSIGN                                                   !2, '912c1366-d5e0-c63a-e91d-7d6a495da509%7C147963'
   75     3        INIT_FCALL                                               'm24g'
          4        SEND_VAR                                                 !2
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $8      
          8        ASSIGN                                                   !3, $8
   76     9        INIT_FCALL                                               'base64_encode'
         10        SEND_VAR                                                 !3
         11        DO_ICALL                                         $10     
         12        CONCAT                                           ~11     'Encrypted+Data%3A+', $10
         13        CONCAT                                           ~12     ~11, '%0A'
         14        ECHO                                                     ~12
         15      > JMP                                                      ->22
   77    16  E > > CATCH                                       last         'Exception'
   78    17    >   INIT_METHOD_CALL                                         !4, 'getMessage'
         18        DO_FCALL                                      0  $13     
         19        CONCAT                                           ~14     'Error%3A+', $13
         20        CONCAT                                           ~15     ~14, '%0A'
         21        ECHO                                                     ~15
   80    22    > > RETURN                                                   1

Function m24g:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/X2v6d
function name:  m24g
number of ops:  26
compiled vars:  !0 = $bArr, !1 = $modulus, !2 = $exponent, !3 = $publicKey, !4 = $encryptedData
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    3     3        INIT_FCALL_BY_NAME                                       'm23h'
          4        SEND_VAR_EX                                              !1
          5        SEND_VAR_EX                                              !2
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !3, $5
    4     8        INIT_FCALL_BY_NAME                                       'openssl_public_encrypt'
          9        SEND_VAR_EX                                              !0
         10        SEND_VAR_EX                                              !4
         11        SEND_VAR_EX                                              !3
         12        FETCH_CONSTANT                                   ~7      'OPENSSL_PKCS1_PADDING'
         13        SEND_VAL_EX                                              ~7
         14        DO_FCALL                                      0  $8      
         15      > JMPZ                                                     $8, ->18
    5    16    > > RETURN                                                   !4
         17*       JMP                                                      ->25
    7    18    >   NEW                                              $9      'Exception'
         19        INIT_FCALL_BY_NAME                                       'openssl_error_string'
         20        DO_FCALL                                      0  $10     
         21        CONCAT                                           ~11     'Encryption+failed%3A+', $10
         22        SEND_VAL_EX                                              ~11
         23        DO_FCALL                                      0          
         24      > THROW                                         0          $9
    9    25*     > RETURN                                                   null

End of function m24g

Function m23h:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X2v6d
function name:  m23h
number of ops:  51
compiled vars:  !0 = $modulus, !1 = $exponent, !2 = $rsa, !3 = $keyDetails, !4 = $pem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        INIT_FCALL_BY_NAME                                       'gmp_import'
          3        INIT_FCALL                                               'hex2bin'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
          6        SEND_VAR_NO_REF_EX                                       $5
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !0, $6
   13     9        INIT_FCALL_BY_NAME                                       'gmp_import'
         10        INIT_FCALL                                               'hex2bin'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                         $8      
         13        SEND_VAR_NO_REF_EX                                       $8
         14        DO_FCALL                                      0  $9      
         15        ASSIGN                                                   !1, $9
   16    16        INIT_FCALL_BY_NAME                                       'gmp_strval'
         17        SEND_VAR_EX                                              !0
         18        SEND_VAL_EX                                              16
         19        DO_FCALL                                      0  $11     
         20        INIT_ARRAY                                       ~12     $11, 'n'
   17    21        INIT_FCALL_BY_NAME                                       'gmp_strval'
         22        SEND_VAR_EX                                              !1
         23        SEND_VAL_EX                                              16
         24        DO_FCALL                                      0  $13     
         25        ADD_ARRAY_ELEMENT                                ~12     $13, 'e'
   15    26        ASSIGN                                                   !2, ~12
   21    27        INIT_ARRAY                                       ~15     'RSA', 'kty'
   22    28        INIT_FCALL_BY_NAME                                       'base64url_encode'
         29        INIT_FCALL                                               'hex2bin'
         30        FETCH_DIM_R                                      ~16     !2, 'n'
         31        SEND_VAL                                                 ~16
         32        DO_ICALL                                         $17     
         33        SEND_VAR_NO_REF_EX                                       $17
         34        DO_FCALL                                      0  $18     
         35        ADD_ARRAY_ELEMENT                                ~15     $18, 'n'
   23    36        INIT_FCALL_BY_NAME                                       'base64url_encode'
         37        INIT_FCALL                                               'hex2bin'
         38        FETCH_DIM_R                                      ~19     !2, 'e'
         39        SEND_VAL                                                 ~19
         40        DO_ICALL                                         $20     
         41        SEND_VAR_NO_REF_EX                                       $20
         42        DO_FCALL                                      0  $21     
         43        ADD_ARRAY_ELEMENT                                ~15     $21, 'e'
   20    44        ASSIGN                                                   !3, ~15
   26    45        INIT_FCALL_BY_NAME                                       'jwkToPem'
         46        SEND_VAR_EX                                              !3
         47        DO_FCALL                                      0  $23     
         48        ASSIGN                                                   !4, $23
   28    49      > RETURN                                                   !4
   29    50*     > RETURN                                                   null

End of function m23h

Function base64url_encode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X2v6d
function name:  base64url_encode
number of ops:  15
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        INIT_FCALL                                               'rtrim'
          2        INIT_FCALL                                               'strtr'
          3        INIT_FCALL                                               'base64_encode'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $1      
          6        SEND_VAR                                                 $1
          7        SEND_VAL                                                 '%2B%2F'
          8        SEND_VAL                                                 '-_'
          9        DO_ICALL                                         $2      
         10        SEND_VAR                                                 $2
         11        SEND_VAL                                                 '%3D'
         12        DO_ICALL                                         $3      
         13      > RETURN                                                   $3
   33    14*     > RETURN                                                   null

End of function base64url_encode

Function jwktopem:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X2v6d
function name:  jwkToPem
number of ops:  30
compiled vars:  !0 = $jwk, !1 = $components, !2 = $pem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   37     1        INIT_FCALL_BY_NAME                                       'base64url_decode'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_DIM_FUNC_ARG                               $3      !0, 'n'
          4        SEND_FUNC_ARG                                            $3
          5        DO_FCALL                                      0  $4      
          6        INIT_ARRAY                                       ~5      $4, 'modulus'
   38     7        INIT_FCALL_BY_NAME                                       'base64url_decode'
          8        CHECK_FUNC_ARG                                           
          9        FETCH_DIM_FUNC_ARG                               $6      !0, 'e'
         10        SEND_FUNC_ARG                                            $6
         11        DO_FCALL                                      0  $7      
         12        ADD_ARRAY_ELEMENT                                ~5      $7, 'publicExponent'
   36    13        ASSIGN                                                   !1, ~5
   42    14        INIT_FCALL                                               'chunk_split'
         15        INIT_FCALL                                               'base64_encode'
         16        INIT_FCALL_BY_NAME                                       'createPublicKey'
         17        SEND_VAR_EX                                              !1
         18        DO_FCALL                                      0  $9      
         19        SEND_VAR                                                 $9
         20        DO_ICALL                                         $10     
         21        SEND_VAR                                                 $10
         22        SEND_VAL                                                 64
         23        SEND_VAL                                                 '%0A'
         24        DO_ICALL                                         $11     
         25        CONCAT                                           ~12     '-----BEGIN+PUBLIC+KEY-----%0A', $11
   43    26        CONCAT                                           ~13     ~12, '-----END+PUBLIC+KEY-----%0A'
   41    27        ASSIGN                                                   !2, ~13
   45    28      > RETURN                                                   !2
   46    29*     > RETURN                                                   null

End of function jwktopem

Function base64url_decode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X2v6d
function name:  base64url_decode
number of ops:  11
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1        INIT_FCALL                                               'base64_decode'
          2        INIT_FCALL                                               'strtr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 '-_'
          5        SEND_VAL                                                 '%2B%2F'
          6        DO_ICALL                                         $1      
          7        SEND_VAR                                                 $1
          8        DO_ICALL                                         $2      
          9      > RETURN                                                   $2
   50    10*     > RETURN                                                   null

End of function base64url_decode

Function createpublickey:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X2v6d
function name:  createPublicKey
number of ops:  41
compiled vars:  !0 = $components, !1 = $modulus, !2 = $publicExponent, !3 = $sequence, !4 = $bitString, !5 = $outerSequence
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        FETCH_DIM_R                                      ~6      !0, 'modulus'
          2        ASSIGN                                                   !1, ~6
   54     3        FETCH_DIM_R                                      ~8      !0, 'publicExponent'
          4        ASSIGN                                                   !2, ~8
   56     5        NEW                                              $10     'FG%5CASN1%5CUniversal%5CSequence'
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !3, $10
   57     8        INIT_METHOD_CALL                                         !3, 'addChild'
          9        NEW                                              $13     'FG%5CASN1%5CUniversal%5CInteger'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        SEND_VAR_NO_REF_EX                                       $13
         13        DO_FCALL                                      0          
   58    14        INIT_METHOD_CALL                                         !3, 'addChild'
         15        NEW                                              $16     'FG%5CASN1%5CUniversal%5CInteger'
         16        SEND_VAR_EX                                              !2
         17        DO_FCALL                                      0          
         18        SEND_VAR_NO_REF_EX                                       $16
         19        DO_FCALL                                      0          
   60    20        NEW                                              $19     'FG%5CASN1%5CUniversal%5CBitString'
         21        INIT_METHOD_CALL                                         !3, 'getBinary'
         22        DO_FCALL                                      0  $20     
         23        SEND_VAR_NO_REF_EX                                       $20
         24        DO_FCALL                                      0          
         25        ASSIGN                                                   !4, $19
   62    26        NEW                                              $23     'FG%5CASN1%5CUniversal%5CSequence'
         27        DO_FCALL                                      0          
         28        ASSIGN                                                   !5, $23
   63    29        INIT_METHOD_CALL                                         !5, 'addChild'
         30        NEW                                              $26     'FG%5CASN1%5CUniversal%5CSequence'
         31        DO_FCALL                                      0          
         32        SEND_VAR_NO_REF_EX                                       $26
         33        DO_FCALL                                      0          
   64    34        INIT_METHOD_CALL                                         !5, 'addChild'
         35        SEND_VAR_EX                                              !4
         36        DO_FCALL                                      0          
   66    37        INIT_METHOD_CALL                                         !5, 'getBinary'
         38        DO_FCALL                                      0  $30     
         39      > RETURN                                                   $30
   67    40*     > RETURN                                                   null

End of function createpublickey

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.46 ms | 1435 KiB | 26 Q