openssl 第一篇
自从老罗赞助了openssl以及心脏出血事件的新闻,得以了解了openssl。那么什么是openssl呢?下面摘自官网:
The OpenSSL Project is a collaborative effort to develop a robust, commercial-grade, full-featured, and Open Source toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS) protocols as well as a full-strength general purpose cryptography library. The project is managed by a worldwide community of volunteers that use the Internet to communicate, plan, and develop the OpenSSL toolkit and its related documentation.
简单地说,就是用来保证通信安全的。那么我们常用的php怎么使用呢?在网上找了一下,方法是在php.ini文件中添加extension=php_openssl.dll这个模块,然后将php_openssl.dll, ssleay32.dll, libeay32.dll 3个文件拷贝到 WINDOWS\system32\ 文件夹下。其实就是将这些dll放入环境变量path中去。
但是我一般都是使用集成的服务器套件的,比如phpnow,但是缺点是其没有默认打开这个模块,导致我按照这个步骤做依然不行;然后我在使用xampp的时候,就发现其是默认打开了Openssl的,所以相对是比较方便的。
然后当环境搭建好了,我们肯定就会迫不及待地去测试了。这里我给出网上找的两个例子:
1.使用了openssl_encrypt()这个函数,然后会在目录中产生一个加密后的文件,要解密这个文件,这个例子中使用的是命令行的方法。
<?php
function strtohex($x)
{
$s='';
foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
$source = 'It works !';
$iv = "1234567812345678";
$pass = '1234567812345678';
$method = 'aes-128-cbc';
echo "<br>iv in hex to use: ".strtohex ($iv);
echo "<br>key in hex to use: ".strtohex ($pass);
echo "<br>";
file_put_contents ('./file.encrypted',openssl_encrypt ($source, $method, $pass, true, $iv));
$exec = "openssl enc -".$method." -d -in file.encrypted -nosalt -nopad -K ".strtohex($pass)." -iv ".strtohex($iv);
echo 'executing: '.$exec."<br><br>";
echo exec ($exec);
echo "<br>";
?>
2.第二个例子使用openssl_pkey_new()这个函数产生了一个密钥对,然后利用公钥加密和私钥解密举例。
<?php if (isset($_SERVER['HTTPS']) )
{
echo "SECURE: This page is being accessed through a secure connection.<br><br>";
}
else
{
echo "UNSECURE: This page is being access through an unsecure connection.<br><br>";
} // Create the keypair
$res=openssl_pkey_new(); // Get private key
openssl_pkey_export($res, $privatekey); // Get public key
$publickey=openssl_pkey_get_details($res);
$publickey=$publickey["key"]; echo "Private Key:<BR>$privatekey<br><br>Public Key:<BR>$publickey<BR><BR>"; $cleartext = '1234 5678 9012 3456'; echo "Clear text:<br>$cleartext<BR><BR>"; openssl_public_encrypt($cleartext, $crypttext, $publickey); echo "Crypt text:<br>$crypttext<BR><BR>"; openssl_private_decrypt($crypttext, $decrypted, $privatekey); echo "Decrypted text:<BR>$decrypted<br><br>";
?>
但是这个例子我在机子上确没有正确地运行。首先是使用https协议访问就会出问题,第二个是openssl_pkey_new()这个函数没有办法产生密钥对。
使用openssl_error_string()查看出错的问题,error:02001003:system library:fopen:No such process,然后我将xampp/php这个路径加入到path环境变量中去。然后再查看,问题是:error:2006D080:BIO routines:BIO_new_file:no such file。
好像是openssl不能找到openssl.cnf这个文件。然后查看文档:
PHP will search for the openssl.cnf using the following logic:
- the OPENSSL_CONF environmental variable, if set, will be used as the path (including filename) of the configuration file.
- the SSLEAY_CONF environmental variable, if set, will be used as the path (including filename) of the configuration file.
- The file openssl.cnf will be assumed to be found in the default certificate area, as configured at the time that the openssl DLL was compiled. This is usually means that the default filename is c:\usr\local\ssl\openssl.cnf.
很清楚地说明了php寻找openssl.cnf有三种方式。
添加环境变量,右击我的电脑属性里就可以,然后我试了,没有成功:(看到计算机里众多的dll和openssl.cnf,我就想放弃了。如果有人成功了请告诉我。
然后看到官方文档最后一句话:Note that it is possible to override the default path from the script using the configargs of the functions that require a configuration file.
于是我在范例前面添加一个config变量,以后用的所有openssl函数里都添加$config来设置,这样居然成功了!!!!
<?php
//cnf存放路径
$opensslConfigPath = "D:/xampp/apache/conf/openssl.cnf";
$config = array(
"config" => $opensslConfigPath,
"digest_alg" => "sha512",
"private_key_bits" => 4096,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
$res = openssl_pkey_new($config);
if (empty($res)) {return false;}
openssl_pkey_export($res, $privKey, NULL, $config);
$pubKey = openssl_pkey_get_details($res);
if ($pubKey === FALSE){return false;}
$pubKey = $pubKey["key"];
$data = '1234 5678 9012';
echo '原文:'.$data;
// 加密
if ($res === FALSE){return false;}
openssl_public_encrypt($data, $encrypted, $pubKey);
// 解密
$res = openssl_private_decrypt($encrypted, $decrypted, $privKey);
if ($res === FALSE){return false;}
else echo '<br>解密后:'.$decrypted;
大家可以复制过去看看运行结果。
至此终于顺利试用了openssl:)
openssl 第一篇的更多相关文章
- 分布式文件系统 FastDFS 5.0.5 & Linux CentOS 7 安装配置(单点安装)——第一篇
分布式文件系统 FastDFS 5.0.5 & Linux CentOS 7 安装配置(单点安装)--第一篇 简介 首先简单了解一下基础概念,FastDFS是一个开源的轻量级分布式文件系统,由 ...
- lnmp架构(第一篇)
lnmp 架构 第一篇 nginx 源码安装 nginx的安装包:nginx-1.12.0.tar.gz 建议安装前的修改: 在nginx的解压包中修改文件nginx-1.12.0/src/core/ ...
- 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...
- Python爬虫小白入门(四)PhatomJS+Selenium第一篇
一.前言 在上一篇博文中,我们的爬虫面临着一个问题,在爬取Unsplash网站的时候,由于网站是下拉刷新,并没有分页.所以不能够通过页码获取页面的url来分别发送网络请求.我也尝试了其他方式,比如下拉 ...
- Three.js 第一篇:绘制一个静态的3D球体
第一篇就画一个球体吧 首先我们知道Three.js其实是一个3D的JS引擎,其中的强大之处就在于这个JS框架并不是依托于JQUERY来写的.那么,我们在写这一篇绘制3D球体的文章的时候,应该注意哪些地 ...
- 深入学习jQuery选择器系列第一篇——基础选择器和层级选择器
× 目录 [1]id选择器 [2]元素选择器 [3]类选择器[4]通配选择器[5]群组选择器[6]后代选择器[7]兄弟选择器 前面的话 选择器是jQuery的根基,在jQuery中,对事件处理.遍历D ...
- 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Android基础学习第一篇—Project目录结构
写在前面的话: 1. 最近在自学Android,也是边看书边写一些Demo,由于知识点越来越多,脑子越来越记不清楚,所以打算写成读书笔记,供以后查看,也算是把自己学到所理解的东西写出来,献丑,如有不对 ...
- 深入理解ajax系列第一篇——XHR对象
× 目录 [1]创建对象 [2]发送请求 [3]接收响应[4]异步处理[5]实例演示 前面的话 ajax是asynchronous javascript and XML的简写,中文翻译是异步的java ...
随机推荐
- linux系统结构和系统命令初步
以上是第五课和第14课笔记 linux 基本结构: 系统构成:kernel,Modules,Lib,(Shell,Tool)系统引导:BIOS -> Bootlooder -> Kerne ...
- iframe和response.sendRedirect使用的问题
一.iframe下使用response.sendRedirect的问题 一般使用filter过滤用户是否登录,如果用户没有登陆则转向登陆页面,这时候可以使用response.sendRedirect( ...
- Linux 的系统运行级别
运行级别 说明 0 系统关机状态 1 单用户工作状态,用于root对系统进行维护,此时不予许其他用户使用主机.(类似于windows 的安全模式) 2 ...
- spark提交任务的流程
1.spark提交流程 sparkContext其实是与一个集群建立一个链接,当你停掉它之后 就会和集群断开链接,则属于这个资源的Excutor就会释放掉了,Driver 向Master申请资源,Ma ...
- LODS LODSB LODSW LODSD 例子【载入串指令】
http://qwop.iteye.com/blog/1958761 // lodsb.cpp : Defines the entry point for the console applicatio ...
- BZOJ 1560 火星藏宝图(DP)
思路:发现如果从A能到B,B能到C,那么一定A能到C,且根据不等式:A^2+B^2<=(A+B)^2,而且权值没有负数,因此经过B比不经过B要优,因此,我们从左上到右下做,每一列,我们只记录之前 ...
- SqlServer计算周岁的函数
CREATE Function Dbo.GetAge ( @birthday datetime, @now datetime ) Returns int As Begin Declare @Age i ...
- ADO.Net对Oracle数据库的操作【转载】
一 ADO.Net简介 访问数据库的技术有许多,常见的有一下几种:开放数据库互联(ODBC).数据访问对象(DAO).远程数据对象 (RDO). ActiveX数据对象(ADO).我们今天主要要学习A ...
- PooledDataSource--mybatis-3-mybatis-3.2.3
org.apache.ibatis.executor.SimpleExecutor public <E> List<E> doQuery(MappedStatement ms, ...
- [转]jQuery EasyUI 扩展-- 主题(Themes)
主题(Themes)允许您改变站点的外观和感观.使用主题可以节省设计的时间,让您腾出更多的时间进行开发.您也可以创建一个已有主题的子主题. 主题生成器(Theme Builder) jQuery UI ...