PHP RSA 报错 openssl_sign(): supplied key param cannot be coerced into a private key

PHP RSA 报错
openssl_sign(): supplied key param cannot be coerced into a private key
原因:
RSA 的秘钥一般都有固定换行格式,是不是这种情况导致的呢
按照此格式试试,问题竟然顺利解决

私钥

$str='你的私钥(改成一行)';
$str        = chunk_split($str, 64, "\n");
        $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n";
        $signature = '';
        if (openssl_sign($data, $signature, $key, OPENSSL_ALGO_MD5)) {
            echo base64_encode($signature);
        }

公钥

$data='内容';
$str='你的公钥(改成一行)';
$str               = chunk_split($str, 64, "\n");
        $key = "-----BEGIN PUBLIC KEY-----\n$str-----END PUBLIC KEY-----\n";
        $signature = "";
        if (openssl_verify(base64_decode($data), $signature, $key, OPENSSL_ALGO_MD5) == 1) {
            echo $signature;
        }

案例:
http://php.net/manual/en/function.openssl-sign.php 官方案例 改造了一下
官方的公钥私钥格式太标准,所以要改造一下。
因为我们拿到的公钥私钥有可能就是一行的

$data = "Beeeeer is really good.. hic...";
$private_key = <<

你可能感兴趣的:(php)