dongwen1909 2015-10-12 23:11 采纳率: 0%
浏览 203
已采纳

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

I am trying to encrypt a password to sent to through an API for authentication. From the API I can get the public key in this form:

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

I need to encrypt a user password and send it back to the 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:

$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?

  • 写回答

3条回答 默认 最新

  • doushao1948 2015-10-13 20:06
    关注

    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.

    echo bin2hex($ciphertext);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题