3v4l.org

run code in 300+ PHP versions simultaneously
<?php // This PHP code snippet provides a basic understanding of // PHP's AES encryption. // The first thing to understand is the meaning of these constants: // MCRYPT_RIJNDAEL_128 // MCRYPT_RIJNDAEL_192 // MCRYPT_RIJNDAEL_256 // You would think that MCRYPT_RIJNDAEL_256 specifies 256-bit encryption, // but that is wrong. The three choices specify the block-size to be used // with Rijndael encryption. They say nothing about the key size (i.e. strength) // of the encryption. (Read further to understand how the strength of the // AES encryption is set.) // // The Rijndael encyrption algorithm is a block cipher. It operates on discrete // blocks of data. Padding MUST be added such that // the data to be encrypted has a length that is a multiple of the block size. // (PHP pads with NULL bytes) // Thus, if you specify MCRYPT_RIJNDAEL_256, your encrypted output will always // be a multiple of 32 bytes (i.e. 256 bits). If you specify MCRYPT_RIJNDAEL_128, // your encrypted output will always be a multiple of 16 bytes. // // Note: Strictly speaking, AES is not precisely Rijndael (although in practice // they are used interchangeably) as Rijndael supports a larger range of block // and key sizes; AES has a fixed block size of 128 bits and a key size of // 128, 192, or 256 bits, whereas Rijndael can be specified with key and block // sizes in any multiple of 32 bits, with a minimum of 128 bits and a maximum of // 256 bits. // In summary: If you want to be AES compliant, always choose MCRYPT_RIJNDAEL_128. // // So the first step is to create the cipher object: $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); // We're using CBC mode (cipher-block chaining). Block cipher modes are detailed // here: http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation // CBC mode requires an initialization vector. The size of the IV (initialization // vector) is always equal to the block-size. (It is NOT equal to the key size.) // Given that our block size is 128-bits, the IV is also 128-bits (i.e. 16 bytes). // Thus, for AES encryption, the IV is always 16 bytes regardless of the // strength of encryption. // // Here's some PHP code to verify our IV size: $iv_size = mcrypt_enc_get_iv_size($cipher); printf("iv_size = %d\n",$iv_size); // How do you do 256-bit AES encryption in PHP vs. 128-bit AES encryption??? // The answer is: Give it a key that's 32 bytes long as opposed to 16 bytes long. // For example: $key256 = '12345678901234561234567890123456'; $key128 = '1234567890123456'; // Here's our 128-bit IV which is used for both 256-bit and 128-bit keys. $iv = '1234567890123456'; printf("iv: %s\n",bin2hex($iv)); printf("key256: %s\n",bin2hex($key256)); printf("key128: %s\n",bin2hex($key128)); // This is the plain-text to be encrypted: $cleartext = 'The quick brown fox jumped over the lazy dog.'; printf("plainText: %s\n\n",$cleartext); // The mcrypt_generic_init function initializes the cipher by specifying both // the key and the IV. The length of the key determines whether we're doing // 128-bit, 192-bit, or 256-bit encryption. // Let's do 256-bit encryption here: if (mcrypt_generic_init($cipher, $key256, $iv) != -1) { // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.. $cipherText = mcrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); // Display the result in hex. printf("256-bit encrypted result:\n%s\n\n",bin2hex($cipherText)); } // Now let's do 128-bit encryption: if (mcrypt_generic_init($cipher, $key128, $iv) != -1) { // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.. $cipherText = mcrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); // Display the result in hex. printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText)); } // ------- // Results // ------- // You may use these as test vectors for testing your AES implementations... // // ------------------------ // 256-bit key, CBC mode // ------------------------ // IV = '1234567890123456' // (hex: 31323334353637383930313233343536) // Key = '12345678901234561234567890123456' // (hex: 3132333435363738393031323334353631323334353637383930313233343536) // PlainText: // 'The quick brown fox jumped over the lazy dog' // CipherText(hex): // 2fddc3abec692e1572d9b7d629172a05caf230bc7c8fd2d26ccfd65f9c54526984f7cb1c4326ef058cd7bee3967299e3 // // ------------------------ // 128-bit key, CBC mode // ------------------------ // IV = '1234567890123456' // (hex: 31323334353637383930313233343536) // Key = '1234567890123456' // (hex: 31323334353637383930313233343536) // PlainText: // 'The quick brown fox jumped over the lazy dog' // CipherText(hex): // f78176ae8dfe84578529208d30f446bbb29a64dc388b5c0b63140a4f316b3f341fe7d3b1a3cc5113c81ef8dd714a1c99 ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 68
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 90
Branch analysis from position: 75
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 90
Branch analysis from position: 68
filename:       /in/XcJiX
function name:  (null)
number of ops:  91
compiled vars:  !0 = $cipher, !1 = $iv_size, !2 = $key256, !3 = $key128, !4 = $iv, !5 = $cleartext, !6 = $cipherText
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   INIT_FCALL_BY_NAME                                       'mcrypt_module_open'
          1        FETCH_CONSTANT                                   ~7      'MCRYPT_RIJNDAEL_128'
          2        SEND_VAL_EX                                              ~7
          3        SEND_VAL_EX                                              ''
          4        FETCH_CONSTANT                                   ~8      'MCRYPT_MODE_CBC'
          5        SEND_VAL_EX                                              ~8
          6        SEND_VAL_EX                                              ''
          7        DO_FCALL                                      0  $9      
          8        ASSIGN                                                   !0, $9
   45     9        INIT_FCALL_BY_NAME                                       'mcrypt_enc_get_iv_size'
         10        SEND_VAR_EX                                              !0
         11        DO_FCALL                                      0  $11     
         12        ASSIGN                                                   !1, $11
   46    13        INIT_FCALL                                               'printf'
         14        SEND_VAL                                                 'iv_size+%3D+%25d%0A'
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                                 
   51    17        ASSIGN                                                   !2, '12345678901234561234567890123456'
   52    18        ASSIGN                                                   !3, '1234567890123456'
   55    19        ASSIGN                                                   !4, '1234567890123456'
   57    20        INIT_FCALL                                               'printf'
         21        SEND_VAL                                                 'iv%3A+%25s%0A'
         22        INIT_FCALL                                               'bin2hex'
         23        SEND_VAR                                                 !4
         24        DO_ICALL                                         $17     
         25        SEND_VAR                                                 $17
         26        DO_ICALL                                                 
   58    27        INIT_FCALL                                               'printf'
         28        SEND_VAL                                                 'key256%3A+%25s%0A'
         29        INIT_FCALL                                               'bin2hex'
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $19     
         32        SEND_VAR                                                 $19
         33        DO_ICALL                                                 
   59    34        INIT_FCALL                                               'printf'
         35        SEND_VAL                                                 'key128%3A+%25s%0A'
         36        INIT_FCALL                                               'bin2hex'
         37        SEND_VAR                                                 !3
         38        DO_ICALL                                         $21     
         39        SEND_VAR                                                 $21
         40        DO_ICALL                                                 
   62    41        ASSIGN                                                   !5, 'The+quick+brown+fox+jumped+over+the+lazy+dog.'
   63    42        INIT_FCALL                                               'printf'
         43        SEND_VAL                                                 'plainText%3A+%25s%0A%0A'
         44        SEND_VAR                                                 !5
         45        DO_ICALL                                                 
   69    46        INIT_FCALL_BY_NAME                                       'mcrypt_generic_init'
         47        SEND_VAR_EX                                              !0
         48        SEND_VAR_EX                                              !2
         49        SEND_VAR_EX                                              !4
         50        DO_FCALL                                      0  $25     
         51        IS_NOT_EQUAL                                             $25, -1
         52      > JMPZ                                                     ~26, ->68
   72    53    >   INIT_FCALL_BY_NAME                                       'mcrypt_generic'
         54        SEND_VAR_EX                                              !0
         55        SEND_VAR_EX                                              !5
         56        DO_FCALL                                      0  $27     
         57        ASSIGN                                                   !6, $27
   73    58        INIT_FCALL_BY_NAME                                       'mcrypt_generic_deinit'
         59        SEND_VAR_EX                                              !0
         60        DO_FCALL                                      0          
   76    61        INIT_FCALL                                               'printf'
         62        SEND_VAL                                                 '256-bit+encrypted+result%3A%0A%25s%0A%0A'
         63        INIT_FCALL                                               'bin2hex'
         64        SEND_VAR                                                 !6
         65        DO_ICALL                                         $30     
         66        SEND_VAR                                                 $30
         67        DO_ICALL                                                 
   80    68    >   INIT_FCALL_BY_NAME                                       'mcrypt_generic_init'
         69        SEND_VAR_EX                                              !0
         70        SEND_VAR_EX                                              !3
         71        SEND_VAR_EX                                              !4
         72        DO_FCALL                                      0  $32     
         73        IS_NOT_EQUAL                                             $32, -1
         74      > JMPZ                                                     ~33, ->90
   83    75    >   INIT_FCALL_BY_NAME                                       'mcrypt_generic'
         76        SEND_VAR_EX                                              !0
         77        SEND_VAR_EX                                              !5
         78        DO_FCALL                                      0  $34     
         79        ASSIGN                                                   !6, $34
   84    80        INIT_FCALL_BY_NAME                                       'mcrypt_generic_deinit'
         81        SEND_VAR_EX                                              !0
         82        DO_FCALL                                      0          
   87    83        INIT_FCALL                                               'printf'
         84        SEND_VAL                                                 '128-bit+encrypted+result%3A%0A%25s%0A%0A'
         85        INIT_FCALL                                               'bin2hex'
         86        SEND_VAR                                                 !6
         87        DO_ICALL                                         $37     
         88        SEND_VAR                                                 $37
         89        DO_ICALL                                                 
  120    90    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.01 ms | 1408 KiB | 17 Q