美文网首页
PHP rsa加密生成(加密通信)

PHP rsa加密生成(加密通信)

作者: blank喵 | 来源:发表于2020-04-01 09:32 被阅读0次
<?php
/**
 * User: orzblankcat
 * Date: 2019/1/25
 * Time: 17:30
 */
class RsaClass
{
    protected $config = [
//        "digest_alg"    => 'sha512',               //加密模式
        "private_key_bits" => 4096,                  //字节数  512 1024 2048  4096 等
        "private_key_type" => OPENSSL_KEYTYPE_RSA,   //加密类型
        "config" => "E:/phpStudy/PHPTutorial/Apache/conf/openssl.cnf",
        "encrypt_key"=>true //私钥加密
    ];
    protected $passphrase = '123456';//密码 当encrypt_key=>true必填
    protected $pi_key='';
    protected $pu_key='';
    protected $priPath='Common/rsa/pri.key';
    protected $pubPath='Common/rsa/pub.key';

    public function __construct()
    {
        extension_loaded('openssl') or die('php需要openssl扩展支持');
        if(!file_exists($this->pubPath)||!file_exists($this->priPath))
        {
            $this->createKey();
        }
    }

    public function createKey()
    {
        $res = openssl_pkey_new($this->config);
        //提取私钥
        openssl_pkey_export($res, $private_key, $this->passphrase,$this->config);

        //生成公钥
        $public_key = openssl_pkey_get_details($res);
        $public_key = $public_key["key"];

        //显示数据
        $pri = fopen($this->priPath, "w") or die("Unable to open file!");
        fwrite($pri, $private_key);

        $pub = fopen($this->pubPath, "w") or die("Unable to open file!");
        fwrite($pub, $public_key);

        fclose($pri);
        fclose($pub);
    }

    /**
     * 公钥加密数据
     */
    public function pubEncryptKey($data)
    {
        $this->pu_key = openssl_pkey_get_public(file_get_contents($this->pubPath));
        openssl_public_encrypt($data, $encrypted, $this->pu_key);
        return base64_encode($encrypted);
    }
    /**
     * 公钥解密数据
    */
    public function pubDecodeKey($data)
    {
        $this->pu_key = openssl_pkey_get_public(file_get_contents($this->pubPath));
        openssl_public_decrypt(base64_decode($data), $decrypted, $this->pu_key);//公钥解密
        return $decrypted;
    }
    /**
     * 私钥加密数据
    */
    public function priEncryptKey($data)
    {
        $this->pi_key = openssl_pkey_get_private(file_get_contents($this->priPath),$this->passphrase);
        openssl_private_encrypt($data, $encrypted, $this->pi_key);
        return base64_encode($encrypted);
    }
    /**
     * 私钥解密数据
     */
    public function priDecodeKey($data)
    {
        $this->pi_key = openssl_pkey_get_private(file_get_contents($this->priPath),$this->passphrase);
        openssl_private_decrypt(base64_decode($data), $decrypted, $this->pi_key);//私钥解密
        return $decrypted;
    }

}

使用的时候

$Rsa = new RsaClass();

$result = $Rsa->pubEncryptKey(time());
var_dump($result);
$decoded = $Rsa->priDecodeKey($result);
var_dump($decoded);

相关文章

  • PHP rsa加密生成(加密通信)

    使用的时候

  • RSA签名认证

    RSA可汗学院第一章 RSA加密 RSA加密原理第一章 RSA加密原理第二章 如何生成RSA公钥私钥 生成类似支付...

  • Android-rsa解密服务端给的加密过的内容,利用rsa公钥

    内容后台加密方式:Base64 encode加密 -> Rsa私钥加密, 给我公钥用来解密, 实现方式参考:PHP...

  • SSH免密登陆

    一、SSH简介 SSH(Secure Shell)是一种通信加密协议,加密算法包括:RSA、DSA等。 1、RSA...

  • RSA非对称加密算法

    RSA算法,经典非对称加密算法,通过生成公钥 私钥 进行加密解密 公钥加密 私钥解密 反之 私钥加密 公钥...

  • python的加密方式: rsa加密和解密

    RSA加密是一种非对称加密,通常使用公钥加密,私钥解密。 公钥、私钥的生成 生成文件如下图: 可以将生成的公钥、私...

  • PHP RSA 加密

    RSA非对称加密算法,如果是公钥加密,就得用私钥解密,反过来也一样,私钥加密的就用公钥解密,以下是相关实现函数 这...

  • PHP RSA加密

    这两天正好做一个rsa加密,现在就总结一下其中的几个要点。 1.什么是rsa算法 RSA公钥加密算法是1977年由...

  • RSA加密 PHP

    php rsa类 生成 rsa 私钥openssl genrsa -out rsaprivatekey.pem 1...

  • iOS学习-数据加密

    在iOS端使用RSA加密的记录 一、需求: SDK开发,使用RSA加密和后台进行数据交互,后台是PHP要求:1、p...

网友评论

      本文标题:PHP rsa加密生成(加密通信)

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