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); $decrypted = UnsafeCrypto::decrypt($encrypted, $key); echo $encrypted; echo ' '; echo $decrypted;

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.0120.00616.75
8.3.50.0040.01116.30
8.3.40.0060.01018.84
8.3.30.0150.00018.75
8.3.20.0080.00021.92
8.3.10.0000.00823.61
8.3.00.0060.00319.56
8.2.180.0140.00718.29
8.2.170.0080.00822.96
8.2.160.0030.01319.08
8.2.150.0000.00824.18
8.2.140.0080.00024.66
8.2.130.0040.00419.26
8.2.120.0040.00426.35
8.2.110.0060.01321.00
8.2.100.0110.00017.91
8.2.90.0000.00818.09
8.2.80.0050.00317.97
8.2.70.0030.00617.50
8.2.60.0000.00817.90
8.2.50.0050.00318.10
8.2.40.0060.00320.42
8.2.30.0040.00419.22
8.2.20.0000.00718.01
8.2.10.0000.00718.00
8.2.00.0040.00418.04
8.1.280.0070.01025.92
8.1.270.0050.00323.91
8.1.260.0070.00026.35
8.1.250.0040.00428.09
8.1.240.0060.00322.52
8.1.230.0040.00721.02
8.1.220.0040.00417.79
8.1.210.0000.00818.77
8.1.200.0030.00617.36
8.1.190.0040.00417.34
8.1.180.0030.00618.10
8.1.170.0080.00018.64
8.1.160.0040.00419.16
8.1.150.0000.00718.95
8.1.140.0030.00519.56
8.1.130.0000.00718.88
8.1.120.0000.00817.44
8.1.110.0060.00317.39
8.1.100.0000.00717.47
8.1.90.0000.00717.36
8.1.80.0030.00517.34
8.1.70.0000.00717.44
8.1.60.0080.00017.56
8.1.50.0030.00617.54
8.1.40.0080.00017.52
8.1.30.0000.00917.77
8.1.20.0040.00417.73
8.1.10.0030.00517.63
8.1.00.0000.00817.43
8.0.300.0030.00320.16
8.0.290.0060.00316.75
8.0.280.0030.00318.44
8.0.270.0030.00317.32
8.0.260.0000.00716.87
8.0.250.0000.00716.89
8.0.240.0050.00316.91
8.0.230.0030.00317.00
8.0.220.0030.00316.82
8.0.210.0030.00316.96
8.0.200.0030.00316.90
8.0.190.0000.00816.87
8.0.180.0000.00717.00
8.0.170.0030.00616.91
8.0.160.0040.00416.92
8.0.150.0000.00716.96
8.0.140.0070.00016.92
8.0.130.0000.00513.34
8.0.120.0070.00016.84
8.0.110.0040.00416.73
8.0.100.0000.00716.97
8.0.90.0080.00016.84
8.0.80.0070.01016.84
8.0.70.0050.00216.84
8.0.60.0040.00416.89
8.0.50.0020.00516.86
8.0.30.0110.00717.07
8.0.20.0110.00817.40
8.0.10.0000.00816.90
8.0.00.0080.00816.74
7.4.330.0000.00515.55
7.4.320.0000.00616.48
7.4.300.0030.00316.71
7.4.290.0030.00316.73
7.4.280.0000.00716.68
7.4.270.0070.00016.52
7.4.260.0050.00516.56
7.4.250.0060.00316.59
7.4.240.0040.00416.66
7.4.230.0020.00516.48
7.4.220.0100.00716.60
7.4.210.0090.00616.59
7.4.200.0030.00316.57
7.4.160.0080.00916.66
7.4.150.0140.00417.40
7.4.140.0110.00917.86
7.4.130.0100.01016.56
7.4.120.0140.00816.54
7.3.330.0000.00613.28
7.3.320.0000.00513.27
7.3.310.0070.00016.31
7.3.300.0030.00316.42
7.3.290.0040.00416.43
7.3.280.0090.00716.41
7.3.270.0140.00417.40
7.3.260.0030.01316.39
7.3.250.0050.01316.50
7.3.240.0030.01416.47
7.1.70.0330.01315.61

preferences:
59.47 ms | 400 KiB | 5 Q