微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
    }

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