3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Cipher * * Simple mcrypt interface. * * Cipher is a simple class for working with mcrypt. * * @package Cipher * @author Nathan Lucas <nathan@gimpstraw.com> * @link http://www.gimpstraw.com/ * @copyright Copyright (c) 2008, Nathan Lucas * @version 2.0.0 * * Added $iv to both encrypt() and decrypt() allowing you to use preset IVs * while encrypting/decrypting data. * * Also added getIV(), which returns the instance's current IV in base64 * allowing you to store this IV for use on other instances of Cipher. */ class Cipher { /** * Algorithm to use. * * @access private * @var string */ private $algo; /** * Encryption mode. * * @access private * @var string */ private $mode; /** * Randomization source. * * @access private * @var integer */ private $source; /** * Initialization vector. * * @access private * @var string */ private $iv = null; /** * Encryption key. * * @access private * @var string */ private $key = null; /** * Cipher($algo, $mode, $source) * * Cipher constructor. Sets the algorithm being used, the encryption * mode, and the IV. * * @param string $algo * @param string $mode * @param integer $source (randomization source) * @access public * @return void */ public function __construct($algo = MCRYPT_3DES, $mode = MCRYPT_MODE_CBC, $source = MCRYPT_RAND) { $this->algo = $algo; $this->mode = $mode; $this->source = $source; if (is_null($this->algo) || (strlen($this->algo) == 0)) { $this->algo = MCRYPT_3DES; } if (is_null($this->mode) || (strlen($this->mode) == 0)) { $this->mode = MCRYPT_MODE_CBC; } } /** * encrypt($data, $key, $iv) * * Returns encrpyted $data, base64 encoded. $key must be specified at * least once, it can be changed at any point. * * @param string $data * @param mixed $key * @param string $iv * @access public * @return string */ public function encrypt($data, $key = null, $iv = null) { $key = (strlen($key) == 0) ? $key = null : $key; $this->setKey($key); $this->setIV($iv); $out = mcrypt_encrypt($this->algo, $this->key, $data, $this->mode, $this->iv); return base64_encode($out); } /** * decrypt($data, $key, $iv) * * Returns decrypted $data. $key must be specified at least once, it can * be changed at any point. * * @param mixed $data * @param mixed $key * @param string $iv * @access public * @return string */ public function decrypt($data, $key = null, $iv = null) { $key = (strlen($key) == 0) ? $key = null : $key; $this->setKey($key); $this->setIV($iv); $data = base64_decode($data); $out = mcrypt_decrypt($this->algo, $this->key, $data, $this->mode, $this->iv); return trim($out); } /** * getIV() * * Returns the IV used for encryption so you can use it again in another * Cipher instance to decrypt data. * * @access public * @return string */ public function getIV() { return base64_encode($this->iv); } /** * setIV($iv) * * Sets IV. If $iv is specified, the instance IV will be set to this. If not, * the instance will generate an IV. * * @param string $iv * @access private * @return void */ private function setIV($iv) { if (!is_null($iv)) { $this->iv = base64_decode($iv); } if (is_null($this->iv)) { $iv_size = mcrypt_get_iv_size($this->algo, $this->mode); $this->iv = mcrypt_create_iv($iv_size, $this->source); } } /** * setKey($data, $key) * * Sets Cipher::key. This will be the key used for the encrypt and decrypt * methods until another $key is specified. This will trigger an error if * no initial key is set. * * @param mixed $key * @access private * @return void */ private function setKey($key) { if (!is_null($key)) { $key_size = mcrypt_get_key_size($this->algo, $this->mode); $this->key = hash("sha256", $key, true); $this->key = substr($this->key, 0, $key_size); } if (is_null($this->key)) { trigger_error("You must specify a key at least once in either Cipher::encrpyt() or Cipher::decrypt().", E_USER_ERROR); } } } $cipher = new Cipher(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB); $kunci = "%^$%^&%*HJGHJK"; $string="sembunyikan aku"; $en = $cipher->encrypt($string, $kunci); $de = $cipher->decrypt($en, $kunci); echo "Enkrispi Kata : $string \n"; echo "Hasil Enkripsi : $en \n"; echo "Hasil Dekrispi : $de \n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SshCb
function name:  (null)
number of ops:  32
compiled vars:  !0 = $cipher, !1 = $kunci, !2 = $string, !3 = $en, !4 = $de
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  190     0  E >   NEW                                              $5      'Cipher'
          1        FETCH_CONSTANT                                   ~6      'MCRYPT_BLOWFISH'
          2        SEND_VAL_EX                                              ~6
          3        FETCH_CONSTANT                                   ~7      'MCRYPT_MODE_ECB'
          4        SEND_VAL_EX                                              ~7
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !0, $5
  192     7        ASSIGN                                                   !1, '%25%5E%24%25%5E%26%25%2AHJGHJK'
  193     8        ASSIGN                                                   !2, 'sembunyikan+aku'
  195     9        INIT_METHOD_CALL                                         !0, 'encrypt'
         10        SEND_VAR_EX                                              !2
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $12     
         13        ASSIGN                                                   !3, $12
  196    14        INIT_METHOD_CALL                                         !0, 'decrypt'
         15        SEND_VAR_EX                                              !3
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $14     
         18        ASSIGN                                                   !4, $14
  198    19        ROPE_INIT                                     3  ~17     'Enkrispi+Kata+%3A+'
         20        ROPE_ADD                                      1  ~17     ~17, !2
         21        ROPE_END                                      2  ~16     ~17, '+%0A'
         22        ECHO                                                     ~16
  199    23        ROPE_INIT                                     3  ~20     'Hasil+Enkripsi+%3A+'
         24        ROPE_ADD                                      1  ~20     ~20, !3
         25        ROPE_END                                      2  ~19     ~20, '+%0A'
         26        ECHO                                                     ~19
  200    27        ROPE_INIT                                     3  ~23     'Hasil+Dekrispi+%3A+'
         28        ROPE_ADD                                      1  ~23     ~23, !4
         29        ROPE_END                                      2  ~22     ~23, '+%0A'
         30        ECHO                                                     ~22
         31      > RETURN                                                   1

Class Cipher:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 20
Branch analysis from position: 17
2 jumps found. (Code = 47) Position 1 = 23, Position 2 = 27
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 31
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 27
Branch analysis from position: 20
Branch analysis from position: 16
filename:       /in/SshCb
function name:  __construct
number of ops:  32
compiled vars:  !0 = $algo, !1 = $mode, !2 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV_INIT                                        !0      <const ast>
          1        RECV_INIT                                        !1      <const ast>
          2        RECV_INIT                                        !2      <const ast>
   77     3        ASSIGN_OBJ                                               'algo'
          4        OP_DATA                                                  !0
   78     5        ASSIGN_OBJ                                               'mode'
          6        OP_DATA                                                  !1
   79     7        ASSIGN_OBJ                                               'source'
          8        OP_DATA                                                  !2
   81     9        FETCH_OBJ_R                                      ~6      'algo'
         10        TYPE_CHECK                                    2  ~7      ~6
         11      > JMPNZ_EX                                         ~7      ~7, ->16
         12    >   FETCH_OBJ_R                                      ~8      'algo'
         13        STRLEN                                           ~9      ~8
         14        IS_EQUAL                                         ~10     ~9, 0
         15        BOOL                                             ~7      ~10
         16    > > JMPZ                                                     ~7, ->20
   82    17    >   FETCH_CONSTANT                                   ~12     'MCRYPT_3DES'
         18        ASSIGN_OBJ                                               'algo'
         19        OP_DATA                                                  ~12
   84    20    >   FETCH_OBJ_R                                      ~13     'mode'
         21        TYPE_CHECK                                    2  ~14     ~13
         22      > JMPNZ_EX                                         ~14     ~14, ->27
         23    >   FETCH_OBJ_R                                      ~15     'mode'
         24        STRLEN                                           ~16     ~15
         25        IS_EQUAL                                         ~17     ~16, 0
         26        BOOL                                             ~14     ~17
         27    > > JMPZ                                                     ~14, ->31
   85    28    >   FETCH_CONSTANT                                   ~19     'MCRYPT_MODE_CBC'
         29        ASSIGN_OBJ                                               'mode'
         30        OP_DATA                                                  ~19
   87    31    > > RETURN                                                   null

