Skip to content

Instantly share code, notes, and snippets.

@alangrainger
Created October 30, 2017 06:20
Show Gist options
  • Save alangrainger/a5dab7cc5ef32a8c2d3416aaa0922f5d to your computer and use it in GitHub Desktop.
Save alangrainger/a5dab7cc5ef32a8c2d3416aaa0922f5d to your computer and use it in GitHub Desktop.
PHP hosted encrypted text drop box
<?php
/*
Command lines to generate the keys:
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in private_key.pem -out public_key.pem
*/
if (isset($_POST['data'])) {
$emailData = preg_replace('/\s+/', '', $_POST['data']);
$encryptedData = unserialize($emailData);
$sealed = base64_decode($encryptedData['encdata']);
$ekey = base64_decode($encryptedData['enckey']);
openssl_open($sealed, $openData, $ekey, openssl_get_privatekey($_POST['privatekey']));
?>
<html>
<body>
<pre><?php echo htmlspecialchars($openData); ?></pre>
</body>
</html>
<?php
// End of display section
}
else {
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<p>Encrypted data:</p>
<textarea name="data" rows="10" cols="80"></textarea><br><br>
<p>Private key:</p>
<textarea name="privatekey" rows="10" cols="80"></textarea><br><br>
<input type="submit" />
</form>
</body>
</html>
<?php
}
?>
<html>
<head>
<style>
html, body {
margin: 0;
padding: 0;
}
input, textarea {
font-family: monospace;
font-size:16px;
}
input[type=submit] {
font-size:20px;
}
textarea {
resize: none;
width: 100%;
}
p {
font-family: Helvetica, sans-serif;
color: #444444;
font-size: 20px;
margin-bottom: 6px;
margin-top: 1.2em;
}
</style>
</head>
<body>
<?php
if (isset($_POST['data'])) {
$toAddress = 'email@example.com';
$publicKey = <<<EOT
-----BEGIN PUBLIC KEY-----
public key goes here
-----END PUBLIC KEY-----
EOT;
$sealed ="";
$ekeys="";
$pubKey[] = openssl_pkey_get_public($publicKey);
$result = openssl_seal($_POST['data'], $sealed, $ekeys, $pubKey);
$encryptedData = serialize( array('encdata' => base64_encode($sealed) , 'enckey' => base64_encode($ekeys[0])) );
$mailResult = mail($toAddress, 'Encrypted data from '.$_POST['sender'], $encryptedData);
if ($mailResult) {
echo "<p>Your message has been successfully sent.</p>";
}
else {
echo "<p>There has been an error in the mail system. Your data has been successfully erased.</p>";
}
exit;
}
else {
?>
<form method="post" enctype="multipart/form-data">
<p>Your name:</p>
<input type="text" name="sender" /><br>
<p>Your message:</p>
<textarea name="data" rows="12"></textarea><br><br>
<input type="submit" value="Securely send this data" />
</form>
<?php
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment