• php 验证rsa公钥和私钥是否正确


    <?php
    /**
     * RSA加密
     *
     * @param string $data 待加密数据
     * @param string $publicKey 公钥
     * @return string|false 加密结果
     * @author SC
     */
    function rsaEncrypt($data, $publicKey)
    {
        $ciphertext = '';
        $publicKey = openssl_pkey_get_public($publicKey);
        $data = str_split($data, 117); // 加密的数据长度限制为比密钥长度少11位,如128位的密钥最多加密的数据长度为117
        foreach ($data as $d) {
            openssl_public_encrypt($d, $crypted, $publicKey); // OPENSSL_PKCS1_PADDING
            $ciphertext .= $crypted;
        }
        openssl_free_key($publicKey);
    
        return base64_encode($ciphertext);
    }
    
    
    /**
     * RSA解密
     *
     * @param string $data 待解密数据
     * @param string $privateKey 私钥
     * @param string 解密结果
     * @author SC
     */
    function rsaDecrypt($data, $privateKey)
    {
        $plaintext = '';
        $privateKey = openssl_pkey_get_private($privateKey);
        $data = base64_decode($data);
        $data = str_split($data, 128);
        foreach ($data as $d) {
            $r = openssl_private_decrypt($d, $decrypted, $privateKey);
            $plaintext .= $decrypted;
        }
    
        return $plaintext;
    }
    
    /**
     * 生成RSA签名
     *
     * @param string $data 待签名数据
     * @param string $privateKey 私钥
     * @return string 签名
     * @author SC
     */
    function rsaSign($data, $privateKey)
    {
        $privateKey = openssl_get_privatekey($privateKey);
        openssl_sign($data, $sign, $privateKey, OPENSSL_ALGO_MD5); // OPENSSL_ALGO_MD5 or OPENSSL_ALGO_SHA1(default)
        openssl_free_key($privateKey);
    
        return base64_encode($sign);
    }
    
    /**
     * 检验RSA签名
     *
     * @param string $data 待签名数据
     * @param string $sign 待验证签名
     * @param string $publicKey 公钥
     * @return bool 检验结果
     * @author SC
     */
    function rsaVerify($data, $sign, $publicKey)
    {
        $publicKey = openssl_get_publickey($publicKey);
        $result = openssl_verify($data, base64_decode($sign), $publicKey, OPENSSL_ALGO_MD5); // OPENSSL_ALGO_MD5 or OPENSSL_ALGO_SHA1(default)
        openssl_free_key($publicKey);
    
        return ($result == 1) ? true : false; // -1:错误;0:签名错误;1:签名正确
    }
    
    
    
    $pubKey = '-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsvvkZ0EYjDcmYsRbcdEugUybF
    YGM3WKZfmZQaU+jHIk0UTw2I9kDHC2wmYcIAA3fqCeJ71WvdiqJNXByUYgsKGuLC
    SYUP9wqOGze0U9hNb/M8E1JHlKbo3v0oAsp8awXl7QDRsOhibJUjIdf3Gt/qwh1R
    mAtYSWHi/BLfzC6nfQIDAQAB
    -----END PUBLIC KEY-----';
    $a = rsaEncrypt(123, $pubKey);
    
    $priKey = '-----BEGIN PRIVATE KEY-----
    MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKy++RnQRiMNyZix
    Ftx0S6BTJsVgYzdYpl+ZlBpT6MciTRRPDYj2QMcLbCZhwgADd+oJ4nvVa92Kok1c
    HJRiCwoa4sJJhQ/3Co4bN7RT2E1v8zwTUkeUpuje/SgCynxrBeXtANGw6GJslSMh
    1/ca3+rCHVGYC1hJYeL8Et/MLqd9AgMBAAECgYBJvO9KJIx71BvU0OsISaAqnLP1
    FtLjQjUqo70DjAPWVP8AB0qoxy0ZifetXpdYOCLMxVFnOM/lbpMfYI1WjY9WGmDQ
    C0UMpOEuk/bhqAJMIN79WpEqWvJQYFzRWw3O63SwbU3mcYgqssULk6VXhhTuCuNI
    gc9D/xNyj3CsfKLgRQJBANNsErsLNeM07lOaeTRXC+OYZmz9TOIgXRliYH+r52rF
    QTYGY6zfs1SgDPIZZZ+1nCA6ifkFlm4xnhptlWTZTzsCQQDRK0cGCt52Iusu0LV9
    2jiY1fNP8QX4bJThT+iAuGuSZeOgzWQE1Wic6j1Rsgn6XELwY7kpZbLKXWoXLVJC
    9GinAkA1J53J8vaYai1lTZgdhakSjrVpv4F99L8Ta71xO+oR2QgdKhwTUabHpvm6
    QJJhRgq7u5wGxNXeXJEwec6urUgTAkAstHo2kWYgi7AGTncr0dMc30VkhZgIE3Ty
    fvirYj8jiAFRofRJkC8OsoWui0XUq5lm9XlinqjK1ZJSJ86YHtKrAkBnB6+fO1/h
    j1Jm7L5miQVDYE4VRlVK/oAWTlceVn7a6HUQOTQUlHAiKGxuhzB7jIyLoQWLXHyG
    vpZ0T54nyQqu
    -----END PRIVATE KEY-----';
    $b = rsaDecrypt($a, $priKey);
    echo $b;
    
    $c = rsaSign(333, $priKey);
    //  echo $c;
    echo '<pr>';
    var_dump(rsaVerify(333, $c, $pubKey));
  • 相关阅读:
    MytBatis错题分析
    Spring核心概念
    延迟与缓存
    MyBatis的关联查询
    Mabatis注解
    [leetcode]226. Invert Binary Tree翻转二叉树
    [leetcode]633. Sum of Square Numbers平方数之和
    [leetcode]296. Best Meeting Point最佳见面地点
    [leetcode]412. Fizz Buzz报数
    [leetcode]142. Linked List Cycle II找出循环链表的入口
  • 原文地址:https://www.cnblogs.com/-mrl/p/10874093.html
Copyright © 2020-2023  润新知