美文网首页
Rsa加解密

Rsa加解密

作者: 可望不可j | 来源:发表于2021-08-03 21:13 被阅读0次

/**

* Rsa 加解密

* 用法:

*      (1)公钥加密,私钥解密

*      (2)私钥加密,公钥解密

*/

class Rsa

{

    /**

* 公钥-加密

    * @param string $RSA_PUBLIC 公钥

    * @param string $string 需要加密的字符串

    * @param bool $is_sssembly true|需要拼接  false|不需要

    * @return array

*/

    public function public_key_encryp($RSA_PUBLIC,$string,$is_sssembly=false){

        if($is_sssembly){

            $RSA_PUBLIC = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($RSA_PUBLIC, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

}

        //验证公钥是否正确

        $public_key = openssl_pkey_get_public($RSA_PUBLIC);

        if(!$public_key){

            return ['status'=>false,'messate'=>'公钥不可用'];

}

        //第一个参数是待加密的数据只能是string,第二个参数是加密后的数据,第三个参数是openssl_pkey_get_public返回的资源类型,第四个参数是填充方式

        $return_en = openssl_public_encrypt($string, $crypted, $public_key);

        if(!$return_en){

            return ['status'=>false,'messate'=>'公钥错误'];

}

        $eb64_cry = base64_encode($crypted);

        return ['status'=>true,'messate'=>'ok','data'=>$eb64_cry];

}

    /**

* 私钥-解密

    * @param string $RSA_PRIVATE 私钥

    * @param string $string 需要加密的字符串

    * @param bool $is_sssembly true|需要拼接  false|不需要

    * @return array

*/

    public function private_key_decrypt($RSA_PRIVATE,$string,$is_sssembly=false){

        if($is_sssembly){

            $RSA_PRIVATE = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($RSA_PRIVATE, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

}

        //验证私钥

        $private_key = openssl_pkey_get_private($RSA_PRIVATE);

        if(!$private_key){

            return ['status'=>false,'messate'=>'私钥不可用'];

}

        $return_de = openssl_private_decrypt(base64_decode($string), $decrypted, $private_key);

        if(!$return_de){

            return ['status'=>false,'messate'=>'解密失败,请检查私秘钥'];

}

        return ['status'=>true,'messate'=>'ok','data'=>$decrypted];

}

    /************************************************************************************************************/

/**

* 私钥-加密

    * @param string $RSA_PRIVATE 私钥

    * @param string $string 需要加密的字符串

    * @param bool $is_sssembly true|需要拼接  false|不需要

    * @return array

*/

    public function private_key_encryp($RSA_PRIVATE,$string,$is_sssembly=false){

        if($is_sssembly){

            $RSA_PRIVATE = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($RSA_PRIVATE, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

}

        //验证私钥是否正确

        $private_key = openssl_pkey_get_private($RSA_PRIVATE);

        if(!$private_key){

            return ['status'=>false,'messate'=>'私钥不可用'];

}

        //第一个参数是待加密的数据只能是string,第二个参数是加密后的数据,第三个参数是openssl_pkey_get_public返回的资源类型,第四个参数是填充方式

        $return_en = openssl_private_encrypt($string, $crypted, $private_key);

        if(!$return_en){

            return ['status'=>false,'messate'=>'加密失败'];

}

        $eb64_cry = base64_encode($crypted);

        return ['status'=>true,'messate'=>'ok','data'=>$eb64_cry];

}

    /**

* 公钥-解密

    * @param string $RSA_PUBLIC 公钥

    * @param string $string 需要加密的字符串

    * @param bool $is_sssembly true|需要拼接  false|不需要

    * @return array

*/

    public function public_key_decrypt($RSA_PUBLIC,$string,$is_sssembly=false){

        if($is_sssembly){

            $RSA_PUBLIC = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($RSA_PUBLIC, 64, "\n", true) . "\n-----END PUBLIC KEY-----";

}

        //验证公钥是否正确

        $public_key = openssl_pkey_get_public($RSA_PUBLIC);

        if(!$public_key){

            return ['status'=>false,'messate'=>'公钥不可用'];

}

        $return_en = openssl_public_decrypt(base64_decode($string), $decrypted, $public_key);

        if(!$return_en){

            return ['status'=>false,'messate'=>'解密失败'];

}

        return ['status'=>true,'messate'=>'ok','data'=>$decrypted];

}

}

相关文章

网友评论

      本文标题:Rsa加解密

      本文链接:https://www.haomeiwen.com/subject/aozevltx.html