后台接口中 经常使用到加密算法 如何使用RSA 非对称加密 对数据进行加密传输
下载RSA 加密算法
php-Rsa算法下载
将下载好的RSA 算法 放入 到 TP6项目根目录下 extend 拓展目录中
// 实例化use \rsa\RsaCoding as Crypto;
使用系统自动生成的 32为加密进行加密解密
$plain = "Some secret here for you ...";$key = openssl_random_pseudo_bytes(32);$cipher = Crypto::encrypt($plain, $key);$msg = Crypto::decrypt($cipher, $key);print_r(['明文'=>$plain, '密码'=>base64_encode($key), '解密'=>$msg, '密文'=>$cipher]);
结果返回
Array ( [明文] => Some secret here for you ... [密码] => SpmMiuaPdPFqrjPd4mfAV9h7o5sL3DZrIHuR0YXofjA= [解密] => Some secret here for you ... [密文] => dQRB3s/Q7hN1BJr8Ow6McnA6cApWJ+knwgsn/27LUMupyQECJrDBE58b4NguNn7UvrxAY9JVEtSpZQeAJm+C5nh3EjI6+nOq8P1Tpjp8K0I= )
利用公钥加密,私钥解密做数据保密通信!
$plain = "利用公钥加密,私钥解密做数据保密通信!";$cipher = Crypto::publicEncrypt($plain);$msg = Crypto::privateDecrypt($cipher);print_r(['明文'=>$plain, '解密'=>$msg, '密文'=>$cipher]);
结果返回
Array ( [明文] => 利用公钥加密,私钥解密做数据保密通信! [解密] => 利用公钥加密,私钥解密做数据保密通信! [密文] => pOIXV66qa7mQ0t7ffToFmhRDodrC/ObqAODN7UibBy59pJMRN4elkMrO6yvp18jN23Yp+YThnuiPi53DtO9Ui7t7lvno8OoAsqzs4V/77H4bZKAr6u4skohRokPLT6mW2AkYAOYRpbW9fbAxu3/gfi3i6Nf4otT03pb6+MGZ1y/OF3ScwltmhXvYRZddfnLZbPjBlJX7FNlVzc+ApmIsZj/n76qG/gJl/CLn2K1A+mHrmMD25MiacDvHqLiaC2oe7lEC4uerWS913PMOmmS/PrPa3xQxCRuff0GJ5uAsxbSU+9iwjSxtaMOWWpv1nk0t6dYz2+aD5CWsGRnRFNGfZg== )
利用私钥加密,公钥解密可以做身份验证
$plain = "利用私钥加密,公钥解密可以做身份验证";$cipher = Crypto::privateEncrypt($plain);$msg = Crypto::publicDecrypt($cipher);print_r(['明文'=>$plain, '解密'=>$msg, '密文'=>$cipher]);
返回结果
Array ( [明文] => 利用私钥加密,公钥解密可以做身份验证 [解密] => 利用私钥加密,公钥解密可以做身份验证 [密文] => Z8B6Csq3MzwmZLObT3qyWqHOi5o27wDAEGuSwYzQS3eTIZPIb2+l0SuWKpZtcj5KLuEo/EFV3i8mitUI2Htm6kinl513dxBmpJTkLj1hu1NVs4CoUTE/mUe8UrKUEEleXZstrKzPsCqWOizMxAeE059fzg9NvhNzrFNc0L5lsiIOVD28H447Teo5Z9iCexVGG1LhxXpBWvwhpiVpt5wKfm/kX0wWzqS4e/BETNjh47Z6vBT6SoS1+YnDE5TN/PgWjdwA4X+p5CjzZR3tl3iI8tliPa5hkoH0LJ7f59lmBNPY7MK4X14/iMutbo6GaS7p5vvr5h252aX/oUNYeskG7Q== )
$msg = 'a=123';$sign = Crypto::sign($msg);$verify = Crypto::verify($msg, $sign);print_r(['预签'=>$msg, '签名'=>$sign, '验证'=>$verify==1?"PASS":"FAIL"]);
返回结果
Array ( [预签] => a=123 [签名] => X2N1TNCvVBfnFHrZZAS8Clm98e8UWJZ6eaJTGIR6Pz0X2kBjwMQ/AGMRxi90Ul6ysy664y85tBQtpYNbOEsWfkfzSId2NCJC1bfnMedsRLp3q1GajYq4eLgPm96WVUF1kakDIN/PA5jPo/WSE7YWXX4xBL0bwYRugB7IcaaeLvY9PqHuIhzZxhV7OfBLfPxCqHQVdmfBatRU5dxJqrJ+SfjeAYrJFLbd2K52PidQfctHilQOl6jx9k9lymGX7MvS/fvGRxlIIoE9HPOkKqYVHf3EOkqwZqQweIocjhblOcksjfCVr3hk4c7iWp9fnZm2rrgzVIZSg6mk2vr4QC+2nw== [验证] => PASS )
来源地址:https://blog.csdn.net/chinanes/article/details/127847299