3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Define a 32-byte (64 character) hexadecimal encryption key // Note: The same encryption key used to encrypt the data must be used to decrypt the data define('ENCRYPTION_KEY', 'd0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282'); // Encrypt Function function mc_encrypt($encrypt, $key){ $encrypt = serialize($encrypt); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM); $key = pack('H*', $key); $mac = hash_hmac('sha256', $encrypt, substr(bin2hex($key), -32)); $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $encrypt.$mac, MCRYPT_MODE_CBC, $iv); $encoded = base64_encode($passcrypt).'|'.base64_encode($iv); return $encoded; } // Decrypt Function function mc_decrypt($decrypt, $key){ $decrypt = explode('|', $decrypt.'|'); $decoded = base64_decode($decrypt[0]); $iv = base64_decode($decrypt[1]); if(strlen($iv)!==mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC)){ return false; } $key = pack('H*', $key); $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv)); $mac = substr($decrypted, -64); $decrypted = substr($decrypted, 0, -64); $calcmac = hash_hmac('sha256', $decrypted, substr(bin2hex($key), -32)); if($calcmac!==$mac){ return false; } $decrypted = unserialize($decrypted); return $decrypted; } echo '<h1>Rijndael 256-bit CBC Encryption Function</h1>'; $data = 'Super secret confidential string data.'; $encrypted_data = mc_encrypt($data, ENCRYPTION_KEY); echo '<h2>Example #1: String Data</h2>'; echo 'Data to be Encrypted: ' . $data . '<br/>'; echo 'Encrypted Data: ' . $encrypted_data . '<br/>'; echo 'Decrypted Data: ' . mc_decrypt($encrypted_data, ENCRYPTION_KEY) . '</br>'; $data = array(1, 5, 8, 9, 22, 10, 61); $encrypted_data = mc_encrypt($data, ENCRYPTION_KEY); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3i5Vi
function name:  (null)
number of ops:  35
compiled vars:  !0 = $data, !1 = $encrypted_data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'ENCRYPTION_KEY'
          2        SEND_VAL                                                 'd0a7e7997b6d5fcd55f4b5c32611b87cd923e88837b63bf2941ef819dc8ca282'
          3        DO_ICALL                                                 
   30     4        ECHO                                                     '%3Ch1%3ERijndael+256-bit+CBC+Encryption+Function%3C%2Fh1%3E'
   31     5        ASSIGN                                                   !0, 'Super+secret+confidential+string+data.'
   32     6        INIT_FCALL                                               'mc_encrypt'
          7        SEND_VAR                                                 !0
          8        FETCH_CONSTANT                                   ~4      'ENCRYPTION_KEY'
          9        SEND_VAL                                                 ~4
         10        DO_FCALL                                      0  $5      
         11        ASSIGN                                                   !1, $5
   33    12        ECHO                                                     '%3Ch2%3EExample+%231%3A+String+Data%3C%2Fh2%3E'
   34    13        CONCAT                                           ~7      'Data+to+be+Encrypted%3A+', !0
         14        CONCAT                                           ~8      ~7, '%3Cbr%2F%3E'
         15        ECHO                                                     ~8
   35    16        CONCAT                                           ~9      'Encrypted+Data%3A+', !1
         17        CONCAT                                           ~10     ~9, '%3Cbr%2F%3E'
         18        ECHO                                                     ~10
   36    19        INIT_FCALL                                               'mc_decrypt'
         20        SEND_VAR                                                 !1
         21        FETCH_CONSTANT                                   ~11     'ENCRYPTION_KEY'
         22        SEND_VAL                                                 ~11
         23        DO_FCALL                                      0  $12     
         24        CONCAT                                           ~13     'Decrypted+Data%3A+', $12
         25        CONCAT                                           ~14     ~13, '%3C%2Fbr%3E'
         26        ECHO                                                     ~14
   37    27        ASSIGN                                                   !0, <array>
   38    28        INIT_FCALL                                               'mc_encrypt'
         29        SEND_VAR                                                 !0
         30        FETCH_CONSTANT                                   ~16     'ENCRYPTION_KEY'
         31        SEND_VAL                                                 ~16
         32        DO_FCALL                                      0  $17     
         33        ASSIGN                                                   !1, $17
   40    34      > RETURN                                                   1

