使用RSA解密问题
我有以下步骤来执行解密
- base64 解码响应
- 使用 RSA1024 公钥解密前 128 个字节。密钥采用 Base64 编码的 X509 格式,并带有 PKCS1 填充。
我的代码如下所示:
$decodedString = $this->base64UrlDecode($string); //does proper url decoding
$publicKey = file_get_contents("public.key",true);
$pub_key = openssl_get_publickey($publicKey);
openssl_public_decrypt($decodedString,$decrypted,$pub_key,OPENSSL_PKCS1_PADDING);
var_dump($decrypted);
我无法在 $decrypted 变量中获取任何内容。如果我在使用公钥之前尝试对其进行 Base64 解码,则会收到“不是有效公钥”的错误。为了实现上述两个步骤,我遗漏了什么或做错了什么?
I have following steps to perform for decryption
- base64 decode the response
- Decrypt the first 128 bytes with the RSA1024 public key. Key is in base64 encoded X509 format with PKCS1 padding.
My code looks like this:
$decodedString = $this->base64UrlDecode($string); //does proper url decoding
$publicKey = file_get_contents("public.key",true);
$pub_key = openssl_get_publickey($publicKey);
openssl_public_decrypt($decodedString,$decrypted,$pub_key,OPENSSL_PKCS1_PADDING);
var_dump($decrypted);
I am not able to get anything in $decrypted variable. If I try to base64 decode public key before using it, I am getting error of not a valid public key. What I am missing or doing wrong to achieve mentioned 2 steps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅
openssl_pkey_get_public
的评论:http://www.php.net/manual/en/function.openssl-pkey-get-public.php#101513
PKCS1 填充构成了看来该功能有问题。
See this comment for
openssl_pkey_get_public
:http://www.php.net/manual/en/function.openssl-pkey-get-public.php#101513
PKCS1 padding poses a problem to that function, it seems.
这实际上是我如何获得回应的问题。通过在 Base64 解码之前进行 urldecode,我能够得到正确的结果。
It was actually a problem with how I was getting response. By doing urldecode before base64 decoding I am able to get proper results.