PHP格式化RSA私钥

我们得到的RSA私钥可能是这个样子的

MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUyqf8oRXOGR21xyJ2hEITPcGpcfsAw238V+M4kUpC7PAthF8U/pOaRJWWxNKQhX8DP+UlqJ4mAqSW1TiOSOmYlILphLVcN7CTV1OYmnRICeT0wZekK5So8Cj+PqvPj/SIrgWpZNTEayM5zS2kNz60d8MyvM19aVjJuZswhUfYYiU2S66yiX4DuPySfe3/XI40eZVZkq3sAHJgt136spiphk84HFcC20c//0JMugEduMvAUIxFqYSX+UEvfi9AmIBlbzUA4iLgLEmDXqrcD9nq05wlhdKK8ZqydO+O7LK+q/Fgvk5OseUJP/FDTrzE7NTdTx+MOMhuyVHfvkMGX/83AgMBAAECggEAODKm6jspUjOYTh0NfjgMANp1d/tt82bc9s93KdludAKr4HU7uGFC/KRk35IteZqCU4bhlKywNukOo02bU9pkKFYHcA3ItjDuh2gXUe8cdk0OZgdsr/wp5Pj4crbXC8lyceFQkB9eYWWZ424EjB61l0F6s4KOFo0glOh+nf/pF+e82q31w5oH8j/Vd9Wwy/ul0v4vXChk5Rhkvm1PobyfHatAlWeN/m4JMYKg3DAKkkeLI2m+aTpA4EwidU9TR/vpKCuPPWjk0bTLdYfOClTIJ2XXPttNqGXUEbSqxSlDhwVHbyrvjgZIbLwPj2yM/F/mE2MhB3kqwf/qNMstC4TsGQKBgQDiWwcmH6tMyTjSz5qpsCGiMrHvYkYxwZQCgtPn00kLrMPRsRqOzVX8pLn0fcWmgj6XPAMwY9RSZhOjOL03qw+Y3xi6hpHRDe0R2jhZSvmHGR7ObXQeKwp54LqHQPs0mWSHf8anYtJxFkM5QBUuO3pPtf/KwA/qi7i6uLq00VphcwKBgQCoRyjFPtb/smj/46PKU6w2TxPGAKaQm236yOdW2QFwAWuVFyT+OfeuCntmahPxAqZXXCz9XYIQhoGXClVxlm68ulymAiLfqx26TL5qEguyc5JgzwNFnOqrtY8MUALsLFLwvx0VUHEg1DGIhCuEqTBOCYBpMka7ifMaTfqxpIAqLQKBgEBjCEuN4po6+tDsgOAX29lZdkfTuofYupNkR4bAER0jDaiOKjHIstbkvIiRZjGRBuDaD0Yqf3DY3GkncJtvK0ae31ffg4bl4TOc0T6FFymYuayacddLAvXcOPLURyCVKYVpkUZ308ibPpAAbIX0ITyfVp2DuINNL0Td2y4oBv+FAoGAYorZOzijWrU1e7PotJDvJhaEs8ypvcDnVJbVsiv+BYMvZu1Ywm9btVV6Ee1IpDgzBLF7vxtFETxTIwnwI0Y+rtJLcwOJPIXOMMzELQVISEYZkAtqFRfg7JCzpjgsnfUO1OB1vYeP1/Oq1hDgEx2opeo2gmPou+X6sIq9pc15KDkCgYEAwEF//T/JzSWxRK+bCZM5SxJN4OVAjA4Ryvois0sYjgaiBdz3pg6ul4RRn0rAqYdoS7s/fsCfkQLD+xjAj1BVzZc+Q4XCdpug08EECDv6+C5+FbM1V6QTd/uu63iFfelFb4OhTFGISZOdmhlEajTZbpmmHTEM4ygmXiK+RoOaDN0=
//这是第1种RSA私钥

以上这段RSA是一段连续的字符串, 是没有换行的;

但是在生产活动中, 我们需要的RSA可能是如下格式的

-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUyqf8oRXOGR21
xyJ2hEITPcGpcfsAw238V+M4kUpC7PAthF8U/pOaRJWWxNKQhX8DP+UlqJ4mAqSW
1TiOSOmYlILphLVcN7CTV1OYmnRICeT0wZekK5So8Cj+PqvPj/SIrgWpZNTEayM5
zS2kNz60d8MyvM19aVjJuZswhUfYYiU2S66yiX4DuPySfe3/XI40eZVZkq3sAHJg
t136spiphk84HFcC20c//0JMugEduMvAUIxFqYSX+UEvfi9AmIBlbzUA4iLgLEmD
XqrcD9nq05wlhdKK8ZqydO+O7LK+q/Fgvk5OseUJP/FDTrzE7NTdTx+MOMhuyVHf
vkMGX/83AgMBAAECggEAODKm6jspUjOYTh0NfjgMANp1d/t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aiBdz3pg6ul4RRn0rAqYdoS7s/fsCfkQLD+xjAj1BV
zZc+Q4XCdpug08EECDv6+C5+FbM1V6QTd/uu63iFfelFb4OhTFGISZOdmhlEajTZ
bpmmHTEM4ygmXiK+RoOaDN0=
-----END PRIVATE KEY-----
//这是第2种RSA私钥

这个RSA私钥, 有了文件头和文件尾, 并且过一段时间, 自动换行一下

如何把第1种RSA私钥转化为第2种RSA私钥呢? 示例代码如下

<?php

/**格式化RSA私钥 */
function formatPriKey($key)
{
    $wordWrap = wordwrap(preg_replace('/[\r\n]/', '', $key), 64, "\n", true);
    return "-----BEGIN PRIVATE KEY-----\n" . $wordWrap . "\n-----END PRIVATE KEY-----";
}

$priKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCUyqf8oRXOGR21xyJ2hEITPcGpcfsAw238V+M4kUpC7PAthF8U/pOaRJWWxNKQhX8DP+UlqJ4mAqSW1TiOSOmYlILphLVcN7CTV1OYmnRICeT0wZekK5So8Cj+PqvPj/SIrgWpZNTEayM5zS2kNz60d8MyvM19aVjJuZswhUfYYiU2S66yiX4DuPySfe3/XI40eZVZkq3sAHJgt136spiphk84HFcC20c//0JMugEduMvAUIxFqYSX+UEvfi9AmIBlbzUA4iLgLEmDXqrcD9nq05wlhdKK8ZqydO+O7LK+q/Fgvk5OseUJP/FDTrzE7NTdTx+MOMhuyVHfvkMGX/83AgMBAAECggEAODKm6jspUjOYTh0NfjgMANp1d/tt82bc9s93KdludAKr4HU7uGFC/KRk35IteZqCU4bhlKywNukOo02bU9pkKFYHcA3ItjDuh2gXUe8cdk0OZgdsr/wp5Pj4crbXC8lyceFQkB9eYWWZ424EjB61l0F6s4KOFo0glOh+nf/pF+e82q31w5oH8j/Vd9Wwy/ul0v4vXChk5Rhkvm1PobyfHatAlWeN/m4JMYKg3DAKkkeLI2m+aTpA4EwidU9TR/vpKCuPPWjk0bTLdYfOClTIJ2XXPttNqGXUEbSqxSlDhwVHbyrvjgZIbLwPj2yM/F/mE2MhB3kqwf/qNMstC4TsGQKBgQDiWwcmH6tMyTjSz5qpsCGiMrHvYkYxwZQCgtPn00kLrMPRsRqOzVX8pLn0fcWmgj6XPAMwY9RSZhOjOL03qw+Y3xi6hpHRDe0R2jhZSvmHGR7ObXQeKwp54LqHQPs0mWSHf8anYtJxFkM5QBUuO3pPtf/KwA/qi7i6uLq00VphcwKBgQCoRyjFPtb/smj/46PKU6w2TxPGAKaQm236yOdW2QFwAWuVFyT+OfeuCntmahPxAqZXXCz9XYIQhoGXClVxlm68ulymAiLfqx26TL5qEguyc5JgzwNFnOqrtY8MUALsLFLwvx0VUHEg1DGIhCuEqTBOCYBpMka7ifMaTfqxpIAqLQKBgEBjCEuN4po6+tDsgOAX29lZdkfTuofYupNkR4bAER0jDaiOKjHIstbkvIiRZjGRBuDaD0Yqf3DY3GkncJtvK0ae31ffg4bl4TOc0T6FFymYuayacddLAvXcOPLURyCVKYVpkUZ308ibPpAAbIX0ITyfVp2DuINNL0Td2y4oBv+FAoGAYorZOzijWrU1e7PotJDvJhaEs8ypvcDnVJbVsiv+BYMvZu1Ywm9btVV6Ee1IpDgzBLF7vxtFETxTIwnwI0Y+rtJLcwOJPIXOMMzELQVISEYZkAtqFRfg7JCzpjgsnfUO1OB1vYeP1/Oq1hDgEx2opeo2gmPou+X6sIq9pc15KDkCgYEAwEF//T/JzSWxRK+bCZM5SxJN4OVAjA4Ryvois0sYjgaiBdz3pg6ul4RRn0rAqYdoS7s/fsCfkQLD+xjAj1BVzZc+Q4XCdpug08EECDv6+C5+FbM1V6QTd/uu63iFfelFb4OhTFGISZOdmhlEajTZbpmmHTEM4ygmXiK+RoOaDN0=';

$priKey = formatPriKey($priKey);

echo '<pre>';
echo $priKey;
echo '</pre>';

PHP给字符串创建RSA签名, 以及校验签名是否正确

代码如下

<?php
// 在此定义常量
//RSA 公钥
define('RSA_PUBLIC', '-----BEGIN PUBLIC KEY-----
你的RSA公钥
-----END PUBLIC KEY-----');

//RSA 私钥
define('RSA_PRIVATE', '-----BEGIN PRIVATE KEY-----
你的RSA私钥
-----END PRIVATE KEY-----');


/**
 * 通过私钥给字符串生成签名
 * @param    string     $string 待签名字符串
 * @param    [type]     $priKey     私钥
 * @return   string     base64结果值
 */
function getSign($string,$priKey = RSA_PRIVATE){
    $privKeyId = openssl_pkey_get_private($priKey);
    $signature = '';
    openssl_sign($string, $signature, $privKeyId);
    openssl_free_key($privKeyId);
    return base64_encode($signature);
}

 
/**
 * 通过公钥检测签名是否正确
 * @param    string     $pubKey 公钥
 * @param    string     $sign   签名
 * @param    string     $string 这个签名对应的字符串
 * @return   bool
 */
function checkSign($sign,$string,$pubKey = RSA_PUBLIC){
    $publicKeyId = openssl_pkey_get_public($pubKey);
    $result = openssl_verify($string, base64_decode($sign), $publicKeyId);
    openssl_free_key($publicKeyId);
    return $result === 1 ? true : false;
}

$a = 'hello';
$sign = getSign($a);
$check = checkSign($sign,$a) ? '正确' : '错误';
echo "{$a}的签名是{$sign}<br>";
echo "{$sign}这个签名对{$a}的校验结果是: {$check}";