3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (PHP_VERSION_ID < 70000) exit; $res = openssl_pkey_new([ 'digest_alg' => 'sha256', 'private_key_bite' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA ]); openssl_pkey_export($res, $privateKey); $publicKey = openssl_pkey_get_details($res)['key']; $message = 'Prime Numbers Rock!'; $aesKey = random_bytes(32); // Basically a poor-man's HKDF by just using HMAC $keyE = hash_hmac('sha256', 'Encryption Key', $aesKey, true); $keyA = hash_hmac('sha256', 'Authentication Key', $aesKey, true); $iv = random_bytes(16); $ciphertext = openssl_encrypt($message, 'aes-256-ctr', $keyE, OPENSSL_RAW_DATA, $iv); $mac = hash_hmac('sha256', $iv .$ciphertext, $keyA, true); $combined = $mac . $iv . $ciphertext; $rsaCipher = ''; openssl_public_encrypt($aesKey, $rsaCipher, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); $sendMe = $rsaCipher . $combined; var_dump(base64_encode($sendMe)); ## DECRYPTION ## $rsaPart = mb_substr($sendMe, 0, 256, '8bit'); // Assuming 2048-bit RSA $aesPart = mb_substr($sendMe, 256, null, '8bit'); $mac = mb_substr($aesPart, 0, 32, '8bit'); $iv = mb_substr($aesPart, 32, 16, '8bit'); $cipher = mb_substr($aesPart, 48, null, '8bit'); $aesKey = ''; openssl_private_decrypt($rsaPart, $aesKey, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); $keyE = hash_hmac('sha256', 'Encryption Key', $aesKey, true); $keyA = hash_hmac('sha256', 'Authentication Key', $aesKey, true); $calc = hash_hmac('sha256', $iv . $cipher, $keyA, true); if (!hash_equals($calc, $mac)) { throw new Exception('MAC validation failure'); } $decrypted = openssl_decrypt($cipher, 'aes-256-ctr', $keyE, OPENSSL_RAW_DATA, $iv); var_dump($decrypted); // string(19) "Prime Numbers Rock!"
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 1, Position 2 = 2
Branch analysis from position: 1
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 43) Position 1 = 149, Position 2 = 153
Branch analysis from position: 149
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 153
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nYVPf
function name:  (null)
number of ops:  166
compiled vars:  !0 = $res, !1 = $privateKey, !2 = $publicKey, !3 = $message, !4 = $aesKey, !5 = $keyE, !6 = $keyA, !7 = $iv, !8 = $ciphertext, !9 = $mac, !10 = $combined, !11 = $rsaCipher, !12 = $sendMe, !13 = $rsaPart, !14 = $aesPart, !15 = $cipher, !16 = $calc, !17 = $decrypted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E > > JMPZ                                                     <false>, ->2
          1    > > EXIT                                                     
    3     2    >   INIT_FCALL_BY_NAME                                       'openssl_pkey_new'
    4     3        INIT_ARRAY                                       ~18     'sha256', 'digest_alg'
    5     4        ADD_ARRAY_ELEMENT                                ~18     2048, 'private_key_bite'
    6     5        FETCH_CONSTANT                                   ~19     'OPENSSL_KEYTYPE_RSA'
          6        ADD_ARRAY_ELEMENT                                ~18     ~19, 'private_key_type'
          7        SEND_VAL_EX                                              ~18
    3     8        DO_FCALL                                      0  $20     
          9        ASSIGN                                                   !0, $20
    8    10        INIT_FCALL_BY_NAME                                       'openssl_pkey_export'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
    9    14        INIT_FCALL_BY_NAME                                       'openssl_pkey_get_details'
         15        SEND_VAR_EX                                              !0
         16        DO_FCALL                                      0  $23     
         17        FETCH_DIM_R                                      ~24     $23, 'key'
         18        ASSIGN                                                   !2, ~24
   11    19        ASSIGN                                                   !3, 'Prime+Numbers+Rock%21'
   12    20        INIT_FCALL                                               'random_bytes'
         21        SEND_VAL                                                 32
         22        DO_ICALL                                         $27     
         23        ASSIGN                                                   !4, $27
   14    24        INIT_FCALL                                               'hash_hmac'
         25        SEND_VAL                                                 'sha256'
         26        SEND_VAL                                                 'Encryption+Key'
         27        SEND_VAR                                                 !4
         28        SEND_VAL                                                 <true>
         29        DO_ICALL                                         $29     
         30        ASSIGN                                                   !5, $29
   15    31        INIT_FCALL                                               'hash_hmac'
         32        SEND_VAL                                                 'sha256'
         33        SEND_VAL                                                 'Authentication+Key'
         34        SEND_VAR                                                 !4
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $31     
         37        ASSIGN                                                   !6, $31
   17    38        INIT_FCALL                                               'random_bytes'
         39        SEND_VAL                                                 16
         40        DO_ICALL                                         $33     
         41        ASSIGN                                                   !7, $33
   18    42        INIT_FCALL_BY_NAME                                       'openssl_encrypt'
         43        SEND_VAR_EX                                              !3
         44        SEND_VAL_EX                                              'aes-256-ctr'
         45        SEND_VAR_EX                                              !5
         46        FETCH_CONSTANT                                   ~35     'OPENSSL_RAW_DATA'
         47        SEND_VAL_EX                                              ~35
         48        SEND_VAR_EX                                              !7
         49        DO_FCALL                                      0  $36     
         50        ASSIGN                                                   !8, $36
   19    51        INIT_FCALL                                               'hash_hmac'
         52        SEND_VAL                                                 'sha256'
         53        CONCAT                                           ~38     !7, !8
         54        SEND_VAL                                                 ~38
         55        SEND_VAR                                                 !6
         56        SEND_VAL                                                 <true>
         57        DO_ICALL                                         $39     
         58        ASSIGN                                                   !9, $39
   21    59        CONCAT                                           ~41     !9, !7
         60        CONCAT                                           ~42     ~41, !8
         61        ASSIGN                                                   !10, ~42
   22    62        ASSIGN                                                   !11, ''
   23    63        INIT_FCALL_BY_NAME                                       'openssl_public_encrypt'
         64        SEND_VAR_EX                                              !4
         65        SEND_VAR_EX                                              !11
         66        SEND_VAR_EX                                              !2
         67        FETCH_CONSTANT                                   ~45     'OPENSSL_PKCS1_OAEP_PADDING'
         68        SEND_VAL_EX                                              ~45
         69        DO_FCALL                                      0          
   24    70        CONCAT                                           ~47     !11, !10
         71        ASSIGN                                                   !12, ~47
   26    72        INIT_FCALL                                               'var_dump'
         73        INIT_FCALL                                               'base64_encode'
         74        SEND_VAR                                                 !12
         75        DO_ICALL                                         $49     
         76        SEND_VAR                                                 $49
         77        DO_ICALL                                                 
   30    78        INIT_FCALL                                               'mb_substr'
         79        SEND_VAR                                                 !12
         80        SEND_VAL                                                 0
         81        SEND_VAL                                                 256
         82        SEND_VAL                                                 '8bit'
         83        DO_ICALL                                         $51     
         84        ASSIGN                                                   !13, $51
   31    85        INIT_FCALL                                               'mb_substr'
         86        SEND_VAR                                                 !12
         87        SEND_VAL                                                 256
         88        SEND_VAL                                                 null
         89        SEND_VAL                                                 '8bit'
         90        DO_ICALL                                         $53     
         91        ASSIGN                                                   !14, $53
   32    92        INIT_FCALL                                               'mb_substr'
         93        SEND_VAR                                                 !14
         94        SEND_VAL                                                 0
         95        SEND_VAL                                                 32
         96        SEND_VAL                                                 '8bit'
         97        DO_ICALL                                         $55     
         98        ASSIGN                                                   !9, $55
   33    99        INIT_FCALL                                               'mb_substr'
        100        SEND_VAR                                                 !14
        101        SEND_VAL                                                 32
        102        SEND_VAL                                                 16
        103        SEND_VAL                                                 '8bit'
        104        DO_ICALL                                         $57     
        105        ASSIGN                                                   !7, $57
   34   106        INIT_FCALL                                               'mb_substr'
        107        SEND_VAR                                                 !14
        108        SEND_VAL                                                 48
        109        SEND_VAL                                                 null
        110        SEND_VAL                                                 '8bit'
        111        DO_ICALL                                         $59     
        112        ASSIGN                                                   !15, $59
   36   113        ASSIGN                                                   !4, ''
   37   114        INIT_FCALL_BY_NAME                                       'openssl_private_decrypt'
        115        SEND_VAR_EX                                              !13
        116        SEND_VAR_EX                                              !4
        117        SEND_VAR_EX                                              !1
        118        FETCH_CONSTANT                                   ~62     'OPENSSL_PKCS1_OAEP_PADDING'
        119        SEND_VAL_EX                                              ~62
        120        DO_FCALL                                      0          
   38   121        INIT_FCALL                                               'hash_hmac'
        122        SEND_VAL                                                 'sha256'
        123        SEND_VAL                                                 'Encryption+Key'
        124        SEND_VAR                                                 !4
        125        SEND_VAL                                                 <true>
        126        DO_ICALL                                         $64     
        127        ASSIGN                                                   !5, $64
   39   128        INIT_FCALL                                               'hash_hmac'
        129        SEND_VAL                                                 'sha256'
        130        SEND_VAL                                                 'Authentication+Key'
        131        SEND_VAR                                                 !4
        132        SEND_VAL                                                 <true>
        133        DO_ICALL                                         $66     
        134        ASSIGN                                                   !6, $66
   41   135        INIT_FCALL                                               'hash_hmac'
        136        SEND_VAL                                                 'sha256'
        137        CONCAT                                           ~68     !7, !15
        138        SEND_VAL                                                 ~68
        139        SEND_VAR                                                 !6
        140        SEND_VAL                                                 <true>
        141        DO_ICALL                                         $69     
        142        ASSIGN                                                   !16, $69
   42   143        INIT_FCALL                                               'hash_equals'
        144        SEND_VAR                                                 !16
        145        SEND_VAR                                                 !9
        146        DO_ICALL                                         $71     
        147        BOOL_NOT                                         ~72     $71
        148      > JMPZ                                                     ~72, ->153
   43   149    >   NEW                                              $73     'Exception'
        150        SEND_VAL_EX                                              'MAC+validation+failure'
        151        DO_FCALL                                      0          
        152      > THROW                                         0          $73
   46   153    >   INIT_FCALL_BY_NAME                                       'openssl_decrypt'
        154        SEND_VAR_EX                                              !15
        155        SEND_VAL_EX                                              'aes-256-ctr'
        156        SEND_VAR_EX                                              !5
        157        FETCH_CONSTANT                                   ~75     'OPENSSL_RAW_DATA'
        158        SEND_VAL_EX                                              ~75
        159        SEND_VAR_EX                                              !7
        160        DO_FCALL                                      0  $76     
        161        ASSIGN                                                   !17, $76
   47   162        INIT_FCALL                                               'var_dump'
        163        SEND_VAR                                                 !17
        164        DO_ICALL                                                 
        165      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.52 ms | 1016 KiB | 19 Q