PHP RSA和RSA2加密算法代码

PHP RSA和RSA2加密算法代码

rsa和rsa2加密代码实现

公钥私钥生成地址

// An highlighted block
1024 为rsa公私钥对
2048 为rsa2公私钥对
pub_key rsa公钥
pri_key rsa私钥
粘贴即可使用
    /**
    *	有不懂联系邮箱[email protected]
     * @param string $decrypted
     * @param string $sign_type
     * @param string $pub_key
     * @return string
     * 分段公钥加密
     */
    public function partPubEncrypt($decrypted='',$sign_type="rsa",$pub_key)
    {
     
        $piecewise=$sign_type=='rsa'?117:234;
        if(empty($decrypted)) $decrypted=$this->data;
        $dataArray = str_split($decrypted,$piecewise);
        $bContent = '';
        foreach ($dataArray as $key => $subData) {
     
            $bContent .= self::publicEncrypt($subData,$pub_key);
        }
        return base64_encode($bContent);
    }
      /**
     * @param $decrypted
     * @param $pub_key
     * @return null
     * 公钥加密
     * pub_key公钥
     */
    private function publicEncrypt($decrypted,$pub_key)
    {
     
        if (!is_string($decrypted)) {
     
            return null;
        }
        return (openssl_public_encrypt($decrypted, $encrypted,$pub_key)) ? $encrypted : null;
    }
        /**
     * 分段私钥解密
     * @param string $encrypted
     * @param string $sign_type
     * @param string $pri_key
     * @return String
     */
    public function partPrivDecrypt($encrypted='',$sign_type="rsa",$pri_key)
    {
     
        $piecewise=$sign_type=='rsa'?128:256;
        if(empty($encrypted)) $encrypted=$this->data;
        $encrypted = base64_decode($encrypted);
        $dataArray = str_split($encrypted, $piecewise);
        $bContent = '';
        foreach ($dataArray as $key => $subData) {
     
            $bContent .= self::privDecryptNB64($subData,$pri_key);
        }
        return  $bContent;
    }
        /**
     * 私钥解密
     * @param $encrypted //解密字符
     * @param $pri_key 私钥
     * @return null
     */
    private function privDecryptNB64($encrypted,$pri_key)
    {
     
        if (!is_string($encrypted)) {
     
            return null;
        }
        return (openssl_private_decrypt($encrypted, $decrypted, $pri_key)) ? $decrypted : null;
    }

你可能感兴趣的:(RSA2,RSA,加密解密,rsa,openssl)