php - Rsa加密示例

环境 php5.6

<?php

/**
 * @class_name RSA加密类
 */
class Rsa
{
    const KEY_LENGTH = 1024; // 长度,因RSA对明文加密长度有限制,所以采用分段加密。

    private $__privateKey;
    private $__publicKey;
    // 使用这个类要传入公钥私钥路径
    public function __construct($config)
    {
        if (!extension_loaded('openssl')) {
            throw new Exception('缺少openssl模块');
        }

        if (empty($config['publicKey']) && empty($config['privateKey'])) {
            throw new Exception('缺少公钥或私钥参数');
        }

        if (!empty($config['publicKey'])) {
            $public_key = file_get_contents($config['publicKey']);
            $this->__publicKey = openssl_pkey_get_public($public_key);
        }

        if (!empty($config['privateKey'])) {
            $private_key = file_get_contents($config['privateKey']);
            $this->__privateKey = openssl_pkey_get_private($private_key);
        }
    }

    /**
     * @name 加密
     * @author wan.cheng 
     * @param array $originData 原数据
     * @return string
     */
    public function encrypt($originData)
    {
        $encryptData = ''; // 存储加密后的数据
        $originData = json_encode($originData);
        $partLength = self::KEY_LENGTH / 8 - 11; // 每块明文的长度
        $parts = str_split($originData, $partLength); // 明文分块
        foreach ($parts as $part) {
            $encryptTemp = '';
            openssl_public_encrypt($part, $encryptTemp, $this->__publicKey);
            $encryptData .= $encryptTemp;
        }

        return base64_encode($encryptData);
    }

    /**
     * @name 解密
     * @author wan.cheng 
     * @param $encryptData 加密后的数据
     * @return array
     */
    public function decrypt($encryptData)
    {
        $originData = ''; // 存储解密后的数据
        $encryptData = base64_decode($encryptData);

        $partLength = self::KEY_LENGTH / 8; // 每块密文的长度
        $parts = str_split($encryptData, $partLength); // 密文分块
        foreach ($parts as $part) {
            $encryptTemp = '';
            openssl_private_decrypt($part, $encryptTemp, $this->__privateKey);
            $originData .= $encryptTemp;
        }
        return json_decode($originData, true);
    }

    /**
     * @name 非分段加密
     * @author wan.cheng 
     * @param [string] @originData 需要加密的字符串
     * @return str 加密后的字符串
     */
    public function shortEncrypt($originData)
    {
        $encryptTemp = '';
        openssl_public_encrypt($originData, $encryptTemp, $this->__publicKey);
        return base64_encode($encryptTemp);
    }

    /**
     * @name 非分段解密
     * @author wan.cheng 
     * @param  [string] $encryptData [加密的字符串]
     * @return [type]              [description]
     */
    public function shortDecrypt($encryptData)
    {
        $encryptData = base64_decode($encryptData);
        $encryptTemp = '';
        openssl_private_decrypt($encryptData, $encryptTemp, $this->__privateKey);
        return $encryptTemp;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值