3v4l.org

run code in 300+ PHP versions simultaneously
<?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;

preferences:
33.52 ms | 402 KiB | 5 Q