PHP之 进行rsa证书加密

调用 // 公钥加密随机生成的字符串(key)得到的加密值

        'check' => RSA256Util::encryptByPub($this->publicCertPath, $key)

类文件

<?php

require_once dirname(__FILE__) . '/CacheUtil.php';

class RSA256Util
{
    /**
     * @param $certPath string
     * @return string
     */
    public static function parseCertFileToPem($certPath)
    {
        return CacheUtil::getCacheValue($certPath, function () use ($certPath) {
            $certificateCAcerContent = file_get_contents($certPath);
            return '-----BEGIN CERTIFICATE-----' . PHP_EOL . chunk_split(base64_encode($certificateCAcerContent), 64, PHP_EOL) . '-----END CERTIFICATE-----' . PHP_EOL;
        });
    }

/**
 * 公钥加密
 */
public static function encryptByPub($pubCertPath, $data)     // pubCertPath 是证书文件地址
{
    //获取公钥
    $pu_key = openssl_pkey_get_public(self::parseCertFileToPem($pubCertPath));
    if (!$pu_key) {
        throw new RuntimeException("公钥不正确");
    }
    //公钥加密
    openssl_public_encrypt($data, $encrypted, $pu_key);
    openssl_free_key($pu_key);
    return base64_encode($encrypted);
}

public static function verifySign($pubCertPath, $sign, $srcData)
{
    //获取公钥
    $pu_key = openssl_pkey_get_public(self::parseCertFileToPem($pubCertPath));
    if (!$pu_key) {
        throw new RuntimeException("公钥不正确");
    }
    $success = openssl_verify($srcData, base64_decode($sign), $pu_key, OPENSSL_ALGO_SHA256);
    // Free the key from memory
    openssl_free_key($pu_key);
    if ($success === 1) {
        return true;
    }
    return false;
}


/**
 * @param $pfxPath string
 * @param $pfxPwd string
 * @return array
 */
private static function readPfxCertInfo($pfxPath, $pfxPwd)
{
    return CacheUtil::getCacheValue($pfxPath, function () use ($pfxPwd, $pfxPath) {
        $cert_store = file_get_contents($pfxPath);
        $status = openssl_pkcs12_read($cert_store, $cert_info, $pfxPwd);
        if (!$status) {
            throw new RuntimeException('Invalid pfxPwd');
        }
        return $cert_info;
    });
}

/**
 * pfx私钥文件签名
 */
public static function signWithPfx($data, $pfxPath, $pfxPwd)
{
    $cert_info = self::readPfxCertInfo($pfxPath, $pfxPwd);
    $private_key = $cert_info['pkey'];
    $pri_key = openssl_get_privatekey($private_key);
    if (!$pri_key) {
        throw new RuntimeException('Invalid private key from Pfx file ' . $pfxPath);
    }
    $status = openssl_sign($data, $signature, $pri_key, OPENSSL_ALGO_SHA256);
    // Free the key from memory
    openssl_free_key($pri_key);
    if (!$status) {
        throw new RuntimeException('Computing of the signature failed');
    }
    return base64_encode($signature);
}
}

CacheUtil.php

<?php

class CacheUtil
{
    private static $cacheMap = [];

    public static function getCacheValue($key, $func)
    {
        if (array_key_exists($key, self::$cacheMap)) {
            return self::$cacheMap[$key];
        }
        if (is_callable($func)) {
            return self::$cacheMap[$key] = call_user_func($func);
        }
        return null;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值