End of function __construct

Function encrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SshCb
function name:  encrypt
number of ops:  38
compiled vars:  !0 = $data, !1 = $key, !2 = $iv, !3 = $out
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      null
  102     3        STRLEN                                           ~4      !1
          4        IS_EQUAL                                                 ~4, 0
          5      > JMPZ                                                     ~5, ->9
          6    >   ASSIGN                                           ~6      !1, null
          7        QM_ASSIGN                                        ~7      ~6
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~7      !1
         10    >   ASSIGN                                                   !1, ~7
  104    11        INIT_METHOD_CALL                                         'setKey'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
  105    14        INIT_METHOD_CALL                                         'setIV'
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0          
  107    17        INIT_FCALL_BY_NAME                                       'mcrypt_encrypt'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_OBJ_FUNC_ARG                               $11     'algo'
         20        SEND_FUNC_ARG                                            $11
         21        CHECK_FUNC_ARG                                           
         22        FETCH_OBJ_FUNC_ARG                               $12     'key'
         23        SEND_FUNC_ARG                                            $12
         24        SEND_VAR_EX                                              !0
         25        CHECK_FUNC_ARG                                           
         26        FETCH_OBJ_FUNC_ARG                               $13     'mode'
         27        SEND_FUNC_ARG                                            $13
         28        CHECK_FUNC_ARG                                           
         29        FETCH_OBJ_FUNC_ARG                               $14     'iv'
         30        SEND_FUNC_ARG                                            $14
         31        DO_FCALL                                      0  $15     
         32        ASSIGN                                                   !3, $15
  108    33        INIT_FCALL                                               'base64_encode'
         34        SEND_VAR                                                 !3
         35        DO_ICALL                                         $17     
         36      > RETURN                                                   $17
  109    37*     > RETURN                                                   null

End of function encrypt

Function decrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SshCb
function name:  decrypt
number of ops:  42
compiled vars:  !0 = $data, !1 = $key, !2 = $iv, !3 = $out
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  123     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
          2        RECV_INIT                                        !2      null
  124     3        STRLEN                                           ~4      !1
          4        IS_EQUAL                                                 ~4, 0
          5      > JMPZ                                                     ~5, ->9
          6    >   ASSIGN                                           ~6      !1, null
          7        QM_ASSIGN                                        ~7      ~6
          8      > JMP                                                      ->10
          9    >   QM_ASSIGN                                        ~7      !1
         10    >   ASSIGN                                                   !1, ~7
  126    11        INIT_METHOD_CALL                                         'setKey'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
  127    14        INIT_METHOD_CALL                                         'setIV'
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0          
  129    17        INIT_FCALL                                               'base64_decode'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $11     
         20        ASSIGN                                                   !0, $11
  130    21        INIT_FCALL_BY_NAME                                       'mcrypt_decrypt'
         22        CHECK_FUNC_ARG                                           
         23        FETCH_OBJ_FUNC_ARG                               $13     'algo'
         24        SEND_FUNC_ARG                                            $13
         25        CHECK_FUNC_ARG                                           
         26        FETCH_OBJ_FUNC_ARG                               $14     'key'
         27        SEND_FUNC_ARG                                            $14
         28        SEND_VAR_EX                                              !0
         29        CHECK_FUNC_ARG                                           
         30        FETCH_OBJ_FUNC_ARG                               $15     'mode'
         31        SEND_FUNC_ARG                                            $15
         32        CHECK_FUNC_ARG                                           
         33        FETCH_OBJ_FUNC_ARG                               $16     'iv'
         34        SEND_FUNC_ARG                                            $16
         35        DO_FCALL                                      0  $17     
         36        ASSIGN                                                   !3, $17
  131    37        INIT_FCALL                                               'trim'
         38        SEND_VAR                                                 !3
         39        DO_ICALL                                         $19     
         40      > RETURN                                                   $19
  132    41*     > RETURN                                                   null

End of function decrypt

Function getiv:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SshCb
function name:  getIV
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  144     0  E >   INIT_FCALL                                               'base64_encode'
          1        FETCH_OBJ_R                                      ~0      'iv'
          2        SEND_VAL                                                 ~0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
  145     5*     > RETURN                                                   null

