在本地php7.0环境读取秘钥的时候正常,但在服务器php5.3的环境中却出现错误,查原因是openssl_get_publickey在读取秘钥是返回了false.
原代码:

class RsaCrypt
{
    // RSA私钥
    const PRIVATE_KEY = ***/xwjpGSffTRj5C1WDGUxEaunwLdPJ1xhANyjo/E2oBsrrtPUAxl73jOdVeTwmrvGbNZ16YVM/O7btsIJYXtT0nn7uBs+f/yvmkwzyo3QGa1XL9SaxIWx4FG8LrHgM+2VQIDAQABAoGAAYTqvNfB10eEh6d3GeTxa1+mMkPFEOlB+XGx6MrjJMEJNGnh4/KhysvEV3lNyerrMyATbjGcVA+F0tHxeJG1cI8silri14G/Q0iJHBd7KlxWgdN70qGThrpDc7hPEI8O+9tMWHJIo8QCuykwVa9x+2IT+ksNLwJ9qRh0hBL/PjUCQQDvr3NDkqBZSwXcKkXl9MYp2ANsprN1pr4SNMXgfvst0WoX3DnszgiXuibZ82H8g5qWhi767i8ZYNfYrBBTCt2PAkEA4ccWhHzRxIDtH8HjjYK/ODelxwtG5M4u79jQzZGd0UcTswivuFu9OoYuauO+A2N4OldfwN3Bz3X9+l/03JyD2wJBAOpPJywAs/Z2q+TeAnTZw7r2K+rUUA2MkrTzmBltqRjSZpeqt4mypJ3G4eIg1kJnWQEbbePqYmq5zGhJVLAJqCsCQB/LVntvzGZ8tWfMeQv7howXIgxGpUQhWPDDupvITs4i0T5cEcrHeH1eWfkJez+k6VBl74EMeXeKqSpyCtxqWhMCQQCbIc+8qIMHJ/***';

    /**
     *RSA私钥进行签名
     *$data:签名前字符串
     */
    public static function sign($data)
    {
        $key = "-----BEGIN RSA PRIVATE KEY-----\r\n".$start_key."\r\n-----END RSA PRIVATE KEY-----";
        $private_key = openssl_get_privatekey($key);
        openssl_sign($data,$sign,$private_key);
        openssl_free_key($private_key);
        $rsaSign = base64_encode($sign); // 加密后数据转换为base64
        return $rsaSign;
    }
}

尝试了很多方法,最后用wordwrap把秘钥字符串拆行,然后问题就解决了
更改后:

public static function rsaSign($data)
{
    $private_content = wordwrap(self::PRIVATE_KEY, 64, "\n", true);
    
    $key = "-----BEGIN RSA PRIVATE KEY-----\r\n".$private_content."\r\n-----END RSA PRIVATE KEY-----";
    $private_key = openssl_get_privatekey($key);
    openssl_sign($data,$sign,$private_key);
    openssl_free_key($private_key);
    $rsaSign = base64_encode($sign); // 加密后数据转换为base64
    return $rsaSign;
}


人偶_阿诺
141 声望9 粉丝

个人简介什么都没有