最近想用 PHP 模拟登陆一个网站练手,但在Javascript
中发现了如下RSA 公钥加密
的function
function userLogin() {
var password = $("#password").val();
if(password.length != 256){
setMaxDigits(131);
var key = new RSAKeyPair("010001", '', "00b15fdee0d7ed06c21067d59e65031becca4c3eafe52d891725c75c37dac7ca2d");
var enpwd = encodeURIComponent(password);
var result = encryptedString(key, password);
$("#password").val(result);
}
Google 了一下,都用到了 OpenSSL,我试着用 PHP 写了
function rsa_encode($content)
{
$public_key = "-----BEGIN PUBLIC KEY-----\n00b15fdee0d7ed06c21067d59e65031becca4c3eafe52d891725c75c37dac7ca2d\n-----END PUBLIC KEY-----";
$openssl_public_encrypt = openssl_public_encrypt($str, $content, $public_key);
if (!$openssl_public_encrypt)
throw new \Exception('openssl_public_encrypt failed! ');
return $content;
}
提示“RSA 公钥不正确”,后来又去搜了一下,这样合成的 public_key 不符合 PKCS 标准,不知道下一步应该如何操作,感谢!
1
areless 2020-02-27 19:00:34 +08:00 via Android
先得用公钥创建资源句柄~ openssl_pkey_get_public
|
2
sjlleo OP @areless
感谢,我刚刚尝试这么做了 ```PHP function rsa_encode($data) { $public_key = "-----BEGIN PUBLIC KEY-----\n00b15fdee0d7ed06c21067d59e65031becca4c3eafe52d891725c75c37dac7ca2d\n-----END PUBLIC KEY-----"; $key = openssl_pkey_get_public ($rsa_public); $result = str_split ($data , 117); $str = ''; foreach ($result as $v) { openssl_public_encrypt ($v , $crypted , $key); $str .= $crypted; } if (! $str) { throw new \Exception ('Failded RSA Check'); } } ``` 但是它提示 ``` Warning: openssl_public_encrypt(): key parameter is not a valid public key in /demo/rsa.php on line 12 Fatal error: Uncaught Exception: Failded RSA Check in /demo/rsa.php:16 Stack trace: #0 /demo/rsa.php(38): rsa_encode() #1 {main} thrown in /demo/rsa.php on line 16 ``` |
3
RickyC 2020-02-27 19:29:50 +08:00
|
4
areless 2020-02-27 19:32:00 +08:00 via Android 1
@sjlleo 这个是 16 进制转过的公钥,先把公钥还原出来。好像用的是这个 http://www.ohdave.com/rsa/
|
5
RickyC 2020-02-27 19:33:43 +08:00
你这个 公钥放在函数里, 换行的时候, 会不会默认添加了一些空格, 以至于格式不正确?
|
7
sjlleo OP @areless
00b15fdee0d7ed06c21067d59e65031becca4c3eafe52d891725c75c37dac7ca2d 解密后是 skhN61Jj6UYfdPlzh0a9EareSj8viurcHR8CgKxl3coFds1B6+tShOFHvduTcJVMlE38ToF0e1ZoxnmGYhm4uNKyB5sr6vpQsl61/TERMydYmV0PHFYbvLxQCx//OenJWNYyh+H8gT+eQX1+gUTH6CBNUSqdRQKtHhr15ZUQxnQDttmS0Da8fNfBjZ9GGejl12x3zYHstfG3Djz8Z4Iir1FVMEKzMn9GJRhSeTGtVW8u/3NbPXIhM29Z7QVrTQK6f2/H+uF/rpH1bZRn86mzEM/7qwD1+NEN0jm3h+uS7hMmQ/qA5egIebFVe9EsUkXuDwl8CBR2M4utroxz+E6shA==,但是放入后依旧提示公钥不可用诶。 |