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 - How To Set Up an NFS Mount on CentOS 6
About NFS (Network File System) Mounts NFS mounts work to share a directory between several servers. ...
- c# 菜单无限极分类-利用递归
表结构: 前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web ...
- 深度学习word2vec笔记之算法篇
深度学习word2vec笔记之算法篇 声明: 本文转自推酷中的一篇博文http://www.tuicool.com/articles/fmuyamf,若有错误望海涵 前言 在看word2vec的资料 ...
- Android 设置让EditText不自动获取焦点
在EditText所在的父控件中设置如下属性: android:focusable="true" android:focusableInTouchMode="true&q ...
- Codeforces 229D Towers
http://codeforces.com/problemset/problem/229/D 题意:有n(1<=n<=5,000)座塔排在一条直线上,从左到右每个塔的高度分别为hi(1&l ...
- BZOJ 2879 NOI2012美食节
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2879 CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M ...
- Absolute sort
Absolute sort Let's try some sorting. Here is an array with the specific rules. The array (a tuple) ...
- linux awk 使用
awk是linux下的一个命令,他对其他命令的输出,对文件的处理都十分强大,其实他更像一门编程语言,他可以自定义变量,有条件语句,有循环,有数组,有正则,有函数等.他读取输出,或者文件的方式是一行,一 ...
- 05_Elasticsearch 单模式下API的增删改查操作
05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...
- qt model/view 架构基础介绍之QTreeWidget
# -*- coding: utf-8 -*- # python:2.x #说明:QTreeWidget用于展示树型结构,也就是层次结构同前面说的 QListWidget 类似,这个类需要同另外一个辅 ...