我想你已经找到了解决问题的方法,但是对于那些仍在寻找解决方案的人来说,这里有一个在 Javascript 和 PHP 之间使用 RSA 的小例子(示例):
<?php
$path = 'phpseclib';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
include_once('Crypt/RSA.php');
$rsa = new Crypt_RSA();
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_RAW);
$key = $rsa->createKey(512);
$e = new Math_BigInteger($key['publickey']['e'], 10);
$e = $e->toHex();
$n = new Math_BigInteger($key['publickey']['n'], 10);
$n = $n->toHex();
function decrypt($msg, $key) {
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->loadKey($key, CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$s = new Math_BigInteger($msg, 16);
return $rsa->decrypt($s->toBytes());
}
?>
<script type="text/javascript" src="javascript/jsbn.js"></script>
<script type="text/javascript" src="javascript/prng4.js"></script>
<script type="text/javascript" src="javascript/rng.js"></script>
<script type="text/javascript" src="javascript/rsa.js"></script>
<script>
<?php
echo "var n='".$n."';";
echo "var e='".$e."';";
?>
function encrypt() {
var rsa = new RSAKey();
rsa.setPublic(n, e);
document.getElementById('enc_text').value = rsa.encrypt(document.getElementById('plaintext').value);
}
</script>
Plain Text:<br/>
<input id='plaintext' name='plaintext' type="text" size="40"/><br/>
<input type="button" onclick="encrypt()" value="Encrypt"/><br/>
Encrypted Text:<br/>
<form action="" method="post">
<input id="enc_text" name='enc_text' type="text" size="40"/><br/>
<?php
echo '<input id="key" name="key" type="hidden" size="40" value="'.urlencode($key['privatekey']).'"/><br/>';
?>
<input name="submit" type="submit" value="Submit" size="10"/>
</form>
<?php
if(isset($_POST['submit']) && ($_POST['enc_text'] != 0)) {
echo decrypt($_POST['enc_text'], urldecode($_POST['key']));
}
?>
如果您需要更多示例,请访问官方网站获取文档:
http: //phpseclib.sourceforge.net/new/rsa/examples.html
或者
http://bestmike007.com/2011/08/secure-data-transmission-between-pure-php-and-javascript-using-rsa/