3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Helper library for CryptoJS AES encryption/decryption * Allow you to use AES encryption on client side and server side vice versa * * @author BrainFooLong (bfldev.com) * @link https://github.com/brainfoolong/cryptojs-aes-php */ /** * Decrypt data from a CryptoJS json encoding string * * @param mixed $passphrase * @param mixed $jsonString * @return mixed */ function cryptoJsAesDecrypt($passphrase, $jsonString){ $jsondata = json_decode($jsonString, true); $salt = hex2bin($jsondata["s"]); $ct = base64_decode($jsondata["ct"]); $iv = hex2bin($jsondata["iv"]); $concatedPassphrase = $passphrase.$salt; $md5 = array(); $md5[0] = md5($concatedPassphrase, true); $result = $md5[0]; for ($i = 1; $i < 3; $i++) { $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true); $result .= $md5[$i]; } $key = substr($result, 0, 32); $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv); return json_decode($data, true); } /** * Encrypt value to a cryptojs compatiable json encoding string * * @param mixed $passphrase * @param mixed $value * @return string */ function cryptoJsAesEncrypt($passphrase, $value){ $salt = openssl_random_pseudo_bytes(8); $salted = ''; $dx = ''; while (strlen($salted) < 48) { $dx = md5($dx.$passphrase.$salt, true); $salted .= $dx; } $key = substr($salted, 0, 32); $iv = substr($salted, 32,16); $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv); $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt)); return json_encode($data); } echo cryptoJsAeaEncrypt("test", "foo");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7jehl
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   INIT_FCALL_BY_NAME                                       'cryptoJsAeaEncrypt'
          1        SEND_VAL_EX                                              'test'
          2        SEND_VAL_EX                                              'foo'
          3        DO_FCALL                                      0  $0      
          4        ECHO                                                     $0
          5      > RETURN                                                   1

Function cryptojsaesdecrypt:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 35
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 35
Branch analysis from position: 49
Branch analysis from position: 35
filename:       /in/7jehl
function name:  cryptoJsAesDecrypt
number of ops:  69
compiled vars:  !0 = $passphrase, !1 = $jsonString, !2 = $jsondata, !3 = $salt, !4 = $ct, !5 = $iv, !6 = $concatedPassphrase, !7 = $md5, !8 = $result, !9 = $i, !10 = $key, !11 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        INIT_FCALL                                               'json_decode'
          3        SEND_VAR                                                 !1
          4        SEND_VAL                                                 <true>
          5        DO_ICALL                                         $12     
          6        ASSIGN                                                   !2, $12
   19     7        INIT_FCALL                                               'hex2bin'
          8        FETCH_DIM_R                                      ~14     !2, 's'
          9        SEND_VAL                                                 ~14
         10        DO_ICALL                                         $15     
         11        ASSIGN                                                   !3, $15
   20    12        INIT_FCALL                                               'base64_decode'
         13        FETCH_DIM_R                                      ~17     !2, 'ct'
         14        SEND_VAL                                                 ~17
         15        DO_ICALL                                         $18     
         16        ASSIGN                                                   !4, $18
   21    17        INIT_FCALL                                               'hex2bin'
         18        FETCH_DIM_R                                      ~20     !2, 'iv'
         19        SEND_VAL                                                 ~20
         20        DO_ICALL                                         $21     
         21        ASSIGN                                                   !5, $21
   22    22        CONCAT                                           ~23     !0, !3
         23        ASSIGN                                                   !6, ~23
   23    24        ASSIGN                                                   !7, <array>
   24    25        INIT_FCALL                                               'md5'
         26        SEND_VAR                                                 !6
         27        SEND_VAL                                                 <true>
         28        DO_ICALL                                         $27     
         29        ASSIGN_DIM                                               !7, 0
         30        OP_DATA                                                  $27
   25    31        FETCH_DIM_R                                      ~28     !7, 0
         32        ASSIGN                                                   !8, ~28
   26    33        ASSIGN                                                   !9, 1
         34      > JMP                                                      ->47
   27    35    >   INIT_FCALL                                               'md5'
         36        SUB                                              ~32     !9, 1
         37        FETCH_DIM_R                                      ~33     !7, ~32
         38        CONCAT                                           ~34     ~33, !6
         39        SEND_VAL                                                 ~34
         40        SEND_VAL                                                 <true>
         41        DO_ICALL                                         $35     
         42        ASSIGN_DIM                                               !7, !9
         43        OP_DATA                                                  $35
   28    44        FETCH_DIM_R                                      ~36     !7, !9
         45        ASSIGN_OP                                     8          !8, ~36
   26    46        PRE_INC                                                  !9
         47    >   IS_SMALLER                                               !9, 3
         48      > JMPNZ                                                    ~39, ->35
   30    49    >   INIT_FCALL                                               'substr'
         50        SEND_VAR                                                 !8
         51        SEND_VAL                                                 0
         52        SEND_VAL                                                 32
         53        DO_ICALL                                         $40     
         54        ASSIGN                                                   !10, $40
   31    55        INIT_FCALL_BY_NAME                                       'openssl_decrypt'
         56        SEND_VAR_EX                                              !4
         57        SEND_VAL_EX                                              'aes-256-cbc'
         58        SEND_VAR_EX                                              !10
         59        SEND_VAL_EX                                              <true>
         60        SEND_VAR_EX                                              !5
         61        DO_FCALL                                      0  $42     
         62        ASSIGN                                                   !11, $42
   32    63        INIT_FCALL                                               'json_decode'
         64        SEND_VAR                                                 !11
         65        SEND_VAL                                                 <true>
         66        DO_ICALL                                         $44     
         67      > RETURN                                                   $44
   33    68*     > RETURN                                                   null

