标签:
js使用公钥对信息进行加密传输,php使用私钥解密获取信息
js rsa相关信息:
js使用16进制公钥进行加密,生成16进制加密数据,hex_string
http://www-cs-students.stanford.edu/~tjw/jsbn/
注意:js需要先对数据做url编码处理,如 encodeURIComponent(text) 再将数据传输给php
php rsa相关信息(需要openssl扩展):
php解密前需要转换成2进制数据 pack("H*", $hex_string)
http://cn.php.net/manual/zh/book.openssl.php
http://cn.php.net/manual/zh/function.openssl-private-decrypt.php
密钥生成
生成私钥
openssl genrsa -out key.pem 1024
DES3方式加密私钥
openssl rsa -in key.pem -des3 -out prikey.des3.pem
生成公钥
openssl rsa -in key.pem -pubout -out pubkey.pem
生成16进制密钥
openssl asn1parse -i -inform PEM < key.pem
0:d=0 hl=4 l= 604 cons: SEQUENCE
4:d=1 hl=2 l= 1 prim: INTEGER :00
7:d=1 hl=3 l= 129 prim: INTEGER :CC3D43A99398674AC09F0FF240833BBA9C1778EF54B98620CA6441915513F0BA264CED88700898BA5316E85DBE61780770E10C4B5B2F19B2356A1E3941B168A7AAEAE48F48A9F4BC3D7BB5281CF590993B77AB87327AB3F102F96CACC6098909A2745EAFB7198B3A0FF075228EB3C5E4CDB6B7A085FE5F2307988E7FE852D197(十六进制公钥)
139:d=1 hl=2 l= 3 prim: INTEGER :010001
144:d=1 hl=3 l= 128 prim: INTEGER :52DFD531DD3B2D46AEEC9C4ADF94A0CAF305AF43E62C35D9DE665A18BFDE0C836C8130AD795073B0BF807F1F72DE181764DD5CE0C30A54B1F46CB000E9C034478342CB00EDBECCCBC1ED63C29046CB5278AAEA0B0BA2976D37E04978CD65E856D6FEF644D2BD9AB008DA83934F8C227191CC6C8E4E9A1189E23F14A64DE5FC79(私钥)
275:d=1 hl=2 l= 65 prim: INTEGER :E8310AA5191EF2561C3BCA025E880B87B7A351A15C80F659C6F2C57017F7EE559038E174813BBA504D616A1502D2A5213157458DAB1BFF00FA1B929CD45395FD
342:d=1 hl=2 l= 65 prim: INTEGER :E12E7C078048AAD4805E5ED71D0DC4977D051083C0EA82BEC71150F181D3E67EFA3259DB0A686CDE0EF829548A95CCCB4F4D695D3A82E0DC3BAAD61E41D39023
409:d=1 hl=2 l= 64 prim: INTEGER :1C98458E558CA91D8FB691473F6B13B870162DEC685EFD77CAF784F72C0C7D8E8E0763449CD1C53D347A65BD16AEE8653115655C70112F936839D3117B589BE5
475:d=1 hl=2 l= 64 prim: INTEGER :3F441D2ACA5B204BF6090DA268924EB2B538700C646E3EEFB4E4639687006F192BD86BB083FB14E34AD7645D5156872474126ED0F7B87FCEC0A0DDE9C05ADD7B
541:d=1 hl=2 l= 65 prim: INTEGER :E11373ABA580936F0F3954E10E1ED8BCD85ABA6B7A343D82E1210FFF4B9440BD0AD3AE1FF35B90A39190249A28BA478181A179C5DCD6B194C267A11B7C1A0335
源代码:
js:function rsa_encrypt(text) {
var rsa_pubkey="BD325CE52FC6BA090AC0C7A2039236587F99C30FA518F601F2AD33019514EE5A4340A964853E1BDF5374AB4AC22F5CFF3288E5DB94E6752B4999972DF4E23DACACAE4E4DCFB6CBAE256F1B19C4BA892D54C7A3E068F93AB47EC50635556FC223F02CB1F520631E2F03E5509B6C1E24DFB7962BCD6DC74159BF0E5AFC03D9A00D";
var rsa = new RSAKey();
rsa.setPublic(rsa_pubkey, ‘10001‘);
return rsa.encrypt(text);
}
function rsa_submit() {
var text = document.getElementById(‘text‘);
var en = rsa_encrypt(encodeURIComponent(text.value));
text.value = en;
return true;
}
php:
<?php
/** 加密助手 */
class RsaHelper {
private $_public_key;
private $_private_key;
public function __construct($path=null){
$this->_private_key = $path ? trim($path, ‘/‘) . ‘/private.key‘ : dirname(__DIR__) . ‘/certificate/private.key‘;
$this->_public_key = $path ? trim($path, ‘/‘) . ‘/private.key‘ : dirname(__DIR__) . ‘/certificate/public.key‘;
}
//js解密
public function rsa_decrypt_for_js($text, $padding=OPENSSL_PKCS1_PADDING) {
$private_key_content = file_get_contents($this->_private_key);
$private_key = openssl_pkey_get_private($private_key_content);
if ($private_key == false) {
throw new Exception("The private key {$private_key_content} is not invalid");
}
$pack = pack("H*", $text);
$decrypted = ‘‘;
if (!openssl_private_decrypt($pack, $decrypted, $private_key, $padding)) {
$errmsg = ‘‘;
while ($msg = openssl_error_string()) {
$errmsg .= $msg. "\n";
}
throw new Exception("Js decrypt error: " . $errmsg);
}
if ($padding == OPENSSL_NO_PADDING) {
return rtrim(strrev($decrypted), "/0");
} else {
return urldecode($decrypted);
}
}
//php解密
public function rsa_decryp($text, $padding=OPENSSL_PKCS1_PADDING) {
$private_key_content = file_get_contents($this->_private_key);
$private_key = openssl_pkey_get_private($private_key_content);
if ($private_key == false) {
throw new Exception("The private key {$private_key_content} is not invalid");
}
$decrypted = ‘‘;
if (!openssl_private_decrypt($text, $decrypted, $private_key, $padding)) {
$errmsg = ‘‘;
while ($msg = openssl_error_string()) {
$errmsg .= $msg. "\n";
}
throw new Exception("Decrypt error: " . $errmsg);
}
return $decrypted;
}
//php加密
public function rsa_encrypt($text) {
$public_key_content = file_get_contents($this->_public_key);
$public_key = openssl_pkey_get_public($public_key_content);
if ($public_key == false) {
throw new Exception("The public key {$public_key_content} is not invalid");
}
$crypted = ‘‘;
if (!openssl_public_encrypt($text, $crypted, $public_key, OPENSSL_PKCS1_PADDING)) {
$errmsg = ‘‘;
while ($msg = openssl_error_string()) {
$errmsg .= $msg. "\n";
}
throw new Exception("Encrypt error: " . $errmsg);
}
return $crypted;
}
}
标签:
原文地址:http://www.cnblogs.com/hike2008/p/4517765.html