1. /**
  2. * 对数据进行签名
  3. * $data = 'If you are still new to things, we’ve provided a few walkthroughs to get you started.'; 签名数据
  4. * $privatekeyFile = '/path/to/private.key'; 私钥
  5. * $passphrase = ''; 密码
  6. */
  7. function sign($data, $privatekeyFile, $passphrase)
  8. {
  9. // 摘要及签名的算法
  10. $digestAlgo = 'sha512';
  11. $algo = OPENSSL_ALGO_SHA1;
  12. // 加载私钥
  13. $privatekey = openssl_pkey_get_private(file_get_contents($privatekeyFile), $passphrase);
  14. // 生成摘要
  15. $digest = openssl_digest($data, $digestAlgo);
  16. // 签名
  17. $signature = '';
  18. openssl_sign($digest, $signature, $privatekey, $algo);
  19. //释放内存
  20. openssl_free_key($privatekey);
  21. $signature = base64_encode($signature);
  22. return $signature;
  23. }
  24.  
  25. /**
  26. * 验签
  27. * $data = 'If you are still new to things, we’ve provided a few walkthroughs to get you started.';
  28. * $publickeyFile = '/path/to/public.key'; 公钥
  29. */
  30. function verify($data, $publickeyFile)
  31. {
  32. // 摘要及签名的算法,同上面一致
  33. $digestAlgo = 'sha512';
  34. $algo = OPENSSL_ALGO_SHA1;
  35. // 加载公钥
  36. $publickey = openssl_pkey_get_public(file_get_contents($publickeyFile));
  37. // 生成摘要
  38. $digest = openssl_digest($data, $digestAlgo);
  39. // 验签
  40. $verify = openssl_verify($digest, base64_decode($signature), $publickey, $algo);
  41. openssl_free_key($publickey);
  42. return $verify; // int(1)表示验签成功
  43. }
  44. /**
  45. * 加密
  46. * $data = 'If you are still new to things, we’ve provided a few walkthroughs to get you started.';
  47. * $publickeyFile = '/path/to/public.key'; 公钥
  48. */
  49. function encrypt($data, $publickeyFile)
  50. {
  51. // 加载公钥
  52. $publickey = openssl_pkey_get_public(file_get_contents($publickeyFile));
  53. // 使用公钥进行加密
  54. $encryptedData = '';
  55. openssl_public_encrypt($data, $encryptedData, $publickey);
  56. return base64_encode($encryptedData);
  57. }
  58. /**
  59. * 解密
  60. * $encryptedData 待解密数据
  61. * $privatekeyFile = '/path/to/private.key'; 私钥
  62. * $passphrase = ''; 密码
  63. */
  64. function decrypt($encryptedData, $privatekeyFile, $passphrase)
  65. {
  66. // 加载私钥
  67. $privatekey = openssl_pkey_get_private(file_get_contents($privatekeyFile), $passphrase);
  68. // 使用公钥进行加密
  69. $sensitiveData = '';
  70. openssl_private_decrypt(base64_decode($encryptedData), $sensitiveData, $privatekey);
  71. return $sensitiveData; // 应该跟$data一致
  72. }

PHP 之用证书对数据进行签名、验签、加密、解密的更多相关文章

  1. RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密

    原文:RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密 C#在using System.Security.Cryptograph ...

  2. 数据安全管理:RSA加密算法,签名验签流程详解

    本文源码:GitHub·点这里 || GitEE·点这里 一.RSA算法简介 1.加密解密 RSA加密是一种非对称加密,在公开密钥加密和电子商业中RSA被广泛使用.可以在不直接传递密钥的情况下,完成加 ...

  3. 证书:数字签名和验签&加密和解密

    用的是湖北省数字证书认证管理中心的签名和加密 1.带私钥的证书,即p12格式证书(后缀为.pfx) 2.不带私钥的证书,有多种格式,通常我们使用的是cer格式证书(后缀为.cer) 一. 1.什么是对 ...

  4. C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServic

    C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider DESCryptoS ...

  5. PHP 生成公钥私钥,加密解密,签名验签

    test_encry.php <?php //创建私钥,公钥 //create_key(); //要加密内容 $str = "test_str"; //加密 $encrypt ...

  6. [Python3] RSA的加解密和签名/验签实现 -- 使用pycrytodome

    Crypto 包介绍: pycrypto,pycrytodome 和 crypto 是一个东西,crypto 在 python 上面的名字是 pycrypto 它是一个第三方库,但是已经停止更新,所以 ...

  7. RSA签名验签

    import android.util.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import ...

  8. 利用SHA-1算法和RSA秘钥进行签名验签(带注释)

    背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...

  9. RSA密钥生成、加密解密、签名验签

    RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...

  10. js rsa sign使用笔记(加密,解密,签名,验签)

    你将会收获: js如何加密, 解密 js如何签名, 验签 js和Java交互如何相互解密, 验签(重点) 通过谷歌, 发现jsrsasign库使用者较多. 查看api发现这个库功能很健全. 本文使用方 ...

随机推荐

  1. C++模板的特化与偏特化

    http://cppblog.com/SmartPtr/archive/2007/07/04/27496.html (1) 类模板定义一个栈的类模板,它可以用来容纳不同的数据类型 template & ...

  2. android (13) Fragment使用下

    一.Fragment使用: 要在你的activity中管理Fragment,须要使用FragmentManager,能够通过getFragmentManager(),这里注意要是在v4包要用getSu ...

  3. Tomcat PK Resin

    特征 Tomcat Resin 所属公司 Apache CAUCHO 用户数 多 少 可參考文档 多 少 与Eclipse集成复杂度 适中 较复杂. Eclipse下调试开发 简便 复杂.更新类后会自 ...

  4. 20170620_javaweb_小结

    01.session失效的方式 02. session和cookie的区别 03.jsp九大内置对象,意义 以及对应的java类 04.转发和重定向 05.jsp的执行过程 和 生命周期 06.实现s ...

  5. [NOIP2003普及组]麦森数(快速幂+高精度)

    [NOIP2003普及组]麦森数(快速幂+高精度) Description 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1不一定也是素数.到1998 ...

  6. bzoj1008 [HNOI2008]越狱——快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 (这样一道水题还因为忘记写 %lld WA了那么多遍) 发生越狱的状态数,就是全部状态 ...

  7. 如何根据configure.ac和Makefile.am为开源代码产生当前平台的Makefile

    1 2 3 4 5 6 7 8 9 //根据configure.in和Makefile.am生成makefile的步骤,基于UBUNTU 12.04 1.autoscan (可选) 2.aclocal ...

  8. JSP-Runoob:JSP 过滤器

    ylbtech-JSP-Runoob:JSP 过滤器 1.返回顶部 1. JSP 过滤器 JSP 和 Servlet 中的过滤器都是 Java 类. 过滤器可以动态地拦截请求和响应,以变换或使用包含在 ...

  9. AJAX异步实现

    AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容. <head> <meta charset="utf-8"> & ...

  10. Kaka's Matrix Travels

    http://poj.org/problem?id=3422 #include <stdio.h> #include <algorithm> #include <stri ...