doushi9856
doushi9856
2011-09-19 10:32
浏览 167
已采纳

使用RSA的解密问题

I have following steps to perform for decryption

  1. base64 decode the response
  2. 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?

图片转代码服务由CSDN问答提供 功能建议

我有以下步骤来执行解密

  1. base64解码响应
  2. 使用RSA1024公钥解密前128个字节。 密钥采用base64编码的X509格式,带有PKCS1填充。

    我的代码如下所示:

      $ decodingString = $ this-> base64UrlDecode($ string)  ;  //正确的url解码
     $ publicKey = file_get_contents(“public.key”,true);  
     $ pub_key = openssl_get_publickey($ publicKey);  
    openssl_public_decrypt($ decodingString,$ decrypted,$ pub_key,OPENSSL_PKCS1_PADDING); 
    var_dump($ decrypted); 
       
     
     

    我无法获得$ decrypted变量中的任何内容 。 如果我在使用它之前尝试base64解码公钥,我收到错误的不是有效的公钥。 我错过了什么或做错了才能实现上述两个步骤?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanbipu1720
    duanbipu1720 2011-10-07 09:29
    已采纳

    It was actually a problem with how I was getting response. By doing urldecode before base64 decoding I am able to get proper results.

    $decodedString = $this->base64UrlDecode(urldecode($string));
    
    点赞 评论
  • dqdt45183
    dqdt45183 2011-09-19 11:40

    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.

    点赞 评论

相关推荐