3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // Data to be sent $plaintext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eleifend vestibulum nunc sit amet mattis. Nulla at volutpat nulla. Pellentesque sodales vel ligula quis consequat. Suspendisse dapibus dolor nec viverra venenatis. Pellentesque blandit vehicula eleifend. Duis eget fermentum velit. Vivamus varius ut dui vel malesuada. Ut adipiscing est non magna posuere ullamcorper. Proin pretium nibh nec elementum tincidunt. Vestibulum leo urna, porttitor et aliquet id, ornare at nibh. Maecenas placerat justo nunc, varius condimentum diam fringilla sed. Donec auctor tellus vitae justo venenatis, sit amet vulputate felis accumsan. Aenean aliquet bibendum magna, ac adipiscing orci venenatis vitae.'; echo 'Plain text: ' . $plaintext; // Compress the data to be sent $plaintext = gzcompress($plaintext); // Get the public Key of the recipient //$publicKey = openssl_pkey_get_public('file:///path/to/public.key'); $config = array( "digest_alg" => "sha1", "private_key_bits" => 1024, "private_key_type" => OPENSSL_KEYTYPE_RSA, ); // Create the private and public key $res = openssl_pkey_new($config); openssl_pkey_export($res, $privateKey); $publicKey=openssl_pkey_get_details($res); $a_key = openssl_pkey_get_details($publicKey); // Encrypt the data in small chunks and then combine and send it. $chunkSize = ceil($a_key['bits'] / 8) - 11; $output = ''; while ($plaintext) { $chunk = substr($plaintext, 0, $chunkSize); $plaintext = substr($plaintext, $chunkSize); $encrypted = ''; if (!openssl_public_encrypt($chunk, $encrypted, $publicKey)) { die('Failed to encrypt data'); } $output .= $encrypted; } openssl_free_key($publicKey); // This is the final encrypted data to be sent to the recipient $encrypted = $output;
based on PvJ46
Output for 5.4.15 - 5.6.30, 7.0.0 - 7.2.0
Plain text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eleifend vestibulum nunc sit amet mattis. Nulla at volutpat nulla. Pellentesque sodales vel ligula quis consequat. Suspendisse dapibus dolor nec viverra venenatis. Pellentesque blandit vehicula eleifend. Duis eget fermentum velit. Vivamus varius ut dui vel malesuada. Ut adipiscing est non magna posuere ullamcorper. Proin pretium nibh nec elementum tincidunt. Vestibulum leo urna, porttitor et aliquet id, ornare at nibh. Maecenas placerat justo nunc, varius condimentum diam fringilla sed. Donec auctor tellus vitae justo venenatis, sit amet vulputate felis accumsan. Aenean aliquet bibendum magna, ac adipiscing orci venenatis vitae. Warning: openssl_pkey_export(): cannot get key from parameter 1 in /in/0mfrS on line 20 Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in /in/0mfrS on line 21 Warning: openssl_pkey_get_details() expects parameter 1 to be resource, null given in /in/0mfrS on line 23 Warning: openssl_public_encrypt(): key parameter is not a valid public key in /in/0mfrS on line 34 Failed to encrypt data
Output for hhvm-3.12.14 - 3.17.3
Plain text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eleifend vestibulum nunc sit amet mattis. Nulla at volutpat nulla. Pellentesque sodales vel ligula quis consequat. Suspendisse dapibus dolor nec viverra venenatis. Pellentesque blandit vehicula eleifend. Duis eget fermentum velit. Vivamus varius ut dui vel malesuada. Ut adipiscing est non magna posuere ullamcorper. Proin pretium nibh nec elementum tincidunt. Vestibulum leo urna, porttitor et aliquet id, ornare at nibh. Maecenas placerat justo nunc, varius condimentum diam fringilla sed. Donec auctor tellus vitae justo venenatis, sit amet vulputate felis accumsan. Aenean aliquet bibendum magna, ac adipiscing orci venenatis vitae. Warning: cannot get key from parameter 1 in /in/0mfrS on line 20 Warning: openssl_pkey_get_details() expects parameter 1 to be resource, null given in /in/0mfrS on line 21 Warning: openssl_pkey_get_details() expects parameter 1 to be resource, null given in /in/0mfrS on line 23 Warning: key parameter is not a valid public key in /in/0mfrS on line 34 Failed to encrypt data
Output for hhvm-3.10.1 - 3.11.1

Process exited with code 137.
Output for 5.4.0 - 5.4.14
Plain text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eleifend vestibulum nunc sit amet mattis. Nulla at volutpat nulla. Pellentesque sodales vel ligula quis consequat. Suspendisse dapibus dolor nec viverra venenatis. Pellentesque blandit vehicula eleifend. Duis eget fermentum velit. Vivamus varius ut dui vel malesuada. Ut adipiscing est non magna posuere ullamcorper. Proin pretium nibh nec elementum tincidunt. Vestibulum leo urna, porttitor et aliquet id, ornare at nibh. Maecenas placerat justo nunc, varius condimentum diam fringilla sed. Donec auctor tellus vitae justo venenatis, sit amet vulputate felis accumsan. Aenean aliquet bibendum magna, ac adipiscing orci venenatis vitae. Notice: Use of undefined constant OPENSSL_KEYTYPE_RSA - assumed 'OPENSSL_KEYTYPE_RSA' in /in/0mfrS on line 15 Fatal error: Call to undefined function openssl_pkey_new() in /in/0mfrS on line 19
Process exited with code 255.