openssl_public_encrypt工作,但openssl_public_decrypt失败了

时间:2018-02-08 05:01:30

标签: php rsa public-key

我收到了一个公钥和一条消息,

    $msg = "blablabla";
    $public = '-----BEGIN PUBLIC KEY-----
     (some normal public key)
    -----END PUBLIC KEY-----';

RsaEncode可以加密

我做RSA编码消息

function RsaEncode($msg,$publicKey){
  $temp = str_split($msg,117);
  $payload = array();
  foreach ($temp as $key => $value) {
    openssl_public_encrypt($value, $encrypted, $publicKey);
    array_push($payload, $encrypted);
  }
  $payload =implode($payload);
   $payload= base64_encode($payload);
  return $payload;
}

此函数返回正常,我成功编码消息, 在我发送结果后,我得到了一个响应$ response,用私钥加密。

所以我用公钥解码它:

function RsaDecode($response,$publicKey){
 $payload = array();
 $payload2 = base64_decode($response);
 $temp = str_split($payload2,125);
 foreach ($temp as $key => $value) {
   openssl_public_decrypt($value, $encrypted, $publicKey);
   array_push($payload, $encrypted);
 }
 $payload =implode($payload);
 $result = utf8_encode($payload);
 return $result;
}

$ publicKey与$ publicKey相同,

声明:

openssl_public_decrypt($value, $encrypted, $publicKey);

总是返回false。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我改变后解决了这个问题

$temp = str_split($payload2,125);

$temp = str_split($payload2,128);

尺寸不能太小