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 = 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"; } ?>
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/EuNF0
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
-------------------------------------------------------------------------------------
   84     0  E >   ASSIGN                                                   !0, '113744026899259137585420519641795445506522315843166624587737104823905995993308512746423780711220091509477248711684203860721990053300351123157272120027826616226336455748256059222713368430653450802331260182403534624073361236909650202772541076543992882238347687352553725072578344237693663853480535496066481835463'
   85     1        ASSIGN                                                   !1, '65537'
   86     2        ASSIGN                                                   !2, '912c1366-d5e0-c63a-e91d-7d6a495da509%7C147963'
   88     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
   89     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
   90    16  E > > CATCH                                       last         'Exception'
   91    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
   93    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/EuNF0
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/EuNF0
function name:  m23h
number of ops:  36
compiled vars:  !0 = $modulus, !1 = $exponent, !2 = $rsa, !3 = $keyDetails
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        INIT_FCALL_BY_NAME                                       'ensure_even_length'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !0, $4
   13     6        INIT_FCALL_BY_NAME                                       'ensure_even_length'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $6      
          9        ASSIGN                                                   !1, $6
   16    10        INIT_FCALL_BY_NAME                                       'base64url_encode'
         11        INIT_FCALL                                               'hex2bin'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                         $8      
         14        SEND_VAR_NO_REF_EX                                       $8
         15        DO_FCALL                                      0  $9      
         16        INIT_ARRAY                                       ~10     $9, 'n'
   17    17        INIT_FCALL_BY_NAME                                       'base64url_encode'
         18        INIT_FCALL                                               'hex2bin'
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $11     
         21        SEND_VAR_NO_REF_EX                                       $11
         22        DO_FCALL                                      0  $12     
         23        ADD_ARRAY_ELEMENT                                ~10     $12, 'e'
   15    24        ASSIGN                                                   !2, ~10
   21    25        INIT_ARRAY                                       ~14     'RSA', 'kty'
   22    26        FETCH_DIM_R                                      ~15     !2, 'n'
         27        ADD_ARRAY_ELEMENT                                ~14     ~15, 'n'
   23    28        FETCH_DIM_R                                      ~16     !2, 'e'
         29        ADD_ARRAY_ELEMENT                                ~14     ~16, 'e'
   20    30        ASSIGN                                                   !3, ~14
   26    31        INIT_FCALL_BY_NAME                                       'jwkToPem'
         32        SEND_VAR_EX                                              !3
         33        DO_FCALL                                      0  $18     
         34      > RETURN                                                   $18
   27    35*     > RETURN                                                   null

End of function m23h

Function ensure_even_length:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuNF0
function name:  ensure_even_length
number of ops:  11
compiled vars:  !0 = $hex
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        STRLEN                                           ~1      !0
          2        MOD                                              ~2      ~1, 2
          3        IS_NOT_EQUAL                                             ~2, 0
          4      > JMPZ                                                     ~3, ->8
          5    >   CONCAT                                           ~4      '0', !0
          6        QM_ASSIGN                                        ~5      ~4
          7      > JMP                                                      ->9
          8    >   QM_ASSIGN                                        ~5      !0
          9    > > RETURN                                                   ~5
   31    10*     > RETURN                                                   null

End of function ensure_even_length

Function base64url_encode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuNF0
function name:  base64url_encode
number of ops:  15
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   34     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
   35    14*     > RETURN                                                   null

End of function base64url_encode

Function base64url_decode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuNF0
function name:  base64url_decode
number of ops:  11
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     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
   39    10*     > RETURN                                                   null

End of function base64url_decode

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

End of function jwktopem