End of function cryptojsaesdecrypt

Function cryptojsaesencrypt:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 9
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 9
Branch analysis from position: 20
Branch analysis from position: 9
filename:       /in/7jehl
function name:  cryptoJsAesEncrypt
number of ops:  61
compiled vars:  !0 = $passphrase, !1 = $value, !2 = $salt, !3 = $salted, !4 = $dx, !5 = $key, !6 = $iv, !7 = $encrypted_data, !8 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   43     2        INIT_FCALL_BY_NAME                                       'openssl_random_pseudo_bytes'
          3        SEND_VAL_EX                                              8
          4        DO_FCALL                                      0  $9      
          5        ASSIGN                                                   !2, $9
   44     6        ASSIGN                                                   !3, ''
   45     7        ASSIGN                                                   !4, ''
   46     8      > JMP                                                      ->17
   47     9    >   INIT_FCALL                                               'md5'
         10        CONCAT                                           ~13     !4, !0
         11        CONCAT                                           ~14     ~13, !2
         12        SEND_VAL                                                 ~14
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $15     
         15        ASSIGN                                                   !4, $15
   48    16        ASSIGN_OP                                     8          !3, !4
   46    17    >   STRLEN                                           ~18     !3
         18        IS_SMALLER                                               ~18, 48
         19      > JMPNZ                                                    ~19, ->9
   50    20    >   INIT_FCALL                                               'substr'
         21        SEND_VAR                                                 !3
         22        SEND_VAL                                                 0
         23        SEND_VAL                                                 32
         24        DO_ICALL                                         $20     
         25        ASSIGN                                                   !5, $20
   51    26        INIT_FCALL                                               'substr'
         27        SEND_VAR                                                 !3
         28        SEND_VAL                                                 32
         29        SEND_VAL                                                 16
         30        DO_ICALL                                         $22     
         31        ASSIGN                                                   !6, $22
   52    32        INIT_FCALL_BY_NAME                                       'openssl_encrypt'
         33        INIT_FCALL                                               'json_encode'
         34        SEND_VAR                                                 !1
         35        DO_ICALL                                         $24     
         36        SEND_VAR_NO_REF_EX                                       $24
         37        SEND_VAL_EX                                              'aes-256-cbc'
         38        SEND_VAR_EX                                              !5
         39        SEND_VAL_EX                                              <true>
         40        SEND_VAR_EX                                              !6
         41        DO_FCALL                                      0  $25     
         42        ASSIGN                                                   !7, $25
   53    43        INIT_FCALL                                               'base64_encode'
         44        SEND_VAR                                                 !7
         45        DO_ICALL                                         $27     
         46        INIT_ARRAY                                       ~28     $27, 'ct'
         47        INIT_FCALL                                               'bin2hex'
         48        SEND_VAR                                                 !6
         49        DO_ICALL                                         $29     
         50        ADD_ARRAY_ELEMENT                                ~28     $29, 'iv'
         51        INIT_FCALL                                               'bin2hex'
         52        SEND_VAR                                                 !2
         53        DO_ICALL                                         $30     
         54        ADD_ARRAY_ELEMENT                                ~28     $30, 's'
         55        ASSIGN                                                   !8, ~28
   54    56        INIT_FCALL                                               'json_encode'
         57        SEND_VAR                                                 !8
         58        DO_ICALL                                         $32     
         59      > RETURN                                                   $32
   55    60*     > RETURN                                                   null

End of function cryptojsaesencrypt

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.16 ms | 1480 KiB | 21 Q