为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用 openssl_private_decrypt 解密任何消息

无法使用 openssl_private_decrypt 解密任何消息

PHP
慕的地8271018 2023-06-24 17:51:57
我尝试使用openssl_private_decrypt函数从 php 解密 RSA 加密消息。但该函数始终返回 null。我的功能如下:function (){    $encrypted = 'gu6F7PFj2t+Mchp2Vi8dpiEXXVDkrjrSoOnRlCWeZYjOL6WdjvxULyEG5j9dZPORNMYx5eB9gTdni4NYYCKW3GTGS0zaKGgadChy/KZAUo40sBVmTFN3YOT6FfEr4oXZ9c0DqSRJ/LrZsZvsiHh52LleA9sapOgsm0bsgWzgnnE=';    $privateKey = '-----BEGIN RSA PRIVATE KEY-----MIICWwIBAAKBgQCMeu1UGlPod+yC/cD+qDWRyotkKly7A+gD+twgekiTguZUJPJlZnB2JpL/cU9oO9nOKn6WJ4DGy7qNjuOj7sLvIkgekPjW6RA2LgOgmS9gSKeOb1OIi59QdoyNv6xOblPz7ihsc5g/KsKEVCLeP5RfBXRXEzS0KHbBnQ3zS8PktwIDAQABAoGASZJ7xyc+uKpOCNCDChvoamFsnVn+VMeX9LWmIbBJYFAW9wyeknKTBZ3wMK4US1/FE094GoW30OWT311tZUa/ehtKA0M9ODCk/Sx9eWxq/oR1e6ypJS8zzKG47DD33+PxKGh+IPJXo8qINvxj7yYbjMxydo8xvBufuBgjoaU9r7ECQQDcsOZIuhvhyWkffJFjmyFYPNJD8eJBQZnxk9uIH/yDl5vJ+bf7fNFc3t+Epnjqfgf98W5frLUeYuzTreusJHF5AkEAovS9EowcIeMp3Ce1utSdGZpjAkGdwVwnxhDLnyO2/TuKNpYODOS7/bLS3NCg9Kw6UF1VLltFwjsBcSoyDmMrrwJAQOMOQlHeAsvaSbSxdqzU6AGx/OIwmOk13w6PHH3zG6nG12LB0oKHeBvbckAUCzYVGXgT8LNx6UZCxZY8Vbe9WQJAZbTq7VCVnnDyB36hZmKdSwWZFiduFuk1yeFhtYnNBHDM6xTs4A9DZizefwOX8h4sp0wZhj1hZMD/PWpgo6ox8QJAFYMky0FT8n19mNRN9aGkQ2GDqO+F8jLEnFJMPXJMGyCg85CGeRH9sF69skcNToZMR304Ms/xyfLeG72SksHT7g==-----END RSA PRIVATE KEY-----';    return openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privateKey);}解密内容如下:Salut les amis
查看完整描述

1 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

您可以稍微修改代码来检测错误


function (){


    $encrypted = 'gu6F7PFj2t+Mchp2Vi8dpiEXXVDkrjrSoOnRlCWeZYjOL6WdjvxULyEG5j9dZPORNMYx5eB9gTdni4NYYCKW3GTGS0zaKGgadChy/KZAUo40sBVmTFN3YOT6FfEr4oXZ9c0DqSRJ/LrZsZvsiHh52LleA9sapOgsm0bsgWzgnnE=';

    $privateKey = '-----BEGIN RSA PRIVATE KEY-----

MIICWwIBAAKBgQCMeu1UGlPod+yC/cD+qDWRyotkKly7A+gD+twgekiTguZUJPJl

ZnB2JpL/cU9oO9nOKn6WJ4DGy7qNjuOj7sLvIkgekPjW6RA2LgOgmS9gSKeOb1OI

i59QdoyNv6xOblPz7ihsc5g/KsKEVCLeP5RfBXRXEzS0KHbBnQ3zS8PktwIDAQAB

AoGASZJ7xyc+uKpOCNCDChvoamFsnVn+VMeX9LWmIbBJYFAW9wyeknKTBZ3wMK4U

S1/FE094GoW30OWT311tZUa/ehtKA0M9ODCk/Sx9eWxq/oR1e6ypJS8zzKG47DD3

3+PxKGh+IPJXo8qINvxj7yYbjMxydo8xvBufuBgjoaU9r7ECQQDcsOZIuhvhyWkf

fJFjmyFYPNJD8eJBQZnxk9uIH/yDl5vJ+bf7fNFc3t+Epnjqfgf98W5frLUeYuzT

reusJHF5AkEAovS9EowcIeMp3Ce1utSdGZpjAkGdwVwnxhDLnyO2/TuKNpYODOS7

/bLS3NCg9Kw6UF1VLltFwjsBcSoyDmMrrwJAQOMOQlHeAsvaSbSxdqzU6AGx/OIw

mOk13w6PHH3zG6nG12LB0oKHeBvbckAUCzYVGXgT8LNx6UZCxZY8Vbe9WQJAZbTq

7VCVnnDyB36hZmKdSwWZFiduFuk1yeFhtYnNBHDM6xTs4A9DZizefwOX8h4sp0wZ

hj1hZMD/PWpgo6ox8QJAFYMky0FT8n19mNRN9aGkQ2GDqO+F8jLEnFJMPXJMGyCg

85CGeRH9sF69skcNToZMR304Ms/xyfLeG72SksHT7g==

-----END RSA PRIVATE KEY-----';


    if (!openssl_private_decrypt(base64_decode($encrypted), $decrypted, $privateKey)) {

        throw new Exception(openssl_error_string());

    }

    return $decrypted;

}

我机器上的结果是:


PHP Fatal error:  Uncaught Exception: error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error in /home/foobar/test.php:20

因此,在解码加密字符串时出现“pkcs 解码错误”。并且您的openssl_private_decrypt无法解密您的消息。请检查您对文本进行加密/base64_encode 的方式,看看是否有任何错误。


查看完整回答
反对 回复 2023-06-24
  • 1 回答
  • 0 关注
  • 345 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信