Function createpublickey:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuNF0
function name:  createPublicKey
number of ops:  72
compiled vars:  !0 = $components, !1 = $modulus, !2 = $publicExponent, !3 = $sequence
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   60     1        FETCH_DIM_R                                      ~4      !0, 'modulus'
          2        ASSIGN                                                   !1, ~4
   61     3        FETCH_DIM_R                                      ~6      !0, 'publicExponent'
          4        ASSIGN                                                   !2, ~6
   63     5        CONCAT                                           ~8      '%00', !1
          6        ASSIGN                                                   !1, ~8
   65     7        INIT_FCALL                                               'pack'
          8        SEND_VAL                                                 'Ca%2Aa%2A'
          9        SEND_VAL                                                 48
         10        INIT_FCALL_BY_NAME                                       'createLengthPrefix'
         11        STRLEN                                           ~10     !1
         12        STRLEN                                           ~11     !2
         13        ADD                                              ~12     ~10, ~11
         14        ADD                                              ~13     ~12, 4
         15        SEND_VAL_EX                                              ~13
         16        DO_FCALL                                      0  $14     
         17        SEND_VAR                                                 $14
   66    18        INIT_FCALL                                               'pack'
         19        SEND_VAL                                                 'Ca%2A'
         20        SEND_VAL                                                 2
         21        INIT_FCALL_BY_NAME                                       'createLengthPrefix'
         22        STRLEN                                           ~15     !1
         23        SEND_VAL_EX                                              ~15
         24        DO_FCALL                                      0  $16     
         25        CONCAT                                           ~17     $16, !1
         26        SEND_VAL                                                 ~17
         27        DO_ICALL                                         $18     
   67    28        INIT_FCALL                                               'pack'
         29        SEND_VAL                                                 'Ca%2A'
         30        SEND_VAL                                                 2
         31        INIT_FCALL_BY_NAME                                       'createLengthPrefix'
         32        STRLEN                                           ~19     !2
         33        SEND_VAL_EX                                              ~19
         34        DO_FCALL                                      0  $20     
         35        CONCAT                                           ~21     $20, !2
         36        SEND_VAL                                                 ~21
         37        DO_ICALL                                         $22     
         38        CONCAT                                           ~23     $18, $22
         39        SEND_VAL                                                 ~23
         40        DO_ICALL                                         $24     
   65    41        ASSIGN                                                   !3, $24
   69    42        INIT_FCALL                                               'pack'
         43        SEND_VAL                                                 'Ca%2Aa%2A'
         44        SEND_VAL                                                 48
         45        INIT_FCALL_BY_NAME                                       'createLengthPrefix'
         46        STRLEN                                           ~26     !3
         47        ADD                                              ~27     ~26, 13
         48        SEND_VAL_EX                                              ~27
         49        DO_FCALL                                      0  $28     
         50        SEND_VAR                                                 $28
   70    51        INIT_FCALL                                               'pack'
         52        SEND_VAL                                                 'H%2A'
         53        SEND_VAL                                                 '300D06092A864886F70D0101010500'
         54        DO_ICALL                                         $29     
   71    55        INIT_FCALL                                               'pack'
         56        SEND_VAL                                                 'Ca%2A'
         57        SEND_VAL                                                 3
         58        INIT_FCALL_BY_NAME                                       'createLengthPrefix'
         59        STRLEN                                           ~30     !3
         60        ADD                                              ~31     ~30, 1
         61        SEND_VAL_EX                                              ~31
         62        DO_FCALL                                      0  $32     
         63        CONCAT                                           ~33     $32, '%00'
         64        CONCAT                                           ~34     ~33, !3
         65        SEND_VAL                                                 ~34
         66        DO_ICALL                                         $35     
         67        CONCAT                                           ~36     $29, $35
         68        SEND_VAL                                                 ~36
         69        DO_ICALL                                         $37     
         70      > RETURN                                                   $37
   72    71*     > RETURN                                                   null

End of function createpublickey

Function createlengthprefix:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuNF0
function name:  createLengthPrefix
number of ops:  24
compiled vars:  !0 = $length, !1 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
   75     1        IS_SMALLER                                               !0, 128
          2      > JMPZ                                                     ~2, ->7
   76     3    >   INIT_FCALL                                               'chr'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $3      
          6      > RETURN                                                   $3
   78     7    >   INIT_FCALL                                               'ltrim'
          8        INIT_FCALL                                               'pack'
          9        SEND_VAL                                                 'N'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $4      
         12        SEND_VAR                                                 $4
         13        SEND_VAL                                                 '%00'
         14        DO_ICALL                                         $5      
         15        ASSIGN                                                   !1, $5
   79    16        INIT_FCALL                                               'chr'
         17        STRLEN                                           ~7      !1
         18        BW_OR                                            ~8      ~7, 128
         19        SEND_VAL                                                 ~8
         20        DO_ICALL                                         $9      
         21        CONCAT                                           ~10     $9, !1
         22      > RETURN                                                   ~10
   80    23*     > RETURN                                                   null

End of function createlengthprefix

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152.03 ms | 1439 KiB | 34 Q