$result;
}
//--
// Convert a number back into binary form
//--
function number_to_binary($number, $blocksize)
{
$base = "256";
$result = "";
$div = $number;
while($div > 0)
{
$mod = bcmod($div, $base);
$div = bcdiv($div, $base);
$result = chr($mod) . $result;
}
return str_pad($result, $blocksize, "\x00", STR_PAD_LEFT);
}
?>
处理的PHP代码:
复制代码 代码如下:
<?php
//Decimal Data
include "rsa.php";
$modulus=''124124790696783899579957666732205416556275207289308772677367395397704314099727565633927507139389670490184904760526156031441045563225987129220634807383637837918320623518532877734472159024203477820731033762885040862183213160281165618500092483026873487507336293388981515466164416989192069833140532570993394388051.0000000000'';
$private=''59940207454900542501281722336097731406274284149290386158861762508911700758780200454438527029729836453810395133453343700246367853044479311924174899432036400630350527132581124575735909908195078492323048176864577497230467497768502277772070557874686662727818507841304646138785432507752788647631021854537869399041.0000000000'';
$public="65537";
$keylength="1024";
//php encrypt create
//$encrypted = rsa_encrypt("vzxcvz bdxf", $public, $modulus, $keylength);
//$str= bin2hex($encrypted);//bin data to hex data
$str=
PHP+JS+rsa数据加密传输实现代码_网学
浏览:
POST[''ciphertext''];
//echo $str."<br>";
$encrypted=convert($str); //hex data to bin data
$decrypted = rsa_decrypt($encrypted, $private, $modulus, $keylength);
echo $decrypted."<br>";
/**
* 16 to 2
* @param unknown_type $hexString
* @return string|unknown
*/
function convert($hexString)
{
$hexLenght = strlen($hexString);
// only hex numbers is allowed
if ($hexLenght % 2 != 0 || preg_match("/[^\da-fA-F]/",$hexString)) return FALSE;
unset($binString);
for ($x = 1; $x <= $hexLenght/2; $x++)
{
$binString .= chr(hexdec(substr($hexString,2 * $x - 2,2)));
}
return $binString;
}
?>
生成PRM文件及生产需要的密钥及公钥的PHP文件:
复制代码 代码如下:
<?php
//create pem file
//run openssl genrsa -out key.pem 1024
//This file is generated variables needed for the operation
list($keylength, $modulus, $public, $private,$modulus_js,$private_js) = read_ssl_key("key.pem");
echo "keylength:(php and js)(private length)<br>";
echo $keylength;
echo "<br>";
echo "modulus:(php)(10)(pubic key)<br>";
echo $modulus;
echo "<br>";
echo "modulus:(js)(16)(pubic key)<br>";
echo $modulus_js;
echo "<br>";
echo "public:(php)(10)(public exponent)<br>";
echo $public;
echo "<br>";
echo "public:(js)(16)(public exponent)<br>";
echo "10001";
echo "<br>";
echo "private:(php)(10)(private key)<br>";
echo $private;
echo "<br>";
echo "private:(js)(16)(private key)<br>";
echo $private_js;
//function
function read_ssl_key($filename)
{
exec("openssl rsa -in $filename -text -noout", $raw);
// read the key length
$keylength = (int) expect($raw[0], "Private-Key: (");
// read the modulus
expect($raw, "modulus:");
for($i = 2; $raw[$i][0] == '' ''; $i++) $modul