https://www.cnblogs.com/sixiong/p/5885111.html
原理:javascript加密PHP解密; 完全依赖openssl;
- openssl 是干嘛的
它集成了众多密码算法及实用工具
rsa加密流程;(今天只讲众多加密方式中的一种)
1. 在当前文件夹下生成一个包含 "公钥"和"私钥" 两部分内容的文本文件; 命名test.key
[[email protected] openssl]#openssl genrsa -out test.key 1024
2.将这个文本文件中的“公钥”提取出来: 命名test_pub.key
[[email protected] openssl]#openssl rsa -in test.key -pubout -out test_pub.key
3.创建一个hello.txt的文本文件,然后利用此前生成的公钥加密文件;
[[email protected] openssl]#echo "1234561122" > ./hello.txt
[[email protected] openssl]#openssl rsautl -encrypt -in hello.txt -inkey test_pub.key -pubin -out hello.en.txt
4.解密文件
[[email protected] openssl]#openssl rsautl -decrypt -in hello.en.txt -inkey test.key -out hello.de.txt
5.解析后的结果
[[email protected] openssl]# cat hello.de.txt
二. jsencrypt.js定义了一个JSEncrypt方法 可以去git搜一下 对于此篇文档就不展开太多
使用步骤
var res = new JSEncrypt
res.setPublicKey('---这里就填写test_pub.key文件中的字符串内容---'); //设置公有key
var temp = res.encrypt("string");//利用刚设好的key 对明文进行加密;
var data = encodeURI(temp).replace(/\+/g, '%2B'), //+号的处理: 加密完成后就可以ajax传送了
下载地址: http://files.cnblogs.com/files/sixiong/openssl.zip
github主页:https://github.com/travist/jsencrypt
demo:http://www.cnblogs.com/hayha/p/6207387.html
test.html:
<textarea id="pubkey" rows="15" cols="65">-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBWNoG5LJ3u44Gs8PWs1MaNUQQ
+mOmh+9zWdzSt3ORbmfCDvU+ssW/6QTTgXvWWx7+Wzq/a4fCCQp72zSqXeVhWkTV
ct9Hyp/iMo5K6qOEK76z9z+tP/u99X6qazeXGVMWKkPiyZT4mKAGd/U8Mph9Z1Z5
kOluA7g7heq8PPlE9wIDAQAB
-----END PUBLIC KEY-----</textarea>
test.php:
<?php
// 接收客户端发送过来的经过加密的登录信息
$input = $_POST;
// 私钥是放在服务器端的,用以验证和解密客户端经过公钥加密后的信息
$private_key = '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCBWNoG5LJ3u44Gs8PWs1MaNUQQ+mOmh+9zWdzSt3ORbmfCDvU+
ssW/6QTTgXvWWx7+Wzq/a4fCCQp72zSqXeVhWkTVct9Hyp/iMo5K6qOEK76z9z+t
P/u99X6qazeXGVMWKkPiyZT4mKAGd/U8Mph9Z1Z5kOluA7g7heq8PPlE9wIDAQAB
AoGABPQwNX9gznEieWM9JuXrUt+jYbsVQfWG2DYi3Pclt/YwhyAniGU0aas1Ahy9
b3JB95/q2hX2Nxo9iozUsYmzFT99dm2HBsHDnpnUgpyDtGo9sXlhLktyey53UKRx
QJkW5dWWUQfssNrCe08N3vtLiDIy04lRQ8F0eJ/iklzk1HECQQC22pOz7V2K5/50
w9LA9UBSl7wWhTTY5G1gsBEm5tNmbM/ZqCJ1FXB4uuDgz0o0N0x8T8JkkPrRWH5q
GIHFRswVAkEAtRbV8PoLnyT73hxtCw0F17aaI8W5AGhvsbjdA6nMo6byBR5xKN+7
lalfXYEfXPnStHVNSnQVFjN3T06iJV6z2wJBAKj51rLYcLBT8XbQG+vK+FUa+WrK
UGr6tQU7z63mc4dcmLtoP+d5F4XKFNRLWyRj0d+zCU5MGCzrnW7IFOxMn30CQEtv
4N3K/C5mtLmZM9+npChxfBKs2l2OJAFwFjnhcUs3T5jMTq2NTlKRRRXppUwREjJ0
ryb15pbiB7C0/Bz/L4MCQC1AOKKjnqQEpINatjZLkyay0bXBih9GXovz3T1eAxaS
QOEzIC+hGjX+2x1z5jUwwKCgjVUaZdrx470SMJM2Js8=
-----END RSA PRIVATE KEY-----';
// 公钥一般存放在登录页面中的一个隐藏域中,但是请注意:公钥和私钥一定要配对,且必须保证私钥的安全
$public_key = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBWNoG5LJ3u44Gs8PWs1MaNUQQ
+mOmh+9zWdzSt3ORbmfCDvU+ssW/6QTTgXvWWx7+Wzq/a4fCCQp72zSqXeVhWkTV
ct9Hyp/iMo5K6qOEK76z9z+tP/u99X6qazeXGVMWKkPiyZT4mKAGd/U8Mph9Z1Z5
kOluA7g7heq8PPlE9wIDAQAB
-----END PUBLIC KEY-----';
/**
* 使用PHP OpenSSL时,最好先看看手册,了解如何开启OpenSSL 和 其中的一些方法的使用
* 具体如何使用这里不做赘述,大家去看看PHP手册,什么都就解决了
*/
$pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$pu_key = openssl_pkey_get_public($public_key);//这个函数可用来判断公钥是否是可用的
$decrypted = "";
openssl_private_decrypt(base64_decode($input['username']),$decrypted,$pi_key);//私钥解密
// 这里的这个 $decrypted就是解密客户端发送过来的用户名,至于后续连接数据库验证登录信息的代码,这里也就省略了
echo json_encode($decrypted);
登录加密实例:
class Decrypt{
private static $private_key = '-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----';
private static $public_key = '-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----';
public static function decrypts($login_name,$password){
$pi_key = openssl_pkey_get_private(self::$private_key);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
$pu_key = openssl_pkey_get_public(self::$public_key);//这个函数可用来判断公钥是否是可用的
$decrypted_login_name = "";
$decrypted_password = "";
openssl_private_decrypt(base64_decode($login_name),$decrypted_login_name,$pi_key);//私钥解密
openssl_private_decrypt(base64_decode($password),$decrypted_password,$pi_key);//私钥解密
return ['login_name'=>$decrypted_login_name,'password'=>$decrypted_password];
}
}
$private_key 和$public_key是同时生成的
2018-12-11
2.利用PHP代码自动随机生成openssl公钥/秘钥对
//生成**
$opensslConfigPath = "D:\phpstudy\PHPTutorial\Apache\conf\openssl.cnf";
$config = array(
"digest_alg" => "sha512",
"private_key_bits" =>2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
'config'=> $opensslConfigPath
);
//创建**对
$res = openssl_pkey_new($config);
while($message = openssl_error_string()){
echo $message . '<br />' . PHP_EOL;
}
//当openssl_error_string出现错误时一般会报如下错误:
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib
error:02001003:system library:fopen:No such process
error:2006D080:BIO routines:BIO_new_file:no such file
error:0E064002:configuration file routines:CONF_load:system lib
openssl_csr_new,openssl_csr_sign,openssl_pkey_new 这3个方法调用都会有上述错误输出
解决:要确保Apache或nginx服务器下有openssl.conf并且可读
//生成私钥
openssl_pkey_export($res, $privkey, null, $config);
//生成公钥
$pubKey = openssl_pkey_get_details($res)['key'];
print_r($privkey);
echo '<div style="height:100px;background:red">1</div>';
print_r($pubKey);
file_put_contents('private.key',$privkey);
file_put_contents('public.key',$pubKey);
项目: