3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * http://stackoverflow.com/questions/9262109/php-simplest-two-way-encryption/30189841#30189841 * * This is not safe to use */ class UnsafeCrypto { const METHOD = 'aes-256-ctr'; /** * Encrypts (but does not authenticate) a message * * @param string $message - plaintext message * @param string $key - encryption key (raw binary expected) * @param boolean $encode - set to TRUE to return a base64-encoded * @return string (raw binary) */ public static function encrypt($message, $key, $encode = false) { $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = openssl_random_pseudo_bytes($nonceSize); $ciphertext = openssl_encrypt( $message, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); // Now let's pack the IV and the ciphertext together // Naively, we can just concatenate if ($encode) { return base64_encode($nonce.$ciphertext); } return $nonce.$ciphertext; } /** * Decrypts (but does not verify) a message * * @param string $message - ciphertext message * @param string $key - encryption key (raw binary expected) * @param boolean $encoded - are we expecting an encoded string? * @return string */ public static function decrypt($message, $key, $encoded = false) { if ($encoded) { $message = base64_decode($message, true); if ($message === false) { throw new Exception('Encryption failure'); } } $nonceSize = openssl_cipher_iv_length(self::METHOD); $nonce = mb_substr($message, 0, $nonceSize, '8bit'); $ciphertext = mb_substr($message, $nonceSize, null, '8bit'); $plaintext = openssl_decrypt( $ciphertext, self::METHOD, $key, OPENSSL_RAW_DATA, $nonce ); return $plaintext; } } $id = md5(uniqid()); var_dump($id); $key = hex2bin($id); var_dump($key); $message = 'Ready your ammunition; we attack at dawn.'; $encrypted = UnsafeCrypto::encrypt($message, $key); $decrypted = UnsafeCrypto::decrypt($encrypted, $key); var_dump($encrypted, $decrypted);
based on vsqNe
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 62) Position 1 = -2
filename:       /in/TJaI1
function name:  (null)
number of ops:  33
compiled vars:  !0 = $id, !1 = $key, !2 = $message, !3 = $encrypted, !4 = $decrypted
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   7     0  E >   NOP                                                      
  74     1        INIT_FCALL                                               'md5'
         2        INIT_FCALL                                               'uniqid'
         3        DO_ICALL                                         $6      
         4        SEND_VAR                                                 $6
         5        DO_ICALL                                         $7      
         6        ASSIGN                                                   !0, $7
  75     7        INIT_FCALL                                               'var_dump'
         8        SEND_VAR                                                 !0
         9        DO_ICALL                                                 
  76    10        INIT_FCALL                                               'hex2bin'
        11        SEND_VAR                                                 !0
        12        DO_ICALL                                         $10     
        13        ASSIGN                                                   !1, $10
  77    14        INIT_FCALL                                               'var_dump'
        15        SEND_VAR                                                 !1
        16        DO_ICALL                                                 
  79    17        ASSIGN                                                   !2, 'Ready+your+ammunition%3B+we+attack+at+dawn.'
  81    18        INIT_STATIC_METHOD_CALL                                  'UnsafeCrypto', 'encrypt'
        19        SEND_VAR                                                 !2
        20        SEND_VAR                                                 !1
        21        DO_FCALL                                      0  $14     
        22        ASSIGN                                                   !3, $14
  82    23        INIT_STATIC_METHOD_CALL                                  'UnsafeCrypto', 'decrypt'
        24        SEND_VAR                                                 !3
        25        SEND_VAR                                                 !1
        26        DO_FCALL                                      0  $16     
        27        ASSIGN                                                   !4, $16
  84    28        INIT_FCALL                                               'var_dump'
        29        SEND_VAR                                                 !3
        30        SEND_VAR                                                 !4
        31        DO_ICALL                                                 
        32      > RETURN                                                   1

