PHP RSA2 加密 、 解密 、签名 、验证签名

突然发现接收百度资源太多了,我也回馈一下吧。

RSA2 标准算法名称 SHA256WithRSA

1、RSA非对称加密技术

2、SHA256 是SHA-2下细分出的一种算法

SHA-2,名称来自于安全散列算法2(英语:Secure Hash Algorithm 2)的缩写,一种密码散列函数算法标准,由美国国家安全局研发,属于SHA算法之一,是SHA-1的后继者。

详细介绍连接:https://blog.csdn.net/u011583927/article/details/80905740

1、加密和解密
公钥是公开的密钥,有加密方使用。只用于加密无法解密。私钥是不公开的,别人无法获取,用户解密。
注意的是,为什么私钥对同一数据进行签名加密的结果是一样的,使用公钥进行加密就不一样了呢?
详细请参考:https://blog.csdn.net/guyongqiangx/article/details/74930951

2、签名和验证签名
签名是由发送数据的一方发起的,防止传输过程中被篡改数据内容。因此签名使用的是私钥。而验证签名使用的是公钥。

附带:
java RSA2 加密、解密 、签名和验证签名
https://blog.csdn.net/TaLinBoy/article/details/106124535

IOS:RSA2 加密、解密 、签名和验证签名
https://blog.csdn.net/TaLinBoy/article/details/106140526

//公钥
function rsaPublicKey()

{
    return "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArNPzr7pnN+LBV9c1vFWlK6qAc5Y3fx1TLeIajhP7ni3Jrrs3uLBOQjoBjx6fmQ9WZyLlqdsFOftLe5cQsQlRHtVakAQIypUJyB7VJ5HzOA+lJS6675V4xzntwrYcTTxh25UXoAMae+RNkKwhmhNIi2fxl1638SbiwTi2msmgvkkeZwu9hw8r+p/3VE8dS6sH6jDda3L/M4fRIymfcRl9NzLue3wVb6ynALHIoOK5IjsKSDlpV6qGgx1QUy+S6RKXRkHoeOt5q/abvH0Sjdbz1FANkQBov3zU1wyt2G/DtClkUjaEMDmC/u1PhO1WQ/qsDmsI7c9cenrMPKpieMDxJQIDAQAB";
}

//私钥
function getPrivateKey()
{
    return $priKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCs0/Ovumc34sFX1zW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tFtigmWPkg/iGVzBbXjqW29Bk1sE+9oWgKYXzQ5XBGx8EwVllwGIY0i9YGf7MUe3Tm7P9MyLzTqImwTWygQejvrBBdiZMZPLiNmx9tAFMpE25D7HBDXUbeC8bC7QDWN+v8rCBQQKBgQCi2nBl6/3iar4siAkemzpqe9wwNg5b5A7z0S481AY+Vr+zkVc4myE2Q0k8e1yRR+OpahUF5gvMCPzLO55PsH+r8KdCbbkn8F4LHm/yCJerndt/KpkuRF0rwnO5pjW3UNAgULp4tf2QnY+tD6AgUztuGl7Yun3cpUtjw+ljgERNWQKBgBX1YkC0ucYJyNl8y7Qb8ysnwqAbQgg9ynkXIpyCjSJcUZPvncFYaQGOTtYPTOa4qp49xfFqkFgOrMiQFoKSZSHtHTQhEpS0v3S0RA0MMCV0sxJ5hq/t3oPuI9+3e1BKXGTiBMMF3X38/xD7lvoLZqVxXWTxzXWudLzhpIqibXKn";
}

/使用RSA公钥加密数据 结果是base64
function encryptRSA($dataStr, $publicKey)
{
    try {
        if (empty($publicKey)) {
            return false;
        }
        if (empty($dataStr)) {
            return false;
        }
        $res = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($publicKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
        $publicKey = openssl_pkey_get_public($res); //解析公钥
        openssl_public_encrypt($dataStr, $encrypted, $publicKey);
        if (empty($encrypted)) {
            return false;
        }
        return base64_encode($encrypted);
    } catch (Exception $e) {
        return false;
    }
}

//使用自己的RSA私钥解析数据 
function decryptRSA($encryptedStr)
{
    try {
        if (empty($encryptedStr)) {
            return false;
        }
        $privateKey = getPrivateKey();
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($privateKey, 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----";
        $key = openssl_pkey_get_private($res); //解析私钥
        $encrypted = base64_decode($encryptedStr);
        //解密
        openssl_private_decrypt($encrypted, $decrypted, $key);
        if (empty($decrypted)) {
            return false;
        }
        return $decrypted;
    } catch (Exception $e) {
        return false;
    }
}

//sha256WithRSA 私钥签名加密
function getSign($dataStr)
{
    try {
        if (empty($dataStr)) {
            return false;
        }
        $privateKey = getPrivateKey();
        $privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" . wordwrap($privateKey, 64, "\n", true) . "\n-----END RSA PRIVATE KEY-----";
        echo $privateKey."\n";
        $key = openssl_pkey_get_private($privateKey);
        openssl_sign($dataStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
        openssl_free_key($key);
        if (empty($sign)) {
            return false;
        }
        $sign = base64_encode($sign);
        return $sign;
    } catch (Exception $e) {
        return false;
    }
}

//验证 sha256WithRSA 签名   sign是签名后的base64   dataStr是签名的内容
function verify($dataStr, $sign, $publicKey)
{
    try {
        if (empty($dataStr)) {
            return false;
        }
        $res = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($publicKey, 64, "\n", true) . "\n-----END PUBLIC KEY-----";
        $key = openssl_pkey_get_public($res);
        $data = openssl_verify($dataStr, base64_decode($sign), $key, OPENSSL_ALGO_SHA256);
        return $data;
    } catch (Exception $e) {
        return false;
    }
}

你可能感兴趣的:(PHP)