前端(JS)與後端(PHP) 通過 RSA 實現加解密

在項目開發過程中,爲了安全起見,通常不能以明文形式傳遞敏感信息,容易被截獲,所以引入RSA。

1. 生成公鑰和私鑰
  • 在終端(基於 Unix 的操作系統)中輸入以下內容。生成私鑰
openssl genrsa -out rsa_1024_priv.pem 1024
  • 通過執行以下命令獲取公鑰
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
  • 可以使用 cat 命令來查看
cat rsa_1024_priv.pem         //查看私鑰
cat rsa_1024_pub.pem          //查看公鑰
  • 前端代碼:
<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/se4lA\n' +
'ftsCNi71rgkLNsOhIR3ZOGywKsKjoZ7pewG6PDvISEodkZxVDlV401VP8sIPFP1d\n' +
'fwdNEZQRHugDiuLIVRdkGWh0loviWWFJuzvJuOFxxyv9IyLt2Whvvoy3y8T9Hntl\n' +
'tgmIbwW9wMA8U1XP9wIDAQAB\n' +
'-----END PUBLIC KEY-----';

    //私鑰,這裏使用自己生成的私鑰
    var private_key = '-----BEGIN RSA PRIVATE KEY-----\n' +
'MIICXgIBAAKBgQDFUEBi3D1Q8yFco32wWI/se4lAftsCNi71rgkLNsOhIR3ZOGyw\n' +
'KsKjoZ7pewG6PDvISEodkZxVDlV401VP8sIPFP1dfwdNEZQRHugDiuLIVRdkGWh0\n' +
'loviWWFJuzvJuOFxxyv9IyLt2Whvvoy3y8T9HntltgmIbwW9wMA8U1XP9wIDAQAB\n' +
'AoGBALoZOuEDQIG9DtZFDSLqJLSyv30pKH8pg5kMkFMXT+QFTBW4diqh6fpTUYJ9\n' +
'FeAFN41rQRwqXfyLDGptSKWjHQtcBDzpYil+CrFnFFIzADgUtzfKVnfMwgO3X5yx\n' +
'j+akB9Pm+er1F9t2qh7OwpbiWXo2ThF5Sfn7PjvdVswmOhsBAkEA7MecQO+PIGzr\n' +
'wwwRLadSyT+fIt8dYxdrZ+Hjc+ff9uo3gAM/IBj08fIHLHf8WOaEwz3G0l944xbm\n' +
'GtcqO9XllwJBANVUg8ZOIkzY0o2EmoP8zxmjIs4WsUrF2msp7vI9kHLeIUBLCwyv\n' +
'fAh0x2SRQTfbr0ZZb7593VyvUGbuP/F4dKECQQCHmZkmpwr0quwp+5Mq7hQ/JKxi\n' +
'MByITzU3fOnhyv08Ts4SBcn0+9XraYwqL1zZHMsTdSGUAREeHFMTM/3/3nNpAkAl\n' +
'jeA4u658GrtOIGbXD10h5T8OSp5hTF07+kDPMhlIr2PpHozOaeIK1wv23z885xZ+\n' +
'6uWqAhxC4bXkuNJW3t6hAkEApKMMfLafxv3S8u8ilRku8JSegPLY325GB2vWHC8H\n' +
'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>
  • 後端代碼
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章