欢迎各位兄弟 发布技术文章
这里的技术是共享的
apache提供工具open-ssh可以生产秘钥
私钥生成方式
openssl genrsa -out rsa_private.pem 1024
find / -name rsa_private.pem
直接登录服务器生成
生成公钥文件 需要在私钥的基础上生成
openssl rsa -in rsa_private.pem -pubout -out rsa_public_key.pem
AES.php
采用公钥加密,私钥解密
每次密文都在变化但是,解密的内容不变化
非对称加密比对称加密要慢一些
- <?php
- /**
- * Created by PhpStorm.
- * User: lj
- * Date: 2018/10/21
- * Time: 9:36 PM
- */
- //PULIC KEY
- $public_key='-----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC42exbLrawxAOUoHb4DHr1dsfO
- IIV3G5ozgjw/fhaiWE2tTO1kHu3irkY5/tVH/u4qT1r8yhyAmFFFmJ5aYqogFSrJ
- h8oUjy/D+GK88cfEUPGyuSyzH9Smn7mzN9jDsEigC59VSpH4oJkSSNYuP4M5/cq4
- BEsrskCQXDT3COU0gwIDAQAB
- -----END PUBLIC KEY-----';
- //private_key
- $private_key='-----BEGIN RSA PRIVATE KEY-----
- MIICXQIBAAKBgQC42exbLrawxAOUoHb4DHr1dsfOIIV3G5ozgjw/fhaiWE2tTO1k
- Hu3irkY5/tVH/u4qT1r8yhyAmFFFmJ5aYqogFSrJh8oUjy/D+GK88cfEUPGyuSyz
- H9Smn7mzN9jDsEigC59VSpH4oJkSSNYuP4M5/cq4BEsrskCQXDT3COU0gwIDAQAB
- AoGBAKxYSl0NZbnTV39ISQOmJzIbB9kwSx+s9Y1WvLN57MJ6UBnm4mJ5z9OMJt/F
- r7cLgaSjDjMogYCIAyY/UEz3NqNS+pnjwMiHVePecQ3tJzVVSlvzhn0QZmgYbOi1
- 1+iRaJTvjuFz1R/CDx4RwUsPPYj3j3tNVda9StPb2v19KoXJAkEA4sVVAi95wQtm
- pkgL/j4QPX097lB88SWcpqCOGUrMzP6Yf3eWaFyONKkBdwPUpyLNCBYQw4FrSf5G
- 6uMzfL1zVQJBANCtY0R29C2krybJrs8+D75OSZskeOLdrRBq85PrtL6bsmNchto8
- ztacYqXs29DKvcG5DIFKkxVGwsN6n8EyOHcCQHaKFH/J9Wk13ni5XJuQMckSuNbk
- eAtOT+7CKsWJ1jiqNWkcSCFRkHxyg/OEqxqbFHZPon+wHTdWlheeoIFqPukCQQDL
- 2nX87g+/fv+z7pYc0k3i5Ou26TxGs7NbNH/a4wngoozzz/HUxE5zDLa5zNc38rbI
- xpNWwplI0aV1/RaPTxYlAkBSAaXvdZ4gvgBR/aRdSQrgVZIctjq3IfnAoDg5TozX
- 3vr3itJRXLftGUSmSwmshJ0CO2drjNfOzw+RQxlk9JtQ
- -----END RSA PRIVATE KEY-----';
- $data='我在操作加密啦啦啦啦';
- //使用公钥加密
- openssl_public_encrypt($data, $encrypt_str, $public_key);
- $encrypt_str=base64_encode($encrypt_str);
- echo '加密后的密文采用64位编码后是:'.$encrypt_str;
- echo'<br>';
- //使用私钥解密
- openssl_private_decrypt(base64_decode($encrypt_str),$decrpt_str,$private_key);
- echo '解密后的数据是:'.$decrpt_str;
- echo'<br>';
代码可以使用私钥加密。公钥解密
下面是使用私钥加密,公钥解密,发现密文不会变化,至于为什么公钥加密私钥解密密文会变化,而私钥加密公钥解密不变化跟RSA算法有关,需要去看相关专业的资料
- //PULIC KEY
- $public_key='-----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC42exbLrawxAOUoHb4DHr1dsfO
- IIV3G5ozgjw/fhaiWE2tTO1kHu3irkY5/tVH/u4qT1r8yhyAmFFFmJ5aYqogFSrJ
- h8oUjy/D+GK88cfEUPGyuSyzH9Smn7mzN9jDsEigC59VSpH4oJkSSNYuP4M5/cq4
- BEsrskCQXDT3COU0gwIDAQAB
- -----END PUBLIC KEY-----';
- //private_key
- $private_key='-----BEGIN RSA PRIVATE KEY-----
- MIICXQIBAAKBgQC42exbLrawxAOUoHb4DHr1dsfOIIV3G5ozgjw/fhaiWE2tTO1k
- Hu3irkY5/tVH/u4qT1r8yhyAmFFFmJ5aYqogFSrJh8oUjy/D+GK88cfEUPGyuSyz
- H9Smn7mzN9jDsEigC59VSpH4oJkSSNYuP4M5/cq4BEsrskCQXDT3COU0gwIDAQAB
- AoGBAKxYSl0NZbnTV39ISQOmJzIbB9kwSx+s9Y1WvLN57MJ6UBnm4mJ5z9OMJt/F
- r7cLgaSjDjMogYCIAyY/UEz3NqNS+pnjwMiHVePecQ3tJzVVSlvzhn0QZmgYbOi1
- 1+iRaJTvjuFz1R/CDx4RwUsPPYj3j3tNVda9StPb2v19KoXJAkEA4sVVAi95wQtm
- pkgL/j4QPX097lB88SWcpqCOGUrMzP6Yf3eWaFyONKkBdwPUpyLNCBYQw4FrSf5G
- 6uMzfL1zVQJBANCtY0R29C2krybJrs8+D75OSZskeOLdrRBq85PrtL6bsmNchto8
- ztacYqXs29DKvcG5DIFKkxVGwsN6n8EyOHcCQHaKFH/J9Wk13ni5XJuQMckSuNbk
- eAtOT+7CKsWJ1jiqNWkcSCFRkHxyg/OEqxqbFHZPon+wHTdWlheeoIFqPukCQQDL
- 2nX87g+/fv+z7pYc0k3i5Ou26TxGs7NbNH/a4wngoozzz/HUxE5zDLa5zNc38rbI
- xpNWwplI0aV1/RaPTxYlAkBSAaXvdZ4gvgBR/aRdSQrgVZIctjq3IfnAoDg5TozX
- 3vr3itJRXLftGUSmSwmshJ0CO2drjNfOzw+RQxlk9JtQ
- -----END RSA PRIVATE KEY-----';
- $data='我在操作加密啦啦啦啦';
- //使用私钥加密
- openssl_private_encrypt($data, $encrypt_str, $private_key);
- $encrypt_str=base64_encode($encrypt_str);
- echo '私钥加密后的密文采用64位编码后是:'.$encrypt_str;
- echo'<br>';
- //使用公钥解密
- openssl_public_decrypt(base64_decode($encrypt_str),$decrpt_str,$public_key);
- echo '公钥解密后的数据是:'.$decrpt_str;
- echo'<br>';
如果觉得写私钥 公钥在代码里太多了 也可以直接使用file_get_content把文件读取过来
最后把代码封装成一个类
- $str='家世界阿加莎';
- $rsa=new RSA();
- $encrypt_str=$rsa->encrypt($str);
- echo '加密后的数据是:'.$encrypt_str;
- $decrpt_str=$rsa->decrypt($encrypt_str);
- echo '<br>';
- echo '解密后的数据是:'.$decrpt_str;
- class RSA{
- public function __construct()
- {
- //PULIC KEY
- $this->public_key='-----BEGIN PUBLIC KEY-----
- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC42exbLrawxAOUoHb4DHr1dsfO
- IIV3G5ozgjw/fhaiWE2tTO1kHu3irkY5/tVH/u4qT1r8yhyAmFFFmJ5aYqogFSrJ
- h8oUjy/D+GK88cfEUPGyuSyzH9Smn7mzN9jDsEigC59VSpH4oJkSSNYuP4M5/cq4
- BEsrskCQXDT3COU0gwIDAQAB
- -----END PUBLIC KEY-----';
- //private_key
- $this->private_key='-----BEGIN RSA PRIVATE KEY-----
- MIICXQIBAAKBgQC42exbLrawxAOUoHb4DHr1dsfOIIV3G5ozgjw/fhaiWE2tTO1k
- Hu3irkY5/tVH/u4qT1r8yhyAmFFFmJ5aYqogFSrJh8oUjy/D+GK88cfEUPGyuSyz
- H9Smn7mzN9jDsEigC59VSpH4oJkSSNYuP4M5/cq4BEsrskCQXDT3COU0gwIDAQAB
- AoGBAKxYSl0NZbnTV39ISQOmJzIbB9kwSx+s9Y1WvLN57MJ6UBnm4mJ5z9OMJt/F
- r7cLgaSjDjMogYCIAyY/UEz3NqNS+pnjwMiHVePecQ3tJzVVSlvzhn0QZmgYbOi1
- 1+iRaJTvjuFz1R/CDx4RwUsPPYj3j3tNVda9StPb2v19KoXJAkEA4sVVAi95wQtm
- pkgL/j4QPX097lB88SWcpqCOGUrMzP6Yf3eWaFyONKkBdwPUpyLNCBYQw4FrSf5G
- 6uMzfL1zVQJBANCtY0R29C2krybJrs8+D75OSZskeOLdrRBq85PrtL6bsmNchto8
- ztacYqXs29DKvcG5DIFKkxVGwsN6n8EyOHcCQHaKFH/J9Wk13ni5XJuQMckSuNbk
- eAtOT+7CKsWJ1jiqNWkcSCFRkHxyg/OEqxqbFHZPon+wHTdWlheeoIFqPukCQQDL
- 2nX87g+/fv+z7pYc0k3i5Ou26TxGs7NbNH/a4wngoozzz/HUxE5zDLa5zNc38rbI
- xpNWwplI0aV1/RaPTxYlAkBSAaXvdZ4gvgBR/aRdSQrgVZIctjq3IfnAoDg5TozX
- 3vr3itJRXLftGUSmSwmshJ0CO2drjNfOzw+RQxlk9JtQ
- -----END RSA PRIVATE KEY-----';
- }
- public function encrypt($data){
- openssl_public_encrypt($data, $encrypt_str, $this->public_key);
- return base64_encode($encrypt_str);
- }
- public function decrypt($data){
- $data=openssl_private_decrypt(base64_decode($data),$decrpt_str,$this->private_key);
- return $decrpt_str;
- }
- }
https就是使用的rsa加密方式
来自 http://www.pianshen.com/article/954391252/
来自 https://blog.csdn.net/resilient/article/details/83246000