PHP 之用证书对数据进行签名、验签、加密、解密
/**
* 对数据进行签名
* $data = 'If you are still new to things, we’ve provided a few walkthroughs to get you started.'; 签名数据
* $privatekeyFile = '/path/to/private.key'; 私钥
* $passphrase = ''; 密码
*/
function sign($data, $privatekeyFile, $passphrase)
{
// 摘要及签名的算法
$digestAlgo = 'sha512';
$algo = OPENSSL_ALGO_SHA1;
// 加载私钥
$privatekey = openssl_pkey_get_private(file_get_contents($privatekeyFile), $passphrase);
// 生成摘要
$digest = openssl_digest($data, $digestAlgo);
// 签名
$signature = '';
openssl_sign($digest, $signature, $privatekey, $algo);
//释放内存
openssl_free_key($privatekey);
$signature = base64_encode($signature);
return $signature;
} /**
* 验签
* $data = 'If you are still new to things, we’ve provided a few walkthroughs to get you started.';
* $publickeyFile = '/path/to/public.key'; 公钥
*/
function verify($data, $publickeyFile)
{
// 摘要及签名的算法,同上面一致
$digestAlgo = 'sha512';
$algo = OPENSSL_ALGO_SHA1;
// 加载公钥
$publickey = openssl_pkey_get_public(file_get_contents($publickeyFile));
// 生成摘要
$digest = openssl_digest($data, $digestAlgo);
// 验签
$verify = openssl_verify($digest, base64_decode($signature), $publickey, $algo);
openssl_free_key($publickey);
return $verify; // int(1)表示验签成功
}
/**
* 加密
* $data = 'If you are still new to things, we’ve provided a few walkthroughs to get you started.';
* $publickeyFile = '/path/to/public.key'; 公钥
*/
function encrypt($data, $publickeyFile)
{
// 加载公钥
$publickey = openssl_pkey_get_public(file_get_contents($publickeyFile));
// 使用公钥进行加密
$encryptedData = '';
openssl_public_encrypt($data, $encryptedData, $publickey);
return base64_encode($encryptedData);
}
/**
* 解密
* $encryptedData 待解密数据
* $privatekeyFile = '/path/to/private.key'; 私钥
* $passphrase = ''; 密码
*/
function decrypt($encryptedData, $privatekeyFile, $passphrase)
{
// 加载私钥
$privatekey = openssl_pkey_get_private(file_get_contents($privatekeyFile), $passphrase);
// 使用公钥进行加密
$sensitiveData = '';
openssl_private_decrypt(base64_decode($encryptedData), $sensitiveData, $privatekey);
return $sensitiveData; // 应该跟$data一致
}

PHP 之用证书对数据进行签名、验签、加密、解密的更多相关文章
- RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密
原文:RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密 C#在using System.Security.Cryptograph ...
- 数据安全管理:RSA加密算法,签名验签流程详解
本文源码:GitHub·点这里 || GitEE·点这里 一.RSA算法简介 1.加密解密 RSA加密是一种非对称加密,在公开密钥加密和电子商业中RSA被广泛使用.可以在不直接传递密钥的情况下,完成加 ...
- 证书:数字签名和验签&加密和解密
用的是湖北省数字证书认证管理中心的签名和加密 1.带私钥的证书,即p12格式证书(后缀为.pfx) 2.不带私钥的证书,有多种格式,通常我们使用的是cer格式证书(后缀为.cer) 一. 1.什么是对 ...
- C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServic
C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider DESCryptoS ...
- PHP 生成公钥私钥,加密解密,签名验签
test_encry.php <?php //创建私钥,公钥 //create_key(); //要加密内容 $str = "test_str"; //加密 $encrypt ...
- [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome
Crypto 包介绍: pycrypto,pycrytodome 和 crypto 是一个东西,crypto 在 python 上面的名字是 pycrypto 它是一个第三方库,但是已经停止更新,所以 ...
- RSA签名验签
import android.util.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import ...
- 利用SHA-1算法和RSA秘钥进行签名验签(带注释)
背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...
- RSA密钥生成、加密解密、签名验签
RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...
- js rsa sign使用笔记(加密,解密,签名,验签)
你将会收获: js如何加密, 解密 js如何签名, 验签 js和Java交互如何相互解密, 验签(重点) 通过谷歌, 发现jsrsasign库使用者较多. 查看api发现这个库功能很健全. 本文使用方 ...
随机推荐
- 3deye-demo-8-14-26-51
源码
- XA transaction ,JTA , two phase commit , GTX0-j
本文主要是对以前一直迷惑的几个名字的学习. xa transaction 简单查了一下,大概就是 distributed transaction. 相比传统的transaction而言,传统的tra ...
- willRotateToInterfaceOrientation 屏幕旋转
/* 1.屏幕发生旋转后, 这个旋转事件会先传递给window的rootViewController(窗口的根控制器) 2.rootViewController又会将这个旋转事件传递给它的子控制器 * ...
- Android资源之图像资源(图像级别资源)
图像状态资源仅仅能定义有限的几种状态. 假设须要很多其它的状态,就要使用图像级别资源. 在该资源文件里能够定义随意多个图像级别. 每一个图像级别是一个整数区间,能够通过ImageView.setIma ...
- cookie知识点简点
cookie几大作用: 1.保持用户登陆状态 2.跟踪用户行为 3.制定页面 4.创建购物车 cookie缺点: 1. 可能被禁用 2.可能被删除:cookie是一个文件,easy被用户删除 3.安全 ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
- js的时间展示
<script type="text/javascript">$(function() { //方法调用 showtime(); //默认加载首页 ...
- A. Train and Peter
A. Train and Peter time limit per test 1 second memory limit per test 64 megabytes input standard in ...
- SpringCloud服务组合
SpringCloud生态强调微服务,微服务也就意味着将各个功能独立的业务抽象出来,做成一个单独的服务供外部调用.但每个人对服务究竟要有多“微”的理解差异很大,导致微服务的粒度很难掌控,划分规则也不统 ...
- python--修改默认递归层级
import sys sys.setrecursionlimit(最大递归次数)