杉哥的个人博客

RSA加解密工具类

一个用php写的RSA加解密工具类

<?php
declare (strict_types = 1);
namespace app\utils\rsa;
/**
 * Desc: RSA加解密工具类
 * Author: p
 * Date: 2021/03/11 14:51
 */
class RSAUtils
{
    /**
     * RSA最大加密明文大小
     */
    public $maxEncryptBlock = 244;

    /**
     * RSA最大解密密文大小
     */
    public $maxDecryptBlock = 256;

    /**
     * 私钥加密
     *
     * @param string $content
     * @return void
     */
    public function encryptByPrivateKey($content,$privateKey)
    {
        $res = $this->PrivateFormatKey($privateKey);

        $crypted = array();
        $dataArray = str_split($content, $this->maxEncryptBlock);
        //分段加密
        foreach($dataArray as $subData){
            $subCrypted = null;
            openssl_private_encrypt($subData, $subCrypted, $res);
            $crypted[] = $subCrypted;
        }
        $crypted = implode('',$crypted);
        return base64_encode($crypted);
    }

    /**
     * 公钥加密
     *
     * @return string
     */
    public function encryptByPublicKey($content,$publicKey)
    {
        $res = $this->PublicFormatKey($publicKey);;

        $crypted = array();
        $dataArray = str_split($content, $this->maxEncryptBlock);
        //对数据分段加密
        foreach($dataArray as $subData){
            $subDecrypted = null;
            openssl_public_encrypt($subData, $subDecrypted, $res);
            $crypted[] = $subDecrypted;
        }
        $crypted = implode('',$crypted);

        // while ($msg = openssl_error_string())
        //     echo $msg . "<br />\n";
        return base64_encode($crypted);
    }

    /**
     * 公钥解密
     *
     * @param string $content
     * @return string
     */
    public function decryptByPublicKey($content,$publicKey)
    {
        $res = $this->PublicFormatKey($publicKey);

        $encryptstr = base64_decode($content);
        $decrypted = array();

        $dataArray = str_split($encryptstr, $this->maxDecryptBlock);
        //对数据分段解密
        foreach($dataArray as $subData){
            $subDecrypted = null;
            openssl_public_decrypt($subData, $subDecrypted, $res);
            $decrypted[] = $subDecrypted;
        }
        $decrypted = implode('',$decrypted);
        return $decrypted;
    }
    
    /**
     * 私钥解密
     *
     * @param string $content
     * @return string
     */
    public function decryptByPrivateKey($content,$privateKey)
    {
        $res = $this->PrivateFormatKey($privateKey);

        $encryptstr = base64_decode($content);
        $decrypted = array();

        $dataArray = str_split($encryptstr, $this->maxDecryptBlock);
        //对数据分段解密
        foreach($dataArray as $subData){
            $subDecrypted = null;
            openssl_private_decrypt($subData, $subDecrypted, $res);
            $decrypted[] = $subDecrypted;
        }
        $decrypted = implode('',$decrypted);
        return $decrypted;
    }
    
    /**
     * @param $key
     * @return string
     */
    private function PrivateFormatKey($key)
    {
        return "-----BEGIN RSA PRIVATE KEY-----" . PHP_EOL .
            wordwrap($key, 64, PHP_EOL, true)
            . PHP_EOL . "-----END RSA PRIVATE KEY-----";
    }

    /**
     * @param $key
     * @return string
     */
    private function PublicFormatKey($key)
    {
        return "-----BEGIN PUBLIC KEY-----" . PHP_EOL .
        wordwrap($key, 64, PHP_EOL, true)
        . PHP_EOL . "-----END PUBLIC KEY-----";
    }
}