3v4l.org

run code in 300+ PHP versions simultaneously
<?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; } } $message = 'Ready your ammunition; we attack at dawn.'; $key = hex2bin('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'); $encrypted = UnsafeCrypto::encrypt($message, $key, true); $decrypted = UnsafeCrypto::decrypt($encrypted, $key, true); var_dump($encrypted, $decrypted);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/f5K93
function name:  (null)
number of ops:  22
compiled vars:  !0 = $message, !1 = $key, !2 = $encrypted, !3 = $decrypted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   ASSIGN                                                   !0, 'Ready+your+ammunition%3B+we+attack+at+dawn.'
   74     1        INIT_FCALL                                               'hex2bin'
          2        SEND_VAL                                                 '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
          3        DO_ICALL                                         $5      
          4        ASSIGN                                                   !1, $5
   76     5        INIT_STATIC_METHOD_CALL                                  'UnsafeCrypto', 'encrypt'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        SEND_VAL                                                 <true>
          9        DO_FCALL                                      0  $7      
         10        ASSIGN                                                   !2, $7
   77    11        INIT_STATIC_METHOD_CALL                                  'UnsafeCrypto', 'decrypt'
         12        SEND_VAR                                                 !2
         13        SEND_VAR                                                 !1
         14        SEND_VAL                                                 <true>
         15        DO_FCALL                                      0  $9      
         16        ASSIGN                                                   !3, $9
   79    17        INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !2
         19        SEND_VAR                                                 !3
         20        DO_ICALL                                                 
         21      > RETURN                                                   1

Class UnsafeCrypto:
Function encrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/f5K93
function name:  encrypt
number of ops:  29
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_BY_NAME                                       'openssl_cipher_iv_length'
          4        SEND_VAL_EX                                              'aes-256-ctr'
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !3, $6
   22     7        INIT_FCALL_BY_NAME                                       'openssl_random_pseudo_bytes'
          8        SEND_VAR_EX                                              !3
          9        DO_FCALL                                      0  $8      
         10        ASSIGN                                                   !4, $8
   24    11        INIT_FCALL_BY_NAME                                       'openssl_encrypt'
   25    12        SEND_VAR_EX                                              !0
   26    13        SEND_VAL_EX                                              'aes-256-ctr'
   25    14        SEND_VAR_EX                                              !1
   28    15        FETCH_CONSTANT                                   ~10     'OPENSSL_RAW_DATA'
         16        SEND_VAL_EX                                              ~10
   25    17        SEND_VAR_EX                                              !4
   24    18        DO_FCALL                                      0  $11     
         19        ASSIGN                                                   !5, $11
   34    20      > JMPZ                                                     !2, ->26
   35    21    >   INIT_FCALL                                               'base64_encode'
         22        CONCAT                                           ~13     !4, !5
         23        SEND_VAL                                                 ~13
         24        DO_ICALL                                         $14     
         25      > RETURN                                                   $14
   37    26    >   CONCAT                                           ~15     !4, !5
         27      > RETURN                                                   ~15
   38    28*     > RETURN                                                   null

End of function encrypt

Function decrypt:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 15
Branch analysis from position: 11
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/f5K93
function name:  decrypt
number of ops:  44
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        TYPE_CHECK                                    4          !0
         10      > JMPZ                                                     ~9, ->15
   53    11    >   NEW                                              $10     'Exception'
         12        SEND_VAL_EX                                              'Encryption+failure'
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $10
   57    15    >   INIT_FCALL_BY_NAME                                       'openssl_cipher_iv_length'
         16        SEND_VAL_EX                                              'aes-256-ctr'
         17        DO_FCALL                                      0  $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_BY_NAME                                       'openssl_decrypt'
   62    34        SEND_VAR_EX                                              !5
   63    35        SEND_VAL_EX                                              'aes-256-ctr'
   62    36        SEND_VAR_EX                                              !1
   65    37        FETCH_CONSTANT                                   ~18     'OPENSSL_RAW_DATA'
         38        SEND_VAL_EX                                              ~18
   62    39        SEND_VAR_EX                                              !4
   61    40        DO_FCALL                                      0  $19     
         41        ASSIGN                                                   !6, $19
   69    42      > RETURN                                                   !6
   70    43*     > RETURN                                                   null

End of function decrypt

End of class UnsafeCrypto.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.94 ms | 1450 KiB | 18 Q