Function mc_encrypt:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3i5Vi
function name:  mc_encrypt
number of ops:  58
compiled vars:  !0 = $encrypt, !1 = $key, !2 = $iv, !3 = $mac, !4 = $passcrypt, !5 = $encoded
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        INIT_FCALL                                               'serialize'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !0, $6
    8     6        INIT_FCALL_BY_NAME                                       'mcrypt_create_iv'
          7        INIT_FCALL_BY_NAME                                       'mcrypt_get_iv_size'
          8        FETCH_CONSTANT                                   ~8      'MCRYPT_RIJNDAEL_256'
          9        SEND_VAL_EX                                              ~8
         10        FETCH_CONSTANT                                   ~9      'MCRYPT_MODE_CBC'
         11        SEND_VAL_EX                                              ~9
         12        DO_FCALL                                      0  $10     
         13        SEND_VAR_NO_REF_EX                                       $10
         14        FETCH_CONSTANT                                   ~11     'MCRYPT_DEV_URANDOM'
         15        SEND_VAL_EX                                              ~11
         16        DO_FCALL                                      0  $12     
         17        ASSIGN                                                   !2, $12
    9    18        INIT_FCALL                                               'pack'
         19        SEND_VAL                                                 'H%2A'
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                         $14     
         22        ASSIGN                                                   !1, $14
   10    23        INIT_FCALL                                               'hash_hmac'
         24        SEND_VAL                                                 'sha256'
         25        SEND_VAR                                                 !0
         26        INIT_FCALL                                               'substr'
         27        INIT_FCALL                                               'bin2hex'
         28        SEND_VAR                                                 !1
         29        DO_ICALL                                         $16     
         30        SEND_VAR                                                 $16
         31        SEND_VAL                                                 -32
         32        DO_ICALL                                         $17     
         33        SEND_VAR                                                 $17
         34        DO_ICALL                                         $18     
         35        ASSIGN                                                   !3, $18
   11    36        INIT_FCALL_BY_NAME                                       'mcrypt_encrypt'
         37        FETCH_CONSTANT                                   ~20     'MCRYPT_RIJNDAEL_256'
         38        SEND_VAL_EX                                              ~20
         39        SEND_VAR_EX                                              !1
         40        CONCAT                                           ~21     !0, !3
         41        SEND_VAL_EX                                              ~21
         42        FETCH_CONSTANT                                   ~22     'MCRYPT_MODE_CBC'
         43        SEND_VAL_EX                                              ~22
         44        SEND_VAR_EX                                              !2
         45        DO_FCALL                                      0  $23     
         46        ASSIGN                                                   !4, $23
   12    47        INIT_FCALL                                               'base64_encode'
         48        SEND_VAR                                                 !4
         49        DO_ICALL                                         $25     
         50        CONCAT                                           ~26     $25, '%7C'
         51        INIT_FCALL                                               'base64_encode'
         52        SEND_VAR                                                 !2
         53        DO_ICALL                                         $27     
         54        CONCAT                                           ~28     ~26, $27
         55        ASSIGN                                                   !5, ~28
   13    56      > RETURN                                                   !5
   14    57*     > RETURN                                                   null

End of function mc_encrypt

