Notepad++Good Luck To You!

PHP7 RSA签名、验签、加密、解密
<?php


namespace App\Helpers;


class RSATools
{
   private $privateKey;
   private $publicKey;
   private $type;

   public function __construct()
   {
       $this->setType('RSA');
   }

   public function setPrivatekey($key)
   {
       if(!file_exists($key)){
           exit("PrivateKey File Lost");
       }
       $this->privateKey = file_get_contents($key);
       return $this;
   }

   public function setPublicKey($key){
       if(!file_exists($key)){
           exit("PublicKey File Lost");
       }
       $this->publicKey=file_get_contents($key);
       return $this;
   }

   public function setType($type='RSA'){
       $this->type=$type;
       return $this;
   }

   public  function sign($content)
   {
       if(!$this->privateKey){
           exit("PrivateKey Not Found!");
       }
       $signature = null;
       if ($this->type == 'RSA') {
           openssl_sign($content, $signature, $this->privateKey, OPENSSL_ALGO_SHA1);
       } elseif ($this->type == 'RSA2') {
           openssl_sign($content, $signature, $this->privateKey, OPENSSL_ALGO_SHA256);
       }
       return $signature ? base64_encode($signature) : null;
   }

   public  function verify($content, $signature)
   {
       $signature = str_replace(" ", "+", $signature);
       $content = str_replace(" ", "+", $content);
       if(!$this->publicKey){
           exit("PublicKey Not Found!");
       }
       $verify = false;
       if ($this->type == 'RSA') {
           $verify = openssl_verify($content, base64_decode($signature), $this->publicKey, OPENSSL_ALGO_SHA1);
       } elseif ($this->type == 'RSA2') {
           $verify = openssl_verify($content, base64_decode($signature), $this->publicKey, OPENSSL_ALGO_SHA256);
       }
       return (boolean)$verify;
   }

   public  function encrypt($content)
   {
       if(!$this->publicKey){
           exit("publicKey Not Found!");
       }

       $encrypted = null;
       if ($this->type == 'RSA') {
           openssl_public_encrypt($content, $encrypted, $this->publicKey, OPENSSL_ALGO_SHA1);
       } elseif ($this->type == 'RSA2') {
           openssl_public_encrypt($content, $encrypted, $this->publicKey, OPENSSL_ALGO_SHA256);
       }
       return base64_encode($encrypted);
   }

   public  function decrypt($content)
   {
       $content = str_replace(" ", "+", $content);
       if(!$this->privateKey){
           exit("PrivateKey Not Found!");
       }

       $decrypted = null;
       if ($this->type == 'RSA') {
           openssl_private_decrypt(base64_decode($content), $decrypted, $this->privateKey, OPENSSL_ALGO_SHA1);
       } elseif ($this->type == 'RSA2') {
           openssl_private_decrypt(base64_decode($content), $decrypted, $this->privateKey, OPENSSL_ALGO_SHA256);
       }
       return $decrypted;
   }

}


«    2023年7月    »
12
3456789
10111213141516
17181920212223
24252627282930
31
TOP 搜索
TOP 控制面板
您好,欢迎到访网站!
  查看权限
TOP 最新留言
    TOP 作者列表
    TOP 站点信息
    • 文章总数:163
    • 页面总数:0
    • 分类总数:6
    • 标签总数:20
    • 评论总数:0
    • 浏览总数:312004
    召唤伊斯特瓦尔