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; } } $id = md5(uniqid()); var_dump($id); $key = hex2bin($id); var_dump($hex); $message = 'Ready your ammunition; we attack at dawn.'; $encrypted = UnsafeCrypto::encrypt($message, $key); $decrypted = UnsafeCrypto::decrypt($encrypted, $key); var_dump($encrypted, $decrypted);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/vsqNe
function name:  (null)
number of ops:  32
compiled vars:  !0 = $id, !1 = $key, !2 = $hex, !3 = $message, !4 = $encrypted, !5 = $decrypted
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   INIT_FCALL                                               'md5'
          1        INIT_FCALL                                               'uniqid'
          2        DO_ICALL                                         $6      
          3        SEND_VAR                                                 $6
          4        DO_ICALL                                         $7      
          5        ASSIGN                                                   !0, $7
   75     6        INIT_FCALL                                               'var_dump'
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                                 
   76     9        INIT_FCALL                                               'hex2bin'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $10     
         12        ASSIGN                                                   !1, $10
   77    13        INIT_FCALL                                               'var_dump'
         14        SEND_VAR                                                 !2
         15        DO_ICALL                                                 
   79    16        ASSIGN                                                   !3, 'Ready+your+ammunition%3B+we+attack+at+dawn.'
   81    17        INIT_STATIC_METHOD_CALL                                  'UnsafeCrypto', 'encrypt'
         18        SEND_VAR                                                 !3
         19        SEND_VAR                                                 !1
         20        DO_FCALL                                      0  $14     
         21        ASSIGN                                                   !4, $14
   82    22        INIT_STATIC_METHOD_CALL                                  'UnsafeCrypto', 'decrypt'
         23        SEND_VAR                                                 !4
         24        SEND_VAR                                                 !1
         25        DO_FCALL                                      0  $16     
         26        ASSIGN                                                   !5, $16
   84    27        INIT_FCALL                                               'var_dump'
         28        SEND_VAR                                                 !4
         29        SEND_VAR                                                 !5
         30        DO_ICALL                                                 
         31      > 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/vsqNe
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
         18        DO_FCALL                                      0  $11     
   24    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/vsqNe
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
         40        DO_FCALL                                      0  $19     
   61    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:
152.4 ms | 1404 KiB | 27 Q