Function mc_decrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 28
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 72, Position 2 = 73
Branch analysis from position: 72
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 73
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3i5Vi
function name:  mc_decrypt
number of ops:  79
compiled vars:  !0 = $decrypt, !1 = $key, !2 = $decoded, !3 = $iv, !4 = $decrypted, !5 = $mac, !6 = $calcmac
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '%7C'
          4        CONCAT                                           ~7      !0, '%7C'
          5        SEND_VAL                                                 ~7
          6        DO_ICALL                                         $8      
          7        ASSIGN                                                   !0, $8
   18     8        INIT_FCALL                                               'base64_decode'
          9        FETCH_DIM_R                                      ~10     !0, 0
         10        SEND_VAL                                                 ~10
         11        DO_ICALL                                         $11     
         12        ASSIGN                                                   !2, $11
   19    13        INIT_FCALL                                               'base64_decode'
         14        FETCH_DIM_R                                      ~13     !0, 1
         15        SEND_VAL                                                 ~13
         16        DO_ICALL                                         $14     
         17        ASSIGN                                                   !3, $14
   20    18        STRLEN                                           ~16     !3
         19        INIT_FCALL_BY_NAME                                       'mcrypt_get_iv_size'
         20        FETCH_CONSTANT                                   ~17     'MCRYPT_RIJNDAEL_256'
         21        SEND_VAL_EX                                              ~17
         22        FETCH_CONSTANT                                   ~18     'MCRYPT_MODE_CBC'
         23        SEND_VAL_EX                                              ~18
         24        DO_FCALL                                      0  $19     
         25        IS_NOT_IDENTICAL                                         $19, ~16
         26      > JMPZ                                                     ~20, ->28
         27    > > RETURN                                                   <false>
   21    28    >   INIT_FCALL                                               'pack'
         29        SEND_VAL                                                 'H%2A'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                         $21     
         32        ASSIGN                                                   !1, $21
   22    33        INIT_FCALL                                               'trim'
         34        INIT_FCALL_BY_NAME                                       'mcrypt_decrypt'
         35        FETCH_CONSTANT                                   ~23     'MCRYPT_RIJNDAEL_256'
         36        SEND_VAL_EX                                              ~23
         37        SEND_VAR_EX                                              !1
         38        SEND_VAR_EX                                              !2
         39        FETCH_CONSTANT                                   ~24     'MCRYPT_MODE_CBC'
         40        SEND_VAL_EX                                              ~24
         41        SEND_VAR_EX                                              !3
         42        DO_FCALL                                      0  $25     
         43        SEND_VAR                                                 $25
         44        DO_ICALL                                         $26     
         45        ASSIGN                                                   !4, $26
   23    46        INIT_FCALL                                               'substr'
         47        SEND_VAR                                                 !4
         48        SEND_VAL                                                 -64
         49        DO_ICALL                                         $28     
         50        ASSIGN                                                   !5, $28
   24    51        INIT_FCALL                                               'substr'
         52        SEND_VAR                                                 !4
         53        SEND_VAL                                                 0
         54        SEND_VAL                                                 -64
         55        DO_ICALL                                         $30     
         56        ASSIGN                                                   !4, $30
   25    57        INIT_FCALL                                               'hash_hmac'
         58        SEND_VAL                                                 'sha256'
         59        SEND_VAR                                                 !4
         60        INIT_FCALL                                               'substr'
         61        INIT_FCALL                                               'bin2hex'
         62        SEND_VAR                                                 !1
         63        DO_ICALL                                         $32     
         64        SEND_VAR                                                 $32
         65        SEND_VAL                                                 -32
         66        DO_ICALL                                         $33     
         67        SEND_VAR                                                 $33
         68        DO_ICALL                                         $34     
         69        ASSIGN                                                   !6, $34
   26    70        IS_NOT_IDENTICAL                                         !6, !5
         71      > JMPZ                                                     ~36, ->73
         72    > > RETURN                                                   <false>
   27    73    >   INIT_FCALL                                               'unserialize'
         74        SEND_VAR                                                 !4
         75        DO_ICALL                                         $37     
         76        ASSIGN                                                   !4, $37
   28    77      > RETURN                                                   !4
   29    78*     > RETURN                                                   null

End of function mc_decrypt

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
221.24 ms | 1410 KiB | 38 Q