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($publicKey['bits'] / 8) - 11; $output = ''; while ($plaintext) { $chunk = substr($plaintext, 0, $chunkSize); $plaintext = substr($plaintext, $chunkSize); $encrypted = ''; if (!openssl_public_encrypt($chunk, $encrypted, $res)) { die('Failed to encrypt data'); } $output .= $encrypted; } openssl_free_key($res); // This is the final encrypted data to be sent to the recipient $encrypted = $output;
based on 4ckIG
Output for 7.0.20, 7.1.5 - 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/XboAj on line 20 Warning: openssl_pkey_get_details() expects parameter 1 to be resource, boolean given in /in/XboAj on line 21 Warning: openssl_public_encrypt(): key parameter is not a valid public key in /in/XboAj on line 34 Failed to encrypt data
Output for 5.4.15 - 7.0.6, 7.1.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_public_encrypt(): Don't know how to get public key from this private key in /in/XboAj on line 34 Warning: openssl_public_encrypt(): key parameter is not a valid public key in /in/XboAj on line 34 Failed to encrypt data
Output for 5.3.18 - 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/XboAj on line 15 Fatal error: Call to undefined function openssl_pkey_new() in /in/XboAj on line 19
Process exited with code 255.