[PHP]:RSA非对称加密

[PHP]:RSA非对称加密

一、私钥和公钥

在线生成网站,或者在linux下生成。

网站生成:https://www.bm8.com.cn/webtool/rsa/

 

rsa_private_key.pem

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgFwZXG9epV7XJ3H4/PO0XMkNkoQenSpl1lAkf1eJABkD8ommOynD
aQQul06bcSzZ5lIYC5ai+0Wzb9JoNJE6AqnF+6T3mvf2sKSs127p16OQnDhpnTgX
WT70AyA6jYq5PSr+RSXxE5UAKVC8ZqvfoNBOMk8TaYMSEupiDaHDMMHTAgMBAAEC
gYBDrMsUdwqzVwwAHsLY3Ta08gkwlkJMrko916PPEbeFqjXlWcXtmU6oqQe71lLi
bzdkKZESXq6e+Ax6IuJFuMfNTc7jSdqzQ9LNdMyiUSpZ32lUUk3anxieF+7Y0bVl
jLQxqD5QSFSw6qmrpPX8N5fDa5W4EWovazyvzRsdQTR1MQJBALZn0nkTQPT437h7
MScXcE2c70nlg5HbzZ5c1G0MrjJgMOoxKJNEMG42LeppOJTXCsTztzb5LYrKKvcc
Su7HnssCQQCBQgZ4OblXyxSom4xY8ad/AgJCTAEmrGyNd6OtfVXpG8nFJabcqMw6
q1/ax34YrMMvQsCLFmkSrEs+gdcyjsAZAkEAiYlvYmvqJs0ho/D/9wHtZBhZNdch
hfG+8RH9+tv83W86ctvuRFy+R9wCcyYzy8b6D9fU5lasyGH4U6u5tKJAUQJATNmp
qQ+A7/7RUJHy9urukCfsmehAgvLfM5kfWsX7LzaTV0DUnbtKedNskVIs2ZTWE6JX
ghqX7D8+GXLvNKME+QJBAI3mpqz95ZMjFuLIupB8+iQrcrOoGnEepet7XtE3Fejg
IYuT1BPKk53cXRiM2rD0aff41nY/SvZnTNIQepiOSxQ=
-----END RSA PRIVATE KEY-----

 

rsa_public_key.pem

-----BEGIN PUBLIC KEY-----
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgFwZXG9epV7XJ3H4/PO0XMkNkoQe
nSpl1lAkf1eJABkD8ommOynDaQQul06bcSzZ5lIYC5ai+0Wzb9JoNJE6AqnF+6T3
mvf2sKSs127p16OQnDhpnTgXWT70AyA6jYq5PSr+RSXxE5UAKVC8ZqvfoNBOMk8T
aYMSEupiDaHDMMHTAgMBAAE=
-----END PUBLIC KEY-----

 

加密类Rsa.php

<?php


class Rsa {

    /**
     * 获取私钥
     * @return bool|resource
     */
    private static function getPrivateKey()
    {
        $abs_path = dirname(__FILE__) . '/rsa_private_key.pem';
        $content = file_get_contents($abs_path);
        return openssl_pkey_get_private($content);
    }

    /**
     * 获取公钥
     * @return bool|resource
     */
    private static function getPublicKey()
    {
        $abs_path = dirname(__FILE__) . '/rsa_public_key.pem';
        $content = file_get_contents($abs_path);
        return openssl_pkey_get_public($content);
    }

    /**
     * 私钥加密
     * @param string $data
     * @return null|string
     */
    public static function privEncrypt($data = '')
    {
        if (!is_string($data)) {
            return null;
        }
        return openssl_private_encrypt($data,$encrypted,self::getPrivateKey()) ? base64_encode($encrypted) : null;
    }

    /**
     * 公钥加密
     * @param string $data
     * @return null|string
     */
    public static function publicEncrypt($data = '')
    {
        if (!is_string($data)) {
            return null;
        }
        return openssl_public_encrypt($data,$encrypted,self::getPublicKey()) ? base64_encode($encrypted) : null;
    }

    /**
     * 私钥解密
     * @param string $encrypted
     * @return null
     */
    public static function privDecrypt($encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;
    }

    /**
     * 公钥解密
     * @param string $encrypted
     * @return null
     */
    public static function publicDecrypt($encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey())) ? $decrypted : null;
    }

}

 

 使用类index.php

<?php

require_once "Rsa.php";
$rsa = new Rsa();
$data['name'] = 'Tom';
$data['age']  = '20';
$privEncrypt = $rsa->privEncrypt(json_encode($data));
echo '私钥加密后:'.$privEncrypt.'<br>'."\n";

$publicDecrypt = $rsa->publicDecrypt($privEncrypt);
echo '公钥解密后:'.$publicDecrypt.'<br>'."\n";

$publicEncrypt = $rsa->publicEncrypt(json_encode($data));
echo '公钥加密后:'.$publicEncrypt.'<br>'."\n";

$privDecrypt = $rsa->privDecrypt($publicEncrypt);
echo '私钥解密后:'.$privDecrypt.'<br>';

 

 

相关原理和使用参考:

https://www.cnblogs.com/xuweiqiang/p/9784584.html

 

posted @ 2020-08-12 22:47  wukong1688  阅读(350)  评论(0编辑  收藏  举报