php RSA非对称加密超长字符处理

class Rsa {

    const  RSA_ENCRYPT_BLOCK_SIZE = 117;//加密切割长度
    const  RSA_DECRYPT_BLOCK_SIZE = 128;//解密切割长度

    /**
     * @param $private_key私钥
     * @return false|resource
     */
    public static function getPrivateKey($private_key)
    {
//        $abs_path = base_path().'/public/rsa/rsa_private_key.pem'; //私钥文件方式读取
//        $content = file_get_contents($abs_path);
//        return openssl_pkey_get_private($content);
        return openssl_pkey_get_private($private_key);
    }

    /**
     * @param $public_key公钥
     * @return false|resource
     */
    private static function getPublicKey($public_key)
    {
//        $abs_path = base_path().'/public/rsa/rsa_public_key.pem'; //公钥文件方式读取
//        $content = file_get_contents($abs_path);
//        return openssl_pkey_get_public($content);
        return openssl_pkey_get_public($public_key);
    }

    /**
     * 私钥加密
     * @param $private_key私钥字符
     * @param string $content 加密字符串
     * @return string|null
     */
    public static function privEncrypt($private_key,$content = '')
    {
        if (!is_string($content)) {
            return null;
        }
        $result='';
        $data = str_split($content, self::RSA_ENCRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_private_encrypt($block, $dataEncrypt, self::getPrivateKey($private_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataEncrypt;
        }
        return $result ? base64_encode($result) : null;
    }

    /**
     * 公钥加密
     * @param $public_key公钥字符
     * @param string $content 加密字符串
     * @return string|null
     */
    public static function publicEncrypt($public_key, $content = '')
    {
        if (!is_string($content)) {
            return null;
        }
        $result='';
        $data = str_split($content, self::RSA_ENCRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_public_encrypt($block, $dataEncrypt, self::getPublicKey($public_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataEncrypt;
        }
        return  $result ? base64_encode($result) : null;
    }

    /**
     * 私钥解密
     * @param $private_key私钥
     * @param string $encrypted解密字符串
     * @return string|null
     */
    public static function privDecrypt($private_key,$encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        $result = '';
        $data = str_split(base64_decode($encrypted), self::RSA_DECRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_private_decrypt($block, $dataDecrypt, self::getPrivateKey($private_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataDecrypt;
        }
        return $result ? $result : null;
    }

    /**
     * 公钥解密
     * @param $public_key公钥
     * @param string $encrypted解密字符串
     * @return string|null
     */
    public static function publicDecrypt($public_key,$encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        $result = '';
        $data = str_split(base64_decode($encrypted), self::RSA_DECRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_public_decrypt($block, $dataDecrypt, self::getPublicKey($public_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataDecrypt;
        }
        return $result ? $result : null;
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值