JS uses the public key to encrypt the transmission of information, PHP uses the private key decryption to obtain information
JS RSA Related information:
JS uses 16-in-place public key to encrypt, generate 16 to encrypt data, hex_string
http://www-cs-students.stanford.edu/~tjw/jsbn/
Note: JS needs to do URL encoding of the data first, such as encodeURIComponent (text) and then transfer data to PHP
PHP RSA related information (requires OpenSSL extension):
PHP needs to be converted to 2 binary data pack before decryption ("h*", $hex _string)
http://cn.php.net/manual/zh/book.openssl.php
http://cn.php.net/manual/zh/function.openssl-private-decrypt.php
Key generation
Generate private key
OpenSSL genrsa-out Key.pem 1024
DES3 Way to encrypt private keys
OpenSSL rsa-in key.pem-des3-out Prikey.des3.pem
Generate Public key
OpenSSL rsa-in key.pem-pubout-out Pubkey.pem
Generate 16 binary keys
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:CC3D43A99398674AC09F0FF240833BBA9C1778EF54B98620CA6441915513F0BA264CED8870089 8ba5316e85dbe61780770e10c4b5b2f19b2356a1e3941b168a7aaeae48f48a9f4bc3d7bb5281cf590993b77ab87327ab3f102f96cacc6098909a2745e afb7198b3a0ff075228eb3c5e4cdb6b7a085fe5f2307988e7fe852d197 (hexadecimal public key)
139:d=1 hl=2 l= 3 prim:integer:010001
144:d=1 hl=3 l= prim:integer:52dfd531dd3b2d46aeec9c4adf94a0caf305af43e62c35d9de665a18bfde0c836c8130ad795 073b0bf807f1f72de181764dd5ce0c30a54b1f46cb000e9c034478342cb00edbecccbc1ed63c29046cb5278aaea0b0ba2976d37e04978cd65e856d6fe F644d2bd9ab008da83934f8c227191cc6c8e4e9a1189e23f14a64de5fc79 (private key)
275:d=1 hl=2 l= prim:INTEGER:E8310AA5191EF2561C3BCA025E880B87B7A351A15C80F659C6F2C57017F7EE559038E174813 Bba504d616a1502d2a5213157458dab1bff00fa1b929cd45395fd
342:d=1 hl=2 l= prim:INTEGER:E12E7C078048AAD4805E5ED71D0DC4977D051083C0EA82BEC71150F181D3E67EFA3259DB0A6 86cde0ef829548a95cccb4f4d695d3a82e0dc3baad61e41d39023
409:d=1 hl=2 l= PRIM:INTEGER:1C98458E558CA91D8FB691473F6B13B870162DEC685EFD77CAF784F72C0C7D8E8E0763449CD 1c53d347a65bd16aee8653115655c70112f936839d3117b589be5
475:d=1 hl=2 l= prim:integer:3f441d2aca5b204bf6090da268924eb2b538700c646e3eefb4e4639687006f192bd86bb083f b14e34ad7645d5156872474126ed0f7b87fcec0a0dde9c05add7b
541:d=1 hl=2 l= prim:INTEGER:E11373ABA580936F0F3954E10E1ED8BCD85ABA6B7A343D82E1210FFF4B9440BD0AD3AE1FF35 b90a39190249a28ba478181a179c5dcd6b194c267a11b7c1a0335
Source:
js:function Rsa_encrypt (text) {
var rsa_pubkey= " Bd325ce52fc6ba090ac0c7a2039236587f99c30fa518f601f2ad33019514ee5a4340a964853e1bdf5374ab4ac22f5cff3288e5db94e6752b4999972df 4e23dacacae4e4dcfb6cbae256f1b19c4ba892d54c7a3e068f93ab47ec50635556fc223f02cb1f520631e2f03e5509b6c1e24dfb7962bcd6dc74159bf 0e5afc03d9a00d ";
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
/** Encryption Helper */
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 Decryption
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 decryption
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 encryption
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} are 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;
}
}
RSA Encryption for PHP JS