wonderwong 2019-08-24 08:24:29 5191次浏览 1条评论 6 1 0

在我们的数据传输过程中,有些数据是加密后返回过来的,这个时候 客户端 需要对这个数据进行解密 ,大家都知道,单向加密,解密 只有一个加密key,黑客很容易就破解了。 那么今天就给大家介绍另外一个加密方式。非对称加密。 这个加密的好处是 ,有两把钥匙,可以打开同一把锁,并且两把钥匙都不一样。私钥加密,公钥解密。公钥解密,私钥解密。代码如下:

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/23
 * Time: 17:23
 */
namespace app\service;

class Rsa {

    /**
     * @var string 公钥
     */
    public $publickey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzZYvkn/r9jAyfKi46VKP6NBt8CMnd56e1qNDTDrRJQv/qpwNMTj8crJnyDnKyUl8g16qfqVKCn3n1kxnbuncyYnYE2M0CZLkA7A+sbpiod48oDXjfUZCpolhEUtXNBE4owKZT/NwymhwI0yq8UtVobjnHTfOQwkA48gOW7Wr+TJ2R2PzzGujKD4oquDdidSFKKI454FpurIkg/PRybNqECytNN7FFZnxiQcKNrqM0hM2Aa/LS0F0E0iifhIrj/S29NGH1QTxjhjx78urGGsu++zhyIKbeHtf7ncLwkWU2KgXwOw6r2qomloGEGrIYlPS8ERCIK+d6bXb+tnbGo7xxQIDAQAB';

    /**
     * @var string 私钥
     */
    public $privatekey = 'MIIEpAIBAAKCAQEAzZYvkn/r9jAyfKi46VKP6NBt8CMnd56e1qNDTDrRJQv/qpwNMTj8crJnyDnKyUl8g16qfqVKCn3n1kxnbuncyYnYE2M0CZLkA7A+sbpiod48oDXjfUZCpolhEUtXNBE4owKZT/NwymhwI0yq8UtVobjnHTfOQwkA48gOW7Wr+TJ2R2PzzGujKD4oquDdidSFKKI454FpurIkg/PRybNqECytNN7FFZnxiQcKNrqM0hM2Aa/LS0F0E0iifhIrj/S29NGH1QTxjhjx78urGGsu++zhyIKbeHtf7ncLwkWU2KgXwOw6r2qomloGEGrIYlPS8ERCIK+d6bXb+tnbGo7xxQIDAQABAoIBADgQTMjWubJZUmtMV6DJMxV6QYDLFWg5iXde7mtLXt2LeB0+kRprP+zHWiXwHKznrVzl4/jFenuB+kcmbs6AWAVd4d7nnrk9pZCb73zoJx2dTUsjZvYywtpHfIEK2q0IRTmu68Ax0wBfmrUFo1hKAwAnPF9raIQ/YE69r6yyzkMWUit+e22KgGznW1Kwih6jBzzKPhKQjwsUNAEds7f1haulRgImA2i4FUjLw41ZyAir8FvTewiO05ETJ4u8MatTAoNyWL3MgzwbPpKBZizA4xO0f2BbfvvDlXH9yw1tCC1HdbY2IJ2rGQjAY8tZr0W28gK198Zy5NUdEFYFD2cbSgECgYEA+tb/NlRXA3TGl2gdCHdGzLu7YTT0+rNpwU3kcm3uOPvaioJb1yfTDqletLhmkDUvu56H7MFdYzXFcK+WRp+04knkm7tYJz3jnS+F0M17x8yZMRidrmY/D2w3J0GI+SdD8omrs0oVjADD10ZnLQN6AbnuDGO5NC4rr3Lh1iTLfI0CgYEA0dDfBqLacJyqjtM2AyaImBzlFSoz8fSsGKZWHxsB9bQiq6TROy6zbx9bJpvuYWtT4PN8zH1fpvurLbJ4Q7aoJBo9a8EiWPjpFqyQNq5M6cE1n227rqD7NWKTW79q8B1Xuf2OlVJfGZC';

    /**
     * @return string 获取公钥
     */
    public function getPublicKey()
    {
        $pub_key ="-----BEGIN PUBLIC KEY-----\n"
            .wordwrap($this->publickey, 64, "\n", true).
            "\n-----END PUBLIC KEY-----";
        return $pub_key;
    }

    /**
     * @return string 获取私钥
     */
    public function getPrivateKey()
    {
        $pri_key ="-----BEGIN PRIVATE KEY-----\n"
            .wordwrap($this->privatekey, 64, "\n", true).
            "\n-----END PRIVATE KEY-----";
        return $pri_key;
    }

    /**
     * 用户公钥加密
     * @param $data string  需要加密的数据
     * @return string  加密后的数据
     */
    public function encryptByPublicKey($data)
    {
        openssl_public_encrypt($data,$crypted,$this->getPublicKey());
        return $crypted;
    }

    /**
     * 用私钥加密
     * @param $data string 需要加密的数据
     * @return string 加密后的数据
     */
    public function encryptByPrivateKey($data)
    {
        openssl_private_encrypt($data,$crypted,$this->getPrivateKey());
        return $crypted;
    }

    /**
     * 公钥解密
     * @param $data string 用公钥解密
     * @return mixed 解密后的数据
     */
    public function decryptByPublicKey($data)
    {
        openssl_public_decrypt($data,$decrypted,$this->getPublicKey());
        return $decrypted;
    }

    /**
     * 私钥解密
     * @param $data string 私钥解密的数据
     * @return mixed 解密后的数据
     */
    public function decryptByPrivateKey($data)
    {
        openssl_private_decrypt($data,$decrypted,$this->getPrivateKey());
        return $decrypted;
    }

    public function makeSign()
    {
        return true;
    }
觉得很赞
您需要登录后才可以评论。登录 | 立即注册