End of function getiv

Function setiv:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 29
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
Branch analysis from position: 9
filename:       /in/SshCb
function name:  setIV
number of ops:  30
compiled vars:  !0 = $iv, !1 = $iv_size
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  157     0  E >   RECV                                             !0      
  158     1        TYPE_CHECK                                    2  ~2      !0
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->9
  159     4    >   INIT_FCALL                                               'base64_decode'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $5      
          7        ASSIGN_OBJ                                               'iv'
          8        OP_DATA                                                  $5
  161     9    >   FETCH_OBJ_R                                      ~6      'iv'
         10        TYPE_CHECK                                    2          ~6
         11      > JMPZ                                                     ~7, ->29
  162    12    >   INIT_FCALL_BY_NAME                                       'mcrypt_get_iv_size'
         13        CHECK_FUNC_ARG                                           
         14        FETCH_OBJ_FUNC_ARG                               $8      'algo'
         15        SEND_FUNC_ARG                                            $8
         16        CHECK_FUNC_ARG                                           
         17        FETCH_OBJ_FUNC_ARG                               $9      'mode'
         18        SEND_FUNC_ARG                                            $9
         19        DO_FCALL                                      0  $10     
         20        ASSIGN                                                   !1, $10
  163    21        INIT_FCALL_BY_NAME                                       'mcrypt_create_iv'
         22        SEND_VAR_EX                                              !1
         23        CHECK_FUNC_ARG                                           
         24        FETCH_OBJ_FUNC_ARG                               $13     'source'
         25        SEND_FUNC_ARG                                            $13
         26        DO_FCALL                                      0  $14     
         27        ASSIGN_OBJ                                               'iv'
         28        OP_DATA                                                  $14
  165    29    > > RETURN                                                   null

End of function setiv

Function setkey:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 28
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 35
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
Branch analysis from position: 28
filename:       /in/SshCb
function name:  setKey
number of ops:  36
compiled vars:  !0 = $key, !1 = $key_size
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  178     0  E >   RECV                                             !0      
  179     1        TYPE_CHECK                                    2  ~2      !0
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ                                                     ~3, ->28
  180     4    >   INIT_FCALL_BY_NAME                                       'mcrypt_get_key_size'
          5        CHECK_FUNC_ARG                                           
          6        FETCH_OBJ_FUNC_ARG                               $4      'algo'
          7        SEND_FUNC_ARG                                            $4
          8        CHECK_FUNC_ARG                                           
          9        FETCH_OBJ_FUNC_ARG                               $5      'mode'
         10        SEND_FUNC_ARG                                            $5
         11        DO_FCALL                                      0  $6      
         12        ASSIGN                                                   !1, $6
  181    13        INIT_FCALL                                               'hash'
         14        SEND_VAL                                                 'sha256'
         15        SEND_VAR                                                 !0
         16        SEND_VAL                                                 <true>
         17        DO_ICALL                                         $9      
         18        ASSIGN_OBJ                                               'key'
         19        OP_DATA                                                  $9
  182    20        INIT_FCALL                                               'substr'
         21        FETCH_OBJ_R                                      ~11     'key'
         22        SEND_VAL                                                 ~11
         23        SEND_VAL                                                 0
         24        SEND_VAR                                                 !1
         25        DO_ICALL                                         $12     
         26        ASSIGN_OBJ                                               'key'
         27        OP_DATA                                                  $12
  184    28    >   FETCH_OBJ_R                                      ~13     'key'
         29        TYPE_CHECK                                    2          ~13
         30      > JMPZ                                                     ~14, ->35
  185    31    >   INIT_FCALL                                               'trigger_error'
         32        SEND_VAL                                                 'You+must+specify+a+key+at+least+once+in+either+Cipher%3A%3Aencrpyt%28%29+or+Cipher%3A%3Adecrypt%28%29.'
         33        SEND_VAL                                                 256
         34        DO_ICALL                                                 
  187    35    > > RETURN                                                   null

End of function setkey

End of class Cipher.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
179.91 ms | 1416 KiB | 25 Q