使用phpseclib从十六进制编码的公钥进行RSA加密 [英] RSA encryption from hex-encoded public key with phpseclib

查看:281
本文介绍了使用phpseclib从十六进制编码的公钥进行RSA加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加密要通过API发送给身份验证的密码.我可以从API获取以下形式的公钥:

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"
 }   } }

我需要加密用户密码,然后将其发送回API.我正在使用PHP,这是到目前为止,但是没有获得正确的十六进制加密密码发送回去:

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:

<代码> $模量= 871db29fbb487b988f2d610d83a1e699c59473d73f6d38efa8d21645524b5fb549d5ffbc6d527b261ff2291cf3b3c81f25a4cb13c801d6f6eb3cae624d74724830f1cb45ec803d8836274f1ddea61ec25d5c44ad7ed0b8a56976291bd06abaa0beb9e1ecbbc59662d20f8ae71191e8cbe617acee2349a64aa20846d4d8910e53";$ exponent ="10001";$ plaintext ="********";include('Crypt/RSA.php');$ rsa =新的Crypt_RSA();$ modulus =新的Math_BigInteger(base64_decode($ modulus),256);$ exponent =新的Math_BigInteger(base64_decode($ exponent),256);$ rsa-> loadKey(array('n'=> $ modulus,'e'=> $ exponent));$ rsa-> setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);$ ciphertext = $ rsa-> encrypt($ plaintext);回声$密文;

在给定API的模数和指数的情况下,如何正确编码密码?

How do I properly encode the password given the modulus and exponent from the API?

推荐答案

我想出了解决方案.这是两个部分.

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);

然后如Nate所述,将结果作为bin放入十六进制.

And out put the result as a bin to hex as Nate stated.

echo bin2hex($ciphertext);

这篇关于使用phpseclib从十六进制编码的公钥进行RSA加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