简体   繁体   English

使用phpseclib从十六进制编码的公钥进行RSA加密

[英]RSA encryption from hex-encoded public key with phpseclib

I am trying to encrypt a password to sent to through an API for authentication. 我正在尝试加密要通过API进行身份验证的密码。 From the API I can get the public key in this form: 我可以从API获取以下形式的公钥:

 {   "result": {
 "keyId": "L5gslEaP921gEI34N5JRVRIEbbx78WJN",
 "key": {
   "n": "871db29fbb487b988f2d610d83a1e699c59473d73f6d38efa8d21645524b5fb549d5ffbc6d527b261ff2291cf3b3c81f25a4cb13c801d6f6eb3cae624d74724830f1cb45ec803d8836274f1ddea61ec25d5c44ad7ed0b8a56976291bd06abaa0beb9e1ecbbc59662d20f8ae71191e8cbe617acee2349a64aa20846d4d8910e53",
   "e": "10001"
 }   } }

I need to encrypt a user password and send it back to the API. 我需要加密用户密码,然后将其发送回API。 I am using PHP and this is what I have so far, but am not getting a proper hex encrypted password to send back: 我正在使用PHP,这是到目前为止,但是没有获得适当的十六进制加密密码来发送回去:

$modulus = "871db29fbb487b988f2d610d83a1e699c59473d73f6d38efa8d21645524b5fb549d5ffbc6d527b261ff2291cf3b3c81f25a4cb13c801d6f6eb3cae624d74724830f1cb45ec803d8836274f1ddea61ec25d5c44ad7ed0b8a56976291bd06abaa0beb9e1ecbbc59662d20f8ae71191e8cbe617acee2349a64aa20846d4d8910e53";
$exponent = "10001";
$plaintext = "********";

include('Crypt/RSA.php');

$rsa = new Crypt_RSA();

$modulus = new Math_BigInteger(base64_decode($modulus), 256);
$exponent = new Math_BigInteger(base64_decode($exponent), 256);

$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));


$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$ciphertext = $rsa->encrypt($plaintext);

echo $ciphertext;

How do I properly encode the password given the modulus and exponent from the API? 如何根据API的模数和指数正确编码密码?

It would be expected that the ciphertext is binary, not a hex string. 期望密文是二进制的,而不是十六进制的字符串。 A common way to convert it to a hex string is bin2hex() . 将其转换为十六进制字符串的常用方法是bin2hex()

Of course, this depends on what your API is expecting, but bin2hex() would be a typical way to do it. 当然,这取决于您的API期望什么,但是bin2hex()将是一种典型的实现方式。

echo bin2hex($ciphertext);

And there's the reverse function, hex2bin() . 还有反向函数hex2bin()

$modulus = "871db29fbb487b988f2d610d83a1e699c59473d73f6d38efa8d21645524b5fb549d5ffbc6d527b261ff2291cf3b3c81f25a4cb13c801d6f6eb3cae624d74724830f1cb45ec803d8836274f1ddea61ec25d5c44ad7ed0b8a56976291bd06abaa0beb9e1ecbbc59662d20f8ae71191e8cbe617acee2349a64aa20846d4d8910e53";
$exponent = "10001";

That's not base64 encoded. 那不是base64编码的。 That's hex encoded. 那是十六进制编码的。 10001 == 65537. Try to load your key like this: 10001 ==65537。尝试像这样加载密钥:

$modulus = new Math_BigInteger(base64_decode($modulus), 16);
$exponent = new Math_BigInteger(base64_decode($exponent), 16);

eg. 例如。 use 16 instead of 256 as the second parameter to the Math_BigInteger constructor. 使用16而不是256作为Math_BigInteger构造函数的第二个参数。

I figured out the solution. 我想出了解决方案。 It is two parts. 这是两个部分。

Encode the hex to binary correctly using these two lines: 使用以下两行将十六进制正确编码为二进制:

$modulus = new Math_BigInteger($modulus, 16);
$exponent = new Math_BigInteger($exponent, 16);

And out put the result as a bin to hex as Nate stated. 就像Nate所说的那样,将结果作为bin放入十六进制中。

echo bin2hex($ciphertext);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM