本文实例讲述了php基于openssl的rsa加密解密。分享给大家供大家参考,具体如下:
"D:/phpserver/Lighttpd/openssl.cnf", //'config' =>'D:/phpStudy/Lighttpd/OpenSSL.cnf', 'private_key_bits' => 1024, // Size of Key. 'private_key_type' => OPENSSL_KEYTYPE_RSA ); //$res = openssl_pkey_new(); $res = openssl_pkey_new($config); // Get private key // openssl_pkey_export($res, $privkey, "PassPhrase number 1" ); openssl_pkey_export($res, $privkey); var_dump($privkey); // Get public key $pubkey=openssl_pkey_get_details($res); // echo "------------>
"; // print_r($pubkey["rsa"]); // $bin_str=$pubkey["rsa"]["n"]; // print_r($bin_str); // echo "
"; // //echo $bin_hex_str = pack("H*" , bin2hex($bin_str)); // echo $bin_hex_str = bin2hex($bin_str); // echo "
------------<
"; $pubkey=$pubkey["key"]; // var_dump($privkey); // var_dump($pubkey); echo $privkey."
"; echo $pubkey."
"; ?>
----------------------
"; $data = "woshizhu";//原始数据 $encrypted = ""; $decrypted = ""; echo "source data:",$data,"
"; echo "private key encrypt:n"; openssl_private_encrypt($data,$encrypted,$pi_key);//私钥加密 $encrypted = base64_encode($encrypted);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的 echo "
----------私钥加密------------
"; echo $encrypted,"
"; echo "
----------私钥加密------------
"; echo "public key decrypt:n"; $decrypted=""; openssl_public_decrypt(base64_decode($encrypted),$decrypted,$pu_key);//私钥加密的内容通过公钥可用解密出来 echo $decrypted,"n"; echo "---------------------------------------n"; echo "public key encrypt:n"; openssl_public_encrypt($data,$encrypted,$pu_key);//公钥加密 $encrypted = base64_encode($encrypted); echo $encrypted,"n"; $decrypted=""; //$encrypted="JBeapcp9iWWYJYElgqtrZxfxM4wVkCaSn/oJZ7NjfR23o76fdbxEXpf+PGWACw3PeTdObwL4108wR3ihKmy2iYkIExGjBYyvx2w9aHies8ZsOIP3LjiMHYTm93Rr8Sc5XxHWQc3Dhbq16JWHYZ2d+RrOpHd4x84GF3JXwivrGO4="; echo "private key decrypt:n"; openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);//私钥解密 echo "--->:".$decrypted,"n"; ?>
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
密码安全性在线检测:
http://tools.jb51.net/password/my_password_safe
高强度密码生成器:
http://tools.jb51.net/password/CreateStrongPassword
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password
迅雷、快车、旋风URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder
在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
更多关于PHP相关内容可查看本站专题:《php加密方法总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。



