3v4l.org

run code in 500+ 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 = 3
Branch analysis from position: 1
1 jumps found. (Code = 61) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 150, Position 2 = 154
Branch analysis from position: 150
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 154
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nYVPf
function name:  (null)
number of ops:  167
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>, ->3
          1    > > INIT_FCALL                                                   'exit'
          2*       DO_ICALL                                                     
    3     3    >   INIT_FCALL_BY_NAME                                           'openssl_pkey_new'
    4     4        INIT_ARRAY                                           ~19     'sha256', 'digest_alg'
    5     5        ADD_ARRAY_ELEMENT                                    ~19     2048, 'private_key_bite'
    6     6        FETCH_CONSTANT                                       ~20     'OPENSSL_KEYTYPE_RSA'
          7        ADD_ARRAY_ELEMENT                                    ~19     ~20, 'private_key_type'
          8        SEND_VAL_EX                                                  ~19
    3     9        DO_FCALL                                          0  $21     
         10        ASSIGN                                                       !0, $21
    8    11        INIT_FCALL_BY_NAME                                           'openssl_pkey_export'
         12        SEND_VAR_EX                                                  !0
         13        SEND_VAR_EX                                                  !1
         14        DO_FCALL                                          0          
    9    15        INIT_FCALL_BY_NAME                                           'openssl_pkey_get_details'
         16        SEND_VAR_EX                                                  !0
         17        DO_FCALL                                          0  $24     
         18        FETCH_DIM_R                                          ~25     $24, 'key'
         19        ASSIGN                                                       !2, ~25
   11    20        ASSIGN                                                       !3, 'Prime+Numbers+Rock%21'
   12    21        INIT_FCALL                                                   'random_bytes'
         22        SEND_VAL                                                     32
         23        DO_ICALL                                             $28     
         24        ASSIGN                                                       !4, $28
   14    25        INIT_FCALL                                                   'hash_hmac'
         26        SEND_VAL                                                     'sha256'
         27        SEND_VAL                                                     'Encryption+Key'
         28        SEND_VAR                                                     !4
         29        SEND_VAL                                                     <true>
         30        DO_ICALL                                             $30     
         31        ASSIGN                                                       !5, $30
   15    32        INIT_FCALL                                                   'hash_hmac'
         33        SEND_VAL                                                     'sha256'
         34        SEND_VAL                                                     'Authentication+Key'
         35        SEND_VAR                                                     !4
         36        SEND_VAL                                                     <true>
         37        DO_ICALL                                             $32     
         38        ASSIGN                                                       !6, $32
   17    39        INIT_FCALL                                                   'random_bytes'
         40        SEND_VAL                                                     16
         41        DO_ICALL                                             $34     
         42        ASSIGN                                                       !7, $34
   18    43        INIT_FCALL_BY_NAME                                           'openssl_encrypt'
         44        SEND_VAR_EX                                                  !3
         45        SEND_VAL_EX                                                  'aes-256-ctr'
         46        SEND_VAR_EX                                                  !5
         47        FETCH_CONSTANT                                       ~36     'OPENSSL_RAW_DATA'
         48        SEND_VAL_EX                                                  ~36
         49        SEND_VAR_EX                                                  !7
         50        DO_FCALL                                          0  $37     
         51        ASSIGN                                                       !8, $37
   19    52        INIT_FCALL                                                   'hash_hmac'
         53        SEND_VAL                                                     'sha256'
         54        CONCAT                                               ~39     !7, !8
         55        SEND_VAL                                                     ~39
         56        SEND_VAR                                                     !6
         57        SEND_VAL                                                     <true>
         58        DO_ICALL                                             $40     
         59        ASSIGN                                                       !9, $40
   21    60        CONCAT                                               ~42     !9, !7
         61        CONCAT                                               ~43     ~42, !8
         62        ASSIGN                                                       !10, ~43
   22    63        ASSIGN                                                       !11, ''
   23    64        INIT_FCALL_BY_NAME                                           'openssl_public_encrypt'
         65        SEND_VAR_EX                                                  !4
         66        SEND_VAR_EX                                                  !11
         67        SEND_VAR_EX                                                  !2
         68        FETCH_CONSTANT                                       ~46     'OPENSSL_PKCS1_OAEP_PADDING'
         69        SEND_VAL_EX                                                  ~46
         70        DO_FCALL                                          0          
   24    71        CONCAT                                               ~48     !11, !10
         72        ASSIGN                                                       !12, ~48
   26    73        INIT_FCALL                                                   'var_dump'
         74        INIT_FCALL                                                   'base64_encode'
         75        SEND_VAR                                                     !12
         76        DO_ICALL                                             $50     
         77        SEND_VAR                                                     $50
         78        DO_ICALL                                                     
   30    79        INIT_FCALL                                                   'mb_substr'
         80        SEND_VAR                                                     !12
         81        SEND_VAL                                                     0
         82        SEND_VAL                                                     256
         83        SEND_VAL                                                     '8bit'
         84        DO_ICALL                                             $52     
         85        ASSIGN                                                       !13, $52
   31    86        INIT_FCALL                                                   'mb_substr'
         87        SEND_VAR                                                     !12
         88        SEND_VAL                                                     256
         89        SEND_VAL                                                     null
         90        SEND_VAL                                                     '8bit'
         91        DO_ICALL                                             $54     
         92        ASSIGN                                                       !14, $54
   32    93        INIT_FCALL                                                   'mb_substr'
         94        SEND_VAR                                                     !14
         95        SEND_VAL                                                     0
         96        SEND_VAL                                                     32
         97        SEND_VAL                                                     '8bit'
         98        DO_ICALL                                             $56     
         99        ASSIGN                                                       !9, $56
   33   100        INIT_FCALL                                                   'mb_substr'
        101        SEND_VAR                                                     !14
        102        SEND_VAL                                                     32
        103        SEND_VAL                                                     16
        104        SEND_VAL                                                     '8bit'
        105        DO_ICALL                                             $58     
        106        ASSIGN                                                       !7, $58
   34   107        INIT_FCALL                                                   'mb_substr'
        108        SEND_VAR                                                     !14
        109        SEND_VAL                                                     48
        110        SEND_VAL                                                     null
        111        SEND_VAL                                                     '8bit'
        112        DO_ICALL                                             $60     
        113        ASSIGN                                                       !15, $60
   36   114        ASSIGN                                                       !4, ''
   37   115        INIT_FCALL_BY_NAME                                           'openssl_private_decrypt'
        116        SEND_VAR_EX                                                  !13
        117        SEND_VAR_EX                                                  !4
        118        SEND_VAR_EX                                                  !1
        119        FETCH_CONSTANT                                       ~63     'OPENSSL_PKCS1_OAEP_PADDING'
        120        SEND_VAL_EX                                                  ~63
        121        DO_FCALL                                          0          
   38   122        INIT_FCALL                                                   'hash_hmac'
        123        SEND_VAL                                                     'sha256'
        124        SEND_VAL                                                     'Encryption+Key'
        125        SEND_VAR                                                     !4
        126        SEND_VAL                                                     <true>
        127        DO_ICALL                                             $65     
        128        ASSIGN                                                       !5, $65
   39   129        INIT_FCALL                                                   'hash_hmac'
        130        SEND_VAL                                                     'sha256'
        131        SEND_VAL                                                     'Authentication+Key'
        132        SEND_VAR                                                     !4
        133        SEND_VAL                                                     <true>
        134        DO_ICALL                                             $67     
        135        ASSIGN                                                       !6, $67
   41   136        INIT_FCALL                                                   'hash_hmac'
        137        SEND_VAL                                                     'sha256'
        138        CONCAT                                               ~69     !7, !15
        139        SEND_VAL                                                     ~69
        140        SEND_VAR                                                     !6
        141        SEND_VAL                                                     <true>
        142        DO_ICALL                                             $70     
        143        ASSIGN                                                       !16, $70
   42   144        INIT_FCALL                                                   'hash_equals'
        145        SEND_VAR                                                     !16
        146        SEND_VAR                                                     !9
        147        DO_ICALL                                             $72     
        148        BOOL_NOT                                             ~73     $72
        149      > JMPZ                                                         ~73, ->154
   43   150    >   NEW                                                  $74     'Exception'
        151        SEND_VAL_EX                                                  'MAC+validation+failure'
        152        DO_FCALL                                          0          
        153      > THROW                                             0          $74
   46   154    >   INIT_FCALL_BY_NAME                                           'openssl_decrypt'
        155        SEND_VAR_EX                                                  !15
        156        SEND_VAL_EX                                                  'aes-256-ctr'
        157        SEND_VAR_EX                                                  !5
        158        FETCH_CONSTANT                                       ~76     'OPENSSL_RAW_DATA'
        159        SEND_VAL_EX                                                  ~76
        160        SEND_VAR_EX                                                  !7
        161        DO_FCALL                                          0  $77     
        162        ASSIGN                                                       !17, $77
   47   163        INIT_FCALL                                                   'var_dump'
        164        SEND_VAR                                                     !17
        165        DO_ICALL                                                     
        166      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
172.73 ms | 1411 KiB | 21 Q