前端(JS)与后端(PHP) 通过 RSA 实现加解密

2022-12-29 18:11:25 浏览数 (4)

在项目开发过程中,为了安全起见,通常不能以明文形式传递敏感信息,容易被截获,所以引入RSA。
1. 生成公钥和私钥
  • 在终端(基于 Unix 的操作系统)中输入以下内容。生成私钥
代码语言:javascript复制
openssl genrsa -out rsa_1024_priv.pem 1024
  • 通过执行以下命令获取公钥
代码语言:javascript复制
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
  • 可以使用 cat 命令来查看
代码语言:javascript复制
cat rsa_1024_priv.pem         //查看私钥
cat rsa_1024_pub.pem          //查看公钥
  • 前端代码:
代码语言:javascript复制
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="bin/jsencrypt.min.js"></script>
<script type="text/javascript">
$(function () {
    //被加密的信息,这里一般是后端传递的接口参数
    var data = 'date';

    //公钥,这里使用自己生成的公钥
    var pub_key = '-----BEGIN PUBLIC KEY-----n'  
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFUEBi3D1Q8yFco32wWI/se4lAn'  
'ftsCNi71rgkLNsOhIR3ZOGywKsKjoZ7pewG6PDvISEodkZxVDlV401VP8sIPFP1dn'  
'fwdNEZQRHugDiuLIVRdkGWh0loviWWFJuzvJuOFxxyv9IyLt2Whvvoy3y8T9Hntln'  
'tgmIbwW9wMA8U1XP9wIDAQABn'  
'-----END PUBLIC KEY-----';

    //私钥,这里使用自己生成的私钥
    var private_key = '-----BEGIN RSA PRIVATE KEY-----n'  
'MIICXgIBAAKBgQDFUEBi3D1Q8yFco32wWI/se4lAftsCNi71rgkLNsOhIR3ZOGywn'  
'KsKjoZ7pewG6PDvISEodkZxVDlV401VP8sIPFP1dfwdNEZQRHugDiuLIVRdkGWh0n'  
'loviWWFJuzvJuOFxxyv9IyLt2Whvvoy3y8T9HntltgmIbwW9wMA8U1XP9wIDAQABn'  
'AoGBALoZOuEDQIG9DtZFDSLqJLSyv30pKH8pg5kMkFMXT QFTBW4diqh6fpTUYJ9n'  
'FeAFN41rQRwqXfyLDGptSKWjHQtcBDzpYil CrFnFFIzADgUtzfKVnfMwgO3X5yxn'  
'j akB9Pm er1F9t2qh7OwpbiWXo2ThF5Sfn7PjvdVswmOhsBAkEA7MecQO PIGzrn'  
'wwwRLadSyT fIt8dYxdrZ Hjc ff9uo3gAM/IBj08fIHLHf8WOaEwz3G0l944xbmn'  
'GtcqO9XllwJBANVUg8ZOIkzY0o2EmoP8zxmjIs4WsUrF2msp7vI9kHLeIUBLCwyvn'  
'fAh0x2SRQTfbr0ZZb7593VyvUGbuP/F4dKECQQCHmZkmpwr0quwp 5Mq7hQ/JKxin'  
'MByITzU3fOnhyv08Ts4SBcn0 9XraYwqL1zZHMsTdSGUAREeHFMTM/3/3nNpAkAln'  
'jeA4u658GrtOIGbXD10h5T8OSp5hTF07 kDPMhlIr2PpHozOaeIK1wv23z885xZ n'  
'6uWqAhxC4bXkuNJW3t6hAkEApKMMfLafxv3S8u8ilRku8JSegPLY325GB2vWHC8Hn'  
'jY Hv3qF9xcP5cbj43KPF0wWhKCECVSvPaMiwUbJzgu9JQ==n'  
'-----END RSA PRIVATE KEY-----';

    //new JSEncrypt
    var js_encrypt = new JSEncrypt();//必须引入jsencrypt.mim.js 文件,否则会报错
    //注意:如果加密就使用公钥,解密就使用私钥,按自己的需求
    //初始化公钥
    js_encrypt.setPublicKey(pub_key);
    //初始化私钥
    js_encrypt.setPrivateKey(private_key);

    //通过 公钥 加密
    var encrypted = js_encrypt.encrypt(data);
    console.log(encrypted);

    //通过 私钥 解密
    var uncrypted = js_encrypt.decrypt(encrypted);
    console.log(uncrypted);
});
</script>
  • 后端代码
代码语言:javascript复制
class Encrypt
{
	//公钥和秘钥
    const PUBKEY= '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFUEBi3D1Q8yFco32wWI/se4lA
ftsCNi71rgkLNsOhIR3ZOGywKsKjoZ7pewG6PDvISEodkZxVDlV401VP8sIPFP1d
fwdNEZQRHugDiuLIVRdkGWh0loviWWFJuzvJuOFxxyv9IyLt2Whvvoy3y8T9Hntl
tgmIbwW9wMA8U1XP9wIDAQAB
-----END PUBLIC KEY-----';

    const PRIVATEKEY = '-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDFUEBi3D1Q8yFco32wWI/se4lAftsCNi71rgkLNsOhIR3ZOGyw
KsKjoZ7pewG6PDvISEodkZxVDlV401VP8sIPFP1dfwdNEZQRHugDiuLIVRdkGWh0
loviWWFJuzvJuOFxxyv9IyLt2Whvvoy3y8T9HntltgmIbwW9wMA8U1XP9wIDAQAB
AoGBALoZOuEDQIG9DtZFDSLqJLSyv30pKH8pg5kMkFMXT QFTBW4diqh6fpTUYJ9
FeAFN41rQRwqXfyLDGptSKWjHQtcBDzpYil CrFnFFIzADgUtzfKVnfMwgO3X5yx
j akB9Pm er1F9t2qh7OwpbiWXo2ThF5Sfn7PjvdVswmOhsBAkEA7MecQO PIGzr
wwwRLadSyT fIt8dYxdrZ Hjc ff9uo3gAM/IBj08fIHLHf8WOaEwz3G0l944xbm
GtcqO9XllwJBANVUg8ZOIkzY0o2EmoP8zxmjIs4WsUrF2msp7vI9kHLeIUBLCwyv
fAh0x2SRQTfbr0ZZb7593VyvUGbuP/F4dKECQQCHmZkmpwr0quwp 5Mq7hQ/JKxi
MByITzU3fOnhyv08Ts4SBcn0 9XraYwqL1zZHMsTdSGUAREeHFMTM/3/3nNpAkAl
jeA4u658GrtOIGbXD10h5T8OSp5hTF07 kDPMhlIr2PpHozOaeIK1wv23z885xZ 
6uWqAhxC4bXkuNJW3t6hAkEApKMMfLafxv3S8u8ilRku8JSegPLY325GB2vWHC8H
jY Hv3qF9xcP5cbj43KPF0wWhKCECVSvPaMiwUbJzgu9JQ==
-----END RSA PRIVATE KEY-----';

    /**
     * 解密
     * @param string $encryptString
     * @return bool|string
     */
    public static function privateDecrypt($encryptString = '')
    {
        $decrypted  = '';
        openssl_private_decrypt(base64_decode($encryptString), $decrypted, self::PRIVATEKEY);
        return $decrypted;
    }

    /**
     * 加密
     * @param string $data
     * @return bool|string
     */
    public static function publicEncrypt($data = '')
    {
        $encrypt_data = '';
        openssl_public_encrypt($data, $encrypt_data, self::PUBKEY);
        $encrypt_data = base64_encode($encrypt_data);
        return $encrypt_data;
    }
}
  • 然后在此基础上根据自己需求进行相关功能实现。 Github 地址:jsencrypt.min.js

1 人点赞