dongtang7347 2014-02-11 21:43
浏览 86
已采纳

PHPSECLIB Javascript兼容性

i've followed this guide to implement RSA on my website and all work fine. encrypt in RSA in Javascript and decrypt in PHP...but the problem is how to encrypt in PHP and decrypt in PHP with this configuration. i'LL show you my code, i need help. thanks in advance.

THIS IS MY CODE:

JAVASCRIPT

<script type="text/javascript" src="js-rsa/jsbn.js"></script>
<script type="text/javascript" src="js-rsa/prng4.js"></script>
<script type="text/javascript" src="js-rsa/rng.js"></script>
<script src="js-rsa/rsa.js"></script>
function rsajsencrypt(data){
var rsa = new RSAKey();
rsa.setPublic('9cbe7dd9152e4ca561b60e7c3fc632c4760dc64611a08c3f13bd2cb81e2daf9cc0d44ff046941862f23c7244f5dd6faa23ea14441f6d2c439de9ecfc5ea9e22dc0a76137df5c853ba39901959735da47e6a8806544e0e8c43c66714acdc1aa7acb4be93e7329fb9541a42fe212dc1937807e5f5f41ca0dd72d5f24daceaa3c6495b2b3b5bddc26cea7c61e5322727e8b517b2fabe590901fe0eaeabe25461e3ffbe772a62236fb79dea37a99b61739aa197f515a6317e072b5449cb1c8f5ad85cd9b180dcc2884e64ff2f143082c1d7fb252ba5de3b0342d634d54b50ceea07decd760bc60cef2e1b5918a10c21349cca1539acf4cb4e39b5605ab31c8073e1b5bb9a78cf38f42c90f0c119f39d42829a8f25aa9bc13d842a53eda75ddbc7d212bfb0c1fce8c7ea8402554f311e7a67002a4d588653361354685d2b02c73ed708367156a9ce334df63248f5eb062c9116e6cddd302a1bcc76a5dcb2481d35541a96d831e774b224490a29b31b2949bb70cbb573662935f7f8204d479aa351c37f2f5602b71bb92a2db9dd833c1c2418591fad3887644950b5a9cd7d12741f1dadac90518829238bd171196b101beb256a0f0769788f18d6969248c61012f84802f1c5971b9165023ebcbeaed4b6a5e2882728a46625955f9ca7bb841baa599ac02de68a5b910e1dae4947a8ce9a7263e44efc00428858c04dfe9bf26f244b1bb', '010001');
var crypted = rsa.encrypt(data);
return(crypted);
}

