$config = [
    'digest_alg' => 'SHA256',
    "private_key_bits" => 1024,
    "private_key_type" => OPENSSL_KEYTYPE_RSA
];

$privateKeyResource = openssl_pkey_new($config);
openssl_pkey_export($privateKeyResource, $privateKey);
$publicKey = openssl_pkey_get_details($privateKeyResource)['key'];

openssl_private_encrypt($data, $crypted, $privateKey, OPENSSL_PKCS1_PADDING);

加密失败的原因是:

  我设置的private_key_bits是1024bit, 转换成字节解释128。而我选择的填充方式是OPENSSL_PKCS1_PADDING,它需要11个字节。因此data最大长度只有117。但是我的要加密的data有123个字节。因此return false。

解决方案:

  (1)将private_key_bits改成2048

    (2) 根据private_key_bits和填充方式,分段加密

 

参考文档:https://blog.csdn.net/leedaning/article/details/51780511

相关文章:

  • 2021-11-26
  • 2021-12-25
  • 2022-01-15
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-15
  • 2021-06-13
  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-06-08
相关资源
相似解决方案