[英]JavaScript RSA encryption to PHP
I need to authenticate myself via PHP script on remote website, and website uses JS-based RSA encryption for passwords. 我需要通过远程网站上的PHP脚本对自己进行身份验证,并且网站对密码使用基于JS的RSA加密。 Here's the code from website:
这是网站上的代码:
function rsa_encrypt(strPlainText) {
var strModulus = "some_random_string";
var strExponent = "10001";
var rsa = new RSAKey();
rsa.setPublic(strModulus, strExponent);
var res = rsa.encrypt(strPlainText);
if (res) {
return res;
}
return false;
}
Browsed a lot of topics on this website, and found that the recommended way is to use phpseclib (if there's another one, let me know). 浏览了该网站上的很多主题,发现推荐的方法是使用phpseclib(如果还有其他方法,请告诉我)。 However, using basic example from http://phpseclib.sourceforge.net/rsa/examples.html#encrypt,enc2 I get just an empty page.
但是,使用来自http://phpseclib.sourceforge.net/rsa/examples.html#encrypt,enc2的基本示例,我只会得到一个空白页面。 I entered
some_random_string
into $rsa->loadKey('...');
我在
$rsa->loadKey('...');
输入了some_random_string
$rsa->loadKey('...');
- not sure if I did it right? -不知道我做对了吗? However, I can't see a place to enter
strExponent
(which is 10001) in this example. 但是,在此示例中,我看不到输入
strExponent
(即10001)的位置。
So I tried another solution - Encrypt and Decrypt text with RSA in PHP and modified my code to look the following: 因此,我尝试了另一种解决方案- 在PHP中使用RSA加密和解密文本,并修改了代码,使其外观如下:
include('Crypt/RSA.php');
$privatekey = "some_random_string";
$rsa = new Crypt_RSA();
$rsa->loadKey($privatekey);
$plaintext = new Math_BigInteger('10001');
echo $rsa->_exponentiate($plaintext)->toBytes();
However, I get this error: 但是,我收到此错误:
Fatal error: Call to a member function abs() on null in Math\BigInteger.php on line 1675
The solution was posted some time ago, so I guess something got changed in phpseclib library during this time, and I'm just not sure how to re-modify my code. 该解决方案是在一段时间前发布的,因此我猜想这段时间phpseclib库中发生了一些变化,而我不确定如何重新修改我的代码。
Popular formats for RSA keys typically contain both the exponent and the modulus within them. RSA密钥的流行格式通常同时包含指数和模数。 See, for example, my answer to I understand the mathematics of RSA encryption: How are the files in ~/.ssh related to the theory?
例如,请参阅我对RSA加密的数学理解的答案:〜/ .ssh中的文件与理论有何关系? for a more detailed discussion of one particular type of key format.
有关一种特殊类型的密钥格式的详细讨论。
If you have the exponent and modulo as distinct values try doing this: 如果您将指数和模数作为不同的值,请尝试执行以下操作:
$rsa->loadKey([
'e' => new Math_BigInteger('10001', 16),
'n' => new Math_BigInteger('some_random_string', 16);
]);
Note the , 16
bit. 请注意
, 16
位。 65537 (10001 in hex) is a common RSA exponent. 65537 (十六进制的10001)是常见的RSA指数。
Math_BigInteger
assumes, by default, that the number being passed to it is in base-10, unless you specifically tell it otherwise. 默认情况下,
Math_BigInteger
假定要传递给它的数字以10为底,除非您另外特别声明。 One requirement of RSA is that e be coprime to either phi(n) or lcm(n). RSA的一个要求是e是phi(n)或lcm(n)的互素。 65537 is trivially coprime because it is prime.
65537是互质的,因为它是质数。 10001 is not prime.
10001不是素数。 It can be factored into 73*137.
它可以分解为73 * 137。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.