and this is how i decrypt with PHP. IT WORKS FINE.

  $pvKey =  file_get_contents("key.txt");
  $rsa = new Crypt_RSA();
  $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
  $rsa->loadKey($pvKey, CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
  $s = new Math_BigInteger($hidden_key, 16);
  $showed_key = $rsa->decrypt($s->toBytes());

so...the following is the PHP FUNCTION to encrypt in RSA and that DON't let me ENCRYPT something that i can decrypt in php. TO USE Javascript to encrypt i have to generate the $newpbkey that encrypts in Js, but don't seems to work in php (the old ones works with php but not with js...help...) i know that they are different type and i think that this is the problem but i don't know how to solve.

 function rsaencryptphp($data){
   require_once("Crypt/RSA.php");
/*$oldpbKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA6XkN3syXRnmj+NX2JMsM fZQh2kkHHaqprQQ8yxYF2GJRMHPFmEWwjhPf385px6eVhQ2xZTmVSzRerxNTNLNg 3FPeC53c27acG5lBBkJkVGiQaPUqvdcSwz/zeVnXJnQeoDPTxGKd4pCHTLfiCNEE pnwaCflsOd3Crp5/bHcKjE97AlTmyyssO8bY16a5DFK+zuFnoid0nit4Q89LYdNa m5yGGfetkc2YWDEwsMcVYnEnHJ4A2gdLPctwY0sngvBguXlwMi4orKVMotsRgKAW Vg5wTlNYGEoWrwXIUIjIVa8Ao9KBRrIJIkOEUXnMcAk23bGIZDJUAdSMWTjFMzQ2 cDN4Qkw6YBOOv6aGCyew47voiHlkyOKXkK+Tnze5cxTHN5pP4ZZOz8RKkkDBL+bt Mpe3vt8pJvzIYynegrCJ8URa1wtc8zY+SiueO32rcHug/WI37ZI1JI/PvHb3HzhK o00stgW2eJyWplglbZ4ds+s+7M/GWdfUc6ytM0qd6B3fyXXD9dsZMwIqwFVduv1q BUoAYaihuEoimubRRrKqCQKpugFO0b7B9O4ukE55JlvpBfIAytIcYm/ID7CVIPYN S2OOf5LhuydorSymwKfTtZPe18RFVfyd19ggclV6jA9BwqOkyfmwD3IYdWwNbEYH rULzEB0U3QLHGwZ85fVUTSECAwEAAQ==";*/
$newpbKey = "9cbe7dd9152e4ca561b60e7c3fc632c4760dc64611a08c3f13bd2cb81e2daf9cc0d44ff046941862f23c7244f5dd6faa23ea14441f6d2c439de9ecfc5ea9e22dc0a76137df5c853ba39901959735da47e6a8806544e0e8c43c66714acdc1aa7acb4be93e7329fb9541a42fe212dc1937807e5f5f41ca0dd72d5f24daceaa3c6495b2b3b5bddc26cea7c61e5322727e8b517b2fabe590901fe0eaeabe25461e3ffbe772a62236fb79dea37a99b61739aa197f515a6317e072b5449cb1c8f5ad85cd9b180dcc2884e64ff2f143082c1d7fb252ba5de3b0342d634d54b50ceea07decd760bc60cef2e1b5918a10c21349cca1539acf4cb4e39b5605ab31c8073e1b5bb9a78cf38f42c90f0c119f39d42829a8f25aa9bc13d842a53eda75ddbc7d212bfb0c1fce8c7ea8402554f311e7a67002a4d588653361354685d2b02c73ed708367156a9ce334df63248f5eb062c9116e6cddd302a1bcc76a5dcb2481d35541a96d831e774b224490a29b31b2949bb70cbb573662935f7f8204d479aa351c37f2f5602b71bb92a2db9dd833c1c2418591fad3887644950b5a9cd7d12741f1dadac90518829238bd171196b101beb256a0f0769788f18d6969248c61012f84802f1c5971b9165023ebcbeaed4b6a5e2882728a46625955f9ca7bb841baa599ac02de68a5b910e1dae4947a8ce9a7263e44efc00428858c04dfe9bf26f244b1bb";
$rsa = new Crypt_RSA();
$rsa->loadKey($pbKey); // public key
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($data);
return $ciphertext;
 }

info: phpseclib is the rsa library that i'm using

  • 写回答

1条回答 默认 最新

  • dsag14654 2014-02-12 14:09
    关注

    I've found the solution on my own, i share it down here. Thanks to everyone have tried help me.

    Joshua Bane

    function rsaencrypt($plaintext){
    require_once("Crypt/RSA.php");
    $pbKey = "082c1d7fb252ba5de3b0342d634d54b50ceea07decd760bc60cef2e1b5918a10c21349cca1539acf4cb4e39b5605ab31c8073e1b5bb9a78cf38f42c90f0c119f39d42829a8f25aa9bc13d842a53eda75ddbc7d212bfb0c1fce8c7ea8402554f311e7a67002a4d588653361354685d2b02c73ed708";
    $rsa = new Crypt_RSA();
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    $public_key = array(
     'n' => new Math_BigInteger($pbKey, 16),
     'e' => new Math_BigInteger('65537', 10) // same value as function that create KEY
               );
    $rsa->loadKey($public_key,CRYPT_RSA_PUBLIC_FORMAT_RAW);
    $ciphertext = $rsa->encrypt($plaintext);
    $ciphertext_hex = bin2hex($ciphertext);
    return $ciphertext_hex;
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?