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_CFB, ''); // 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 = 'a7fc844d17f43955783d7d6f5df7eb4e'; // 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 = '5854889097648545'; 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)); } // ------- // 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 ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0110.00416.50
8.3.50.0080.00817.80
8.3.40.0070.01018.92
8.3.30.0070.00718.79
8.3.20.0050.00220.15
8.3.10.0060.00618.69
8.3.00.0060.00321.85
8.2.180.0170.00316.63
8.2.170.0040.01122.96
8.2.160.0070.00722.26
8.2.150.0000.00824.18
8.2.140.0080.00024.66
8.2.130.0040.00420.79
8.2.120.0040.00426.35
8.2.110.0070.00419.09
8.2.100.0080.00417.80
8.2.90.0040.00419.09
8.2.80.0040.00417.97
8.2.70.0040.00417.63
8.2.60.0080.00017.91
8.2.50.0050.00318.09
8.2.40.0000.00822.27
8.2.30.0050.00319.34
8.2.20.0070.00017.71
8.2.10.0000.00718.09
8.2.00.0020.00518.07
8.1.280.0150.00325.92
8.1.270.0000.00822.06
8.1.260.0000.00828.09
8.1.250.0050.00328.09
8.1.240.0070.00322.04
8.1.230.0030.00717.70
8.1.220.0000.00817.74
8.1.210.0040.00418.77
8.1.200.0080.00317.22
8.1.190.0000.00817.13
8.1.180.0040.00418.10
8.1.170.0000.00818.49
8.1.160.0040.00420.68
8.1.150.0050.00518.86
8.1.140.0030.00519.48
8.1.130.0030.00317.52
8.1.120.0080.00017.25
8.1.110.0030.00517.26
8.1.100.0000.00717.40
8.1.90.0040.00417.39
8.1.80.0000.00817.35
8.1.70.0030.00317.40
8.1.60.0000.00717.52
8.1.50.0000.00817.37
8.1.40.0050.00317.41
8.1.30.0000.00817.62
8.1.20.0070.00017.50
8.1.10.0000.00817.54
8.1.00.0000.00817.44
8.0.300.0080.00018.78
8.0.290.0000.00916.63
8.0.280.0060.00318.27
8.0.270.0050.00217.21
8.0.260.0000.00817.28
8.0.250.0030.00317.00
8.0.240.0000.00716.96
8.0.230.0030.00316.94
8.0.220.0030.00616.95
8.0.210.0040.00416.98
8.0.200.0070.00016.86
8.0.190.0050.00216.96
8.0.180.0000.00716.98
8.0.170.0000.00716.95
8.0.160.0040.00417.00
8.0.150.0000.00716.86
8.0.140.0030.00616.83
8.0.130.0030.00313.33
8.0.120.0050.00316.86
8.0.110.0040.00416.77
8.0.100.0040.00416.86
8.0.90.0100.00016.80
8.0.80.0080.00816.98
8.0.70.0000.00716.79
8.0.60.0050.00216.73
8.0.50.0000.00716.92
8.0.30.0100.00717.10
8.0.20.0120.00617.40
8.0.10.0070.00017.02
8.0.00.0100.00816.77
7.4.330.0000.00516.79
7.4.320.0000.00716.41
7.4.300.0000.00816.61
7.4.290.0070.00016.45
7.4.280.0040.00416.51
7.4.270.0030.00316.57
7.4.260.0040.00416.57
7.4.250.0020.00516.55
7.4.240.0030.00316.39
7.4.230.0000.00716.56
7.4.220.0180.00016.57
7.4.210.0080.00616.65
7.4.200.0030.00316.61
7.4.160.0140.00316.44
7.4.150.0080.00917.40
7.4.140.0100.01017.86
7.4.130.0100.00816.50
7.4.120.0090.01216.69
7.4.110.0100.00716.41
7.4.100.0090.01016.45
7.4.90.0060.01216.56
7.4.80.0080.00819.39
7.4.70.0100.00716.46
7.4.60.0090.00916.43
7.4.50.0080.00416.50
7.4.40.0060.00916.61
7.4.30.0060.01216.48
7.4.00.0050.01214.91
7.3.330.0050.00013.03
7.3.320.0020.00213.04
7.3.310.0030.00516.42
7.3.300.0030.00316.19
7.3.290.0050.01016.30
7.3.280.0090.00716.30
7.3.270.0110.00717.40
7.3.260.0140.00616.34
7.3.250.0100.00916.27
7.3.240.0130.00616.28
7.3.230.0120.00616.46
7.3.210.0110.00716.56
7.3.200.0100.00616.28
7.3.190.0060.01116.54
7.3.180.0130.00316.30
7.3.170.0100.00716.32
7.3.160.0080.00816.43
7.3.120.0150.00314.96
7.3.110.0060.01215.05
7.3.100.0000.01514.79
7.3.90.0030.00714.93
7.3.80.0090.00014.97
7.3.70.0030.00714.65
7.3.60.0110.00314.50
7.3.50.0040.00414.59
7.3.40.0130.00014.76
7.3.30.0060.01214.78
7.3.20.0040.00816.79
7.3.10.0000.01616.59
7.3.00.0030.00716.59
7.2.330.0100.00716.25
7.2.320.0100.00616.60
7.2.310.0130.00616.49
7.2.300.0080.00816.64
7.2.290.0060.00916.33
7.2.250.0070.01114.94
7.2.240.0070.01015.17
7.2.230.0070.00714.71
7.2.220.0060.00314.90
7.2.210.0090.00314.74
7.2.200.0000.01415.02
7.2.190.0060.00914.68
7.2.180.0070.01115.05
7.2.170.0030.01314.79
7.2.60.0060.00616.59
7.2.00.0090.00618.95
7.1.330.0030.00915.45
7.1.320.0060.00615.34
7.1.310.0000.01215.36
7.1.300.0030.00615.67
7.1.290.0050.00515.63
7.1.280.0030.00915.71
7.1.270.0040.01115.36
7.1.260.0000.00815.64
7.1.200.0060.00015.40
7.1.100.0040.00717.91
7.1.70.0000.00716.90
7.1.60.0140.01019.40
7.1.50.0100.01016.81
7.1.00.0030.07722.51
7.0.200.0040.00416.52
7.0.60.0270.05721.66
7.0.50.0070.08017.73
7.0.40.0100.07320.01
7.0.30.0500.07020.18
7.0.20.0330.05020.04
7.0.10.0070.04020.33
7.0.00.0030.04320.20
5.6.280.0030.03321.13
5.6.210.0030.07020.51
5.6.200.0070.05318.22
5.6.190.0030.09320.49
5.6.180.0370.04020.68
5.6.170.0170.04320.71
5.6.160.0100.04720.49
5.6.150.0070.04018.22
5.6.140.0100.08018.16
5.6.130.0030.08718.19
5.6.120.0130.05321.18
5.6.110.0270.05721.03
5.6.100.0030.08021.17
5.6.90.0070.06320.96
5.6.80.0100.06020.54
5.5.350.0200.07720.39
5.5.340.0030.03717.99
5.5.330.0070.04020.38
5.5.320.0530.05020.35
5.5.310.0300.06020.33
5.5.300.0070.04017.98
5.5.290.0130.03717.92
5.5.280.0100.03720.88
5.5.270.0130.06020.71
5.5.260.0070.04720.88
5.5.250.0100.03720.58
5.5.240.0200.08720.20

preferences:
60.21 ms | 400 KiB | 5 Q