RSA加密、解密、验签

RSA 1024位 能加密多少位的字符串?

在PHP中,如果要使用1024位的RSA公钥来进行加密,我们可以使用openssl扩展中的openssl_public_encrypt函数。这个函数使用了PKCS#1 v1.5的填充方案,它要求要加密的字符串的长度必须小于公钥模数长度减去11个字节。因此,对于1024位的公钥,要加密的字符串最多只能有117个字节 。
如果需要加密的字符串长度超过了这个限制,我们可以考虑使用分段加密的方法,即将字符串分成多个小段,每个小段都满足长度限制,然后对每个小段分别进行加密,最后将加密后的字节拼接起来。这样我们就可以加密任意长度的字符串了

<?php
/**
 * @author lyz
 */


class RsaAuth
{
    private $pi_key;
    private $pu_key;

    public function __construct()
    {
//        $fp = fopen("rsa_pi.pem", "r"); //你的私钥文件路径
        $fp = fopen("rsa_private_key.pem", "r"); //你的私钥文件路径
        $private_key = fread($fp, 1024);
        fclose($fp);
//        $fp1 = fopen("rsa_pu.pem", "r"); //你的公钥文件路径
        $fp1 = fopen("rsa_public_key.pem", "r"); //你的公钥文件路径
        $public_key = fread($fp1, 1024);
        fclose($fp1);
        //这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
        $this->pi_key = openssl_pkey_get_private($private_key);
        $this->pu_key = openssl_pkey_get_public($public_key);
    }


    /**
     * 私钥加密
     *
     * @param string $encryptString 加密字符串
     * @return null | string
     * @author lyz
     */
    public function privateKeyEncrypt(string $encryptString): ?string
    {
        $encryptedString = '';
        foreach (str_split($encryptString, 117) as $v) {
            openssl_private_encrypt($v, $encrypted, $this->pi_key);
            $encryptedString = $encryptedString . $encrypted;
        }
        return base64_encode($encryptedString);
    }

    /**
     * 公钥解密
     *
     * @param string $decryptString
     * @return string|null
     * @author lyz
     * @time 2023-06-28 11:40:14
     */
    public function publicKeyDecrypt(string $decryptString): ?string
    {
        $decrypted = '';
        $decryptString = base64_decode($decryptString);
        foreach (str_split($decryptString, 128) as $v) {
            openssl_public_decrypt($v, $decryptedItem, $this->pu_key);
            $decrypted .= $decryptedItem;
        }
        return $decrypted;
    }

    /**
     * 公钥加密
     *
     * @param $encryptString
     * @return string
     * @author lyz
     */
    public function publicKeyEncrypt($encryptString): string
    {
        $encryptedString = '';

        foreach (str_split($encryptString, 117) as $v) {
            var_dump($v);
            openssl_public_encrypt($v, $encrypted, $this->pu_key);
            $encryptedString = $encryptedString . $encrypted;
        }
        return base64_encode($encryptedString);
    }


    /**
     * 私钥解密
     *
     * @param string $decryptString
     * @return string|null
     * @author lyz
     */
    public function privateKeyDecrypt(string $decryptString): ?string
    {
        $decrypted = '';
        $decryptString = base64_decode($decryptString);
        foreach (str_split($decryptString, 128) as $v) {
            openssl_private_decrypt($v, $decryptedItem, $this->pi_key);
            $decrypted .= $decryptedItem;
        }
        return $decrypted;
    }


    /**
     * 构造签名
     *
     * @param string $dataString 被签名数据
     * @return string
     * @author lyz
     */
    public function sign(string $dataString): string
    {
        $signature = false;
        openssl_sign($dataString, $signature, $this->pi_key);
        return base64_encode($signature);
    }


    /**
     * 验签
     *
     * @param string $dataString
     * @param string $signString
     * @return int
     * @author lyz
     * @time 2023-06-28 11:39:38
     */
    public function verify(string $dataString, string $signString): int
    {
        return openssl_verify($dataString, base64_decode($signString), $this->pu_key);
    }


    /**
     * 根据私钥生成公钥代码
     * @desc 使用openssl_pkey_get_details函数来从私钥中获取公钥的信息。这个函数会返回一个数组,其中包含了公钥的类型、位数、指数、模数等信息。
     * 我们可以使用数组中的key元素来获取公钥的PEM格式的字符串,也可以使用openssl_pkey_get_public函数来获取公钥的资源类型的值
     * @ps 在PHP中,我们不能直接从RSA公钥生成RSA私钥,因为这样会破坏RSA的安全性。
     * RSA的基本原理是,公钥和私钥是一对互相匹配的密钥,它们之间有数学上的联系,但是很难从一个密钥推导出另一个密钥。
     * 如果我们可以从公钥生成私钥,那么任何拥有公钥的人都可以解密我们用私钥加密的数据,这样就失去了加密的意义。
     * @author lyz
     *
     *
     */
    public function getPublicKeyByPrivateKey()
    {
        //从文件中读取私钥
        $pem_private_key = file_get_contents('rsa_private_key.pem');
        $private_key = openssl_pkey_get_private($pem_private_key);
        //从私钥中获取公钥的信息
        $details = openssl_pkey_get_details($private_key);
        // 获取公钥的PEM格式的字符串
        $pem_public_key = $details['key'];
        //获取公钥的资源类型的值
        $public_key = openssl_pkey_get_public($pem_public_key);
        $this->pu_key = $public_key;
    }
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值