Skip to content

Instantly share code, notes, and snippets.

@raksa
Last active October 3, 2018 03:17
Show Gist options
  • Save raksa/2f8c79211167d8ac075d2296cd5e2fb9 to your computer and use it in GitHub Desktop.
Save raksa/2f8c79211167d8ac075d2296cd5e2fb9 to your computer and use it in GitHub Desktop.
php <=> js , asymmetric cipher
<?php
require_once 'vendor/autoload.php';
// composer require phpseclib/phpseclib
use phpseclib\Crypt\RSA;
function getFileContent($filePath = '')
{
try {
if (\file_exists($filePath)) {
$file = \fopen($filePath, "r");
$content = '';
while (!\feof($file)) {
$content .= \fgets($file);
}
\fclose($file);
return $content;
}
} catch (\Exception $e) {
\Log::info('Getting file content path: ' . $filePath);
\Log::error($e);
}
return null;
}
function putFileContent($filePath = '', $content = '')
{
try {
return !!\file_put_contents($filePath, $content);
} catch (\Exception $e) {}
return false;
}
$keyPath = __DIR__ . DIRECTORY_SEPARATOR . 'keys' . DIRECTORY_SEPARATOR;
$privateKeyPath = $keyPath . 'asymmetric-cipher.pem';
$publicKeyPath = $keyPath . 'asymmetric-cipher.pub';
if (!($privateKey = getFileContent($privateKeyPath)) || !($publicKey = getFileContent($publicKeyPath))) {
$rsa = new \phpseclib\Crypt\RSA();
$keys = $rsa->createKey();
$privateKey = $keys['privatekey'];
$publicKey = $keys['publickey'];
putFileContent($privateKeyPath, $privateKey);
putFileContent($publicKeyPath, $publicKey);
}
function userPHPEncrypt($plainText)
{
global $publicKey;
\openssl_public_encrypt($plainText, $encryptedText, $publicKey);
return \base64_encode($encryptedText);
}
function userPHPDecrypt($encryptedText)
{
global $privateKey;
$decrypted = null;
\openssl_private_decrypt(\base64_decode($encryptedText), $decrypted, $privateKey);
return $decrypted;
}
$plainText = 'hello world';
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Cipher</title>
<!-- http://travistidwell.com/jsencrypt/ -->
<script src="/jsencrypt-master/bin/jsencrypt.js"></script>
</head>
<body>
<div>
PHP encrypt : <?php echo userPHPEncrypt($plainText); ?>
Test : <?php echo userPHPDecrypt(userPHPEncrypt($plainText)) === $plainText ? 'it work!' : 'something went wrong'; ?>
</div>
<div>
<form method="POST" enctype="multipart/form-data">
<label for="input-private-key">PrivateKey : </label>
<textarea type="text" id="input-private-key" style="width:400px;height:200px;">
<?php echo $privateKey ?>
</textarea>
<br>
<label for="input-public-key">PublicKey : </label>
<textarea type="text" id="input-public-key" style="width:400px;height:200px;"></textarea>
<br>
<label for="input-encrypted">Encrypted : </label>
<textarea type="text" name="encrypted" id="input-encrypted" style="width:400px;height:200px;"></textarea>
<br>
<button type="submit">Submit</button>
</form>
<div>
<?php
if (!empty($_POST) && isset($_POST["encrypted"])) {
echo 'PHP Decode :';
echo userPHPDecrypt($_POST["encrypted"]);
}
?>
</div>
</div>
<script type="text/javascript">
var plainText = '<?php echo $plainText ?>';
var publicKey = `<?php echo $publicKey ?>`;
function userJSEncrypt(text){
var crypt = new JSEncrypt();
crypt.setPublicKey(publicKey);
return crypt.encrypt(text);
}
document.getElementById('input-public-key').value = publicKey;
document.getElementById('input-encrypted').value = userJSEncrypt(plainText);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment