前后端对称加密传输
JSEncrypt
Git地址:https://github.com/travist/jsencrypt
首先这个包 需要提前生成 公钥 私钥 和 依赖 openssl
注意:
明文长度最大为公钥长度-11,假如我的公钥长度是128,那明文最长也就117
如果需要加密解密长文本,请看下一种前后端对称加解密方式
这个库是与openssl一起工作
如何使用这个库:
openssl genrsa -out rsa_1024_priv.pem 1024
cat rsa_1024_priv.pem
然后,您可以将其复制并粘贴到index.html中的私钥部分。
接下来,您可以通过执行以下命令获取公钥。
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
cat rsa_1024_pub.pem
前端加密解密的写法如下:
对应PHP后台加密解密的写法如下:
class Encrypt
{
const JS_ENCRYPT = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN
FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76
xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4
gwQco1KRMDSmXSMkDwIDAQAB
-----END PUBLIC KEY-----';
const JS_DECRYPT = '-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ
WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR
aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB
AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv
xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH
m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd
8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF
z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5
rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM
V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe
aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil
psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz
uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876
-----END RSA PRIVATE KEY-----';
/**
* 解密
*
* @param string $encryptString
*
* @return bool|string
*/
public static function privateDecrypt($encryptString = '')
{
$decrypted = '';
$privateKey = self::JS_DECRYPT;
openssl_private_decrypt(base64_decode($encryptString), $decrypted, $privateKey);
return $decrypted;
}
/**
* 加密
*
* @param string $data
*
* @return bool|string
*/
public static function publicEncrypt($data = '')
{
$encrypt_data = '';
openssl_public_encrypt($data, $encrypt_data, self::JS_ENCRYPT);
$encrypt_data = base64_encode($encrypt_data);
return $encrypt_data;
}
}
如果出现了以下情况
opensll_public_encrypt() :key parameter is not a valid public key in /www/xxxxxx.php on line 123
class Encrypt
{
public function publicEncrypt()
{
$password = "habx-2018";
$key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB";
$key_eol = (string) implode("\n", str_split((string) $key, 64));
$publicKey = (string) "-----BEGIN PUBLIC KEY-----\n" . $key_eol . "\n-----END PUBLIC KEY-----";
openssl_public_encrypt($password, $encryptData, $publicKey, OPENSSL_PKCS1_PADDING);
$encryptData = base64_encode($encryptData);
print_r("encryptData=============" . $encryptData);
return $encryptData;
}
}
(new Encrypt())->publicEncrypt();
使用
$key_eol = (string) implode("\n", str_split((string) $key, 64));
$publicKey = (string) "-----BEGIN PUBLIC KEY-----\n" . $key_eol . "\n-----END PUBLIC KEY-----";
把KEY按照64位增加一个 \n的方式处理
CryptoJS
Git地址:https://github.com/brix/crypto-js
文档地址:https://code.google.com/archive/p/crypto-js/
注意:
明文长度没有限制但是加密的文本也会随之变长
这个库也是与openssl一起工作。
前端加密解密的写法如下:
对应PHP后台加密解密的写法如下:
class AesEncrypt
{
const AES_KEY = "qq3217834abcdefg"; //16位
const AES_IV = "1234567890123456"; //16位
public static function aes_decrypt($str)
{
$decrypted = openssl_decrypt(base64_decode($str), 'aes-128-cbc', self::AES_KEY, OPENSSL_RAW_DATA, self::AES_IV);
return $decrypted;
}
public static function aes_encrypt($plain_text)
{
$encrypted_data = openssl_encrypt($plain_text, 'aes-128-cbc', self::AES_KEY, OPENSSL_RAW_DATA, self::AES_IV);
return base64_encode($encrypted_data);
}
}