Class UnsafeCrypto:
Function encrypt:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 43) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Jump found. (Code = 62) Position 1 = -2
filename:       /in/TJaI1
function name:  encrypt
number of ops:  28
compiled vars:  !0 = $message, !1 = $key, !2 = $encode, !3 = $nonceSize, !4 = $nonce, !5 = $ciphertext
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  19     0  E >   RECV                                             !0      
         1        RECV                                             !1      
         2        RECV_INIT                                        !2      <false>
  21     3        INIT_FCALL                                               'openssl_cipher_iv_length'
         4        SEND_VAL                                                 'aes-256-ctr'
         5        DO_ICALL                                         $6      
         6        ASSIGN                                                   !3, $6
  22     7        INIT_FCALL                                               'openssl_random_pseudo_bytes'
         8        SEND_VAR                                                 !3
         9        DO_ICALL                                         $8      
        10        ASSIGN                                                   !4, $8
  24    11        INIT_FCALL                                               'openssl_encrypt'
        12        SEND_VAR                                                 !0
  26    13        SEND_VAL                                                 'aes-256-ctr'
        14        SEND_VAR                                                 !1
  28    15        SEND_VAL                                                 1
        16        SEND_VAR                                                 !4
        17        DO_ICALL                                         $10     
        18        ASSIGN                                                   !5, $10
  34    19      > JMPZ                                                     !2, ->25
  35    20    >   INIT_FCALL                                               'base64_encode'
        21        CONCAT                                           ~12     !4, !5
        22        SEND_VAL                                                 ~12
        23        DO_ICALL                                         $13     
        24      > RETURN                                                   $13
  37    25    >   CONCAT                                           ~14     !4, !5
        26      > RETURN                                                   ~14
  38    27*     > RETURN                                                   null

End of function encrypt

Function decrypt:
Finding entry points
Branch analysis from position: 0
Jump found. (Code = 43) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
Jump found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
Jump found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
Jump found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/TJaI1
function name:  decrypt
number of ops:  43
compiled vars:  !0 = $message, !1 = $key, !2 = $encoded, !3 = $nonceSize, !4 = $nonce, !5 = $ciphertext, !6 = $plaintext
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  48     0  E >   RECV                                             !0      
         1        RECV                                             !1      
         2        RECV_INIT                                        !2      <false>
  50     3      > JMPZ                                                     !2, ->15
  51     4    >   INIT_FCALL                                               'base64_decode'
         5        SEND_VAR                                                 !0
         6        SEND_VAL                                                 <true>
         7        DO_ICALL                                         $7      
         8        ASSIGN                                                   !0, $7
  52     9        IS_IDENTICAL                                     ~9      !0, <false>
        10      > JMPZ                                                     ~9, ->15
  53    11    >   NEW                                              $10     :-1
        12        SEND_VAL_EX                                              'Encryption+failure'
        13        DO_FCALL                                      0          
        14      > THROW                                         0          $10
  57    15    >   INIT_FCALL                                               'openssl_cipher_iv_length'
        16        SEND_VAL                                                 'aes-256-ctr'
        17        DO_ICALL                                         $12     
        18        ASSIGN                                                   !3, $12
  58    19        INIT_FCALL                                               'mb_substr'
        20        SEND_VAR                                                 !0
        21        SEND_VAL                                                 0
        22        SEND_VAR                                                 !3
        23        SEND_VAL                                                 '8bit'
        24        DO_ICALL                                         $14     
        25        ASSIGN                                                   !4, $14
  59    26        INIT_FCALL                                               'mb_substr'
        27        SEND_VAR                                                 !0
        28        SEND_VAR                                                 !3
        29        SEND_VAL                                                 null
        30        SEND_VAL                                                 '8bit'
        31        DO_ICALL                                         $16     
        32        ASSIGN                                                   !5, $16
  61    33        INIT_FCALL                                               'openssl_decrypt'
        34        SEND_VAR                                                 !5
  63    35        SEND_VAL                                                 'aes-256-ctr'
        36        SEND_VAR                                                 !1
  65    37        SEND_VAL                                                 1
        38        SEND_VAR                                                 !4
        39        DO_ICALL                                         $18     
        40        ASSIGN                                                   !6, $18
  69    41      > RETURN                                                   !6
  70    42*     > RETURN                                                   null

End of function decrypt

End of class UnsafeCrypto.

Generated using Vulcan Logic Dumper, using php 7.3.0