Table of Contents
php接口数据加密、解密、验证签名
 扩展阅读
Home Backend Development PHP Tutorial php接口数据加密、解密、验证签字代码实例

php接口数据加密、解密、验证签字代码实例

Jun 13, 2016 pm 12:22 PM
directory rsa signature

php接口数据加密、解密、验证签名代码实例

php接口数据加密、解密、验证签名

代码很简单,这里就不多废话了,直接奉上代码

<?php/** * 数据加密,解密,验证签名 * @edit http://www.lai18.com * @date 2015-07-08 **///header('Content-Type: text/xml; charset=utf-8');include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Math'.DIRECTORY_SEPARATOR.'BigInteger.php');include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Crypt'.DIRECTORY_SEPARATOR.'AES.php');include_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'phpsec'.DIRECTORY_SEPARATOR.'Crypt'.DIRECTORY_SEPARATOR.'RSA.php');//密文$crypttext = 'v66YKULHFld2JElhm/J9qik2Edr1JHdZIc/k/OesU2GbTX2usXyvF4jGvzvoihrrE8FsfKmllmjsMIjO5fdrS/FD20bYFii4JW3BO3bzshXmz6AEs2DWwG4sK9mNojfOC0IsMoV311X5/JlgUoQXkDy4F5HHpYE9d/xGb0g2XE/hnGSSy2cpQcvQtBlBmixwSckNhsEG92lovlOz8ULwkqG5o7x&#43;qB7P/EMII/WaFAXBJXDXvZX7lmGcOgon6wLhKJLGXorP6BIxOg6LGc6Ux7BAt3i9&#43;0lujNgxIq/sDsl23hsr3yOUpV5C5a813nrHx4HJyd/hBT1UvIUml&#43;eTmJwWCpSfs2cvxIUr0CE57JAZVyXjK13shK3IsZHLPPsm/JcDCrdy0Co/d5uIGJAdzXdsQ56xsju&#43;tlvnA1J6yq2tDIfYK/x6k911A5WXLKYxztD1nq&#43;bTYN3Gv/WFfrzVtgWQBrh06ihS2cwvna0S9EV/YPmhnAjJmrX4trNr9NXQ9xaZaW4lGRg87U5QDV&#43;nQjj1THk0XHFc69N9g2&#43;DsAGyEs9tK6U0ZQ72hJZqZhBCDH1UKw0PLyIhJdxpgPPOWGp8/QVVU2julTeKunvgAAEc3n&#43;GoZfqjsCDi1S6T2MTnjWYWNoFRBhvEZFD/revgpasTOzDQa5NqR1B&#43;mUF70r6uw6MWLJ7cT9Tz3jq&#43;CA';$aeskey = base64_decode('qZe60QZFxuirub2ey4&#43;7&#43;Q==');//AES解密,采用ECB模式$aes = new Crypt_AES(CRYPT_MODE_ECB);//设置AES密钥$aes->setKey($aeskey);//解密AES密文$plaintext = $aes->decrypt(base64_decode($crypttext));echo $plaintext;echo '<hr />';//AES加密明文//echo $aes->encrypt($plaintext);//rsa公钥$publickey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCffOeIVYut9jW5w1L5uKX4aDvd837a8JhaWm5S8YqNQfgEmfD9T&#43;rDknXLqMT&#43;DXeQAqGo4hBmcbej1aoMzn6hIJHk3/TfTAToNN8fgwDotHewsTCBbVkQWtDTby3GouWToVsRi1i/A0Vfb0&#43;xM8MnF46DdhhrnZrycERBSbyrcwIDAQAB';//echo base64_decode($publickey);//rsa签名$signature = 'XHin4uUFqrKDEhKBD/hQisXLFFSxM6EZCvCPqnWCQJq3uEp3ayxmFuUgVE0Xoh4AIWjIIsOWdnaToL1bXvAFKwjCtXnkaRwUpvWrk&#43;Q0eqwsoAdywsVQDEceG5stas1CkPtrznAIW2eBGXCWspOj&#43;aumEAcPyYDxLhDN646Krzw=';//echo base64_decode($signature);$rsa = new Crypt_RSA();//设置RSA签名模式 CRYPT_RSA_SIGNATURE_PSS or CRYPT_RSA_SIGNATURE_PKCS1$rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);//var_dump($rsa->createKey());//生成RSA公钥、私钥//extract($rsa->createKey());//使用RSA私钥生成签名//$rsa->loadKey($privatekey);//$signature = $rsa->sign($plaintext);//使用RSA公钥验证签名echo $plaintext;$rsa->loadKey(base64_decode($publickey));echo $rsa->verify($plaintext, base64_decode($signature)) ? 'verified' : 'unverified';echo '<hr />';//生成RSA公钥、私钥//var_dump($rsa->createKey());extract($rsa->createKey());//使用RSA私钥加密数据$rsa->loadKey($privatekey);$ciphertext = $rsa->encrypt($plaintext);//使用RSA公钥解密数据$rsa->loadKey($publickey);echo $rsa->decrypt($ciphertext);
Copy after login

每当你讲任何涉及到钱财事务等交易问题时,需要特别注意安全问题的考虑,例如开发一个论坛或者是一个购物车等。

 扩展阅读

《PHP实用问题解决案例》系列技术文章整理收藏 

1PHP汉字拼音转换和公历农历转换

2php购物车实现方法

3PHP实现格式化文件数据大小显示的方法

4PHP+jquery实时显示网站在线人数的方法

5PHP 实现判断用户是否手机访问

6PHP转义Json里的特殊字符的函数

7php基于socket实现SMTP发送邮件的方法

8PHP查询快递信息的方法

9PHP简单获取视频预览图的方法

10php接口数据加密、解密、验证签名

11php计算指定目录下文件占用空间的方法

12php通过递归方式复制目录和子目录的方法

13php中通过DirectoryIterator删除整个目录的方法

14PHP生成迅雷、快车、旋风等软件的下载链接代码实例

15PHP中文乱码分类及解决办法大全

16php从数组中随机选择若干不重复元素的方法

17PHP文件上传问题总结——文件大小检测以及大文件上传处理

18如何修改PHP的memory_limit限制

19php判断一个请求是ajax请求还是普通请求的方法

20PHP超牛逼无限极分类生成树方法

21长微博生成(将html转化为图片)原理浅析

版权声明:本文为博主原创文章,未经博主允许不得转载。

1楼qq2291738886昨天 20:51
2291738886
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory Users and Computers Missing [Fixed in 3 Ways] Active Directory Users and Computers Missing [Fixed in 3 Ways] Apr 20, 2023 pm 01:25 PM

Active Directory Users and Computers (ADUC) loss is one of the most frustrating issues reported by many Windows Pro users. ADUC is an incredible MMC snap-in that enables administrators to manage Microsoft Active Directory. However, for some reason it is missing in the Windows Server or Enterprise/Pro editions. Let’s take a closer look at why it’s missing and how we can fix it. Does Windows 11 have Active Directory? Active Directory is useful for anyone wanting to manage remote

Use Python to implement RSA encryption and decryption Use Python to implement RSA encryption and decryption Apr 14, 2023 pm 02:13 PM

Yesterday I saw an English article [1] that showed how to use Python to implement the RSA algorithm. The logic of the code is the same as the previous article Understanding the RSA Algorithm. Friends who are not familiar with RSA can read the article Understanding the RSA Algorithm, which faces What is RSA, the mathematical principles of RSA are explained, and a simple example is given. It can be said that this is the easiest article to understand RSA on Quanzhihu (this comes from readers' comments). I ran the code provided in English and found that it could not encrypt Chinese, so I modified the encryption and decryption functions to support Chinese encryption and decryption. Today’s article will share how to use Python to implement the RSA encryption and decryption process to help you establish

How to use PHP and GMP to perform RSA encryption and decryption algorithms for large integers How to use PHP and GMP to perform RSA encryption and decryption algorithms for large integers Jul 28, 2023 pm 05:25 PM

How to use PHP and GMP to perform RSA encryption and decryption algorithm for large integers. The RSA encryption algorithm is an asymmetric encryption algorithm that is widely used in the field of data security. It implements the process of public key encryption and private key decryption based on two particularly large prime numbers and some simple mathematical operations. In the PHP language, the calculation of large integers can be realized through the GMP (GNUMultiplePrecision) library, and the encryption and decryption functions can be realized by combining the RSA algorithm. This article will introduce how to use PHP and GMP libraries to

How to implement RSA encryption and decryption algorithms using PHP and GMP How to implement RSA encryption and decryption algorithms using PHP and GMP Jul 28, 2023 pm 11:54 PM

How to use PHP and GMP to implement RSA encryption and decryption algorithm RSA encryption algorithm is an asymmetric encryption algorithm that is widely used in the field of information security. In practical applications, it is often necessary to use programming languages ​​to implement RSA encryption and decryption algorithms. PHP is a commonly used server-side scripting language, and GMP (GNUMultiplePrecision) is a high-precision mathematical calculation library that can help us perform large number operations required in the RSA algorithm. This article will introduce how to use PHP and GMP

PHP implements RSA asymmetric encryption technology PHP implements RSA asymmetric encryption technology Jun 18, 2023 am 09:34 AM

RSA asymmetric encryption technology is one of the most popular and secure encryption methods currently. As a widely used programming language, PHP also has unique advantages in implementing RSA encryption. This article will introduce readers to how to use PHP to implement RSA asymmetric encryption technology. 1. What is the RSA algorithm? The RSA algorithm is an asymmetric encryption technology. It is usually used for data encryption and digital signatures. Its security is mainly based on a number theory problem, namely the difficulty of factoring very large integers in a very short time. RSA algorithm encrypted stream

How to write RSA encryption algorithm using Python? How to write RSA encryption algorithm using Python? Sep 20, 2023 pm 01:21 PM

How to write RSA encryption algorithm using Python? Introduction: RSA is an asymmetric encryption algorithm that is widely used in the field of information security. In modern communications, the RSA encryption algorithm is commonly used to encrypt and decrypt sensitive data. This article will introduce how to use Python to write the RSA encryption algorithm and provide specific code examples. Install the Python library Before you start writing the RSA encryption algorithm, you need to install the Python encryption library. It can be installed using the following command: pipinstallrsa generate

NIST finalizes three post-quantum cryptography standards to better protect the Internet, cryptocurrency, and communications NIST finalizes three post-quantum cryptography standards to better protect the Internet, cryptocurrency, and communications Aug 15, 2024 pm 03:50 PM

The US National Institute of Standards and Technology (NIST) has finalized three post-quantum cryptography standards after nearly a decade of work. This move is in preparation for the ability of emerging quantum computers to crack public-key cryptosy

How to fix SSH not working on MacOS Ventura How to fix SSH not working on MacOS Ventura Apr 23, 2023 am 11:46 AM

How to Fix SSH Not Using RSA Signatures on MacOS Ventura We will modify the ssh_config file to allow RSA host keys again, here is how to do it. Open Terminal (either through Spotlight or through the Utilities folder) and enter the following command string: sudonano /etc/ssh/ssh_config You will need to authenticate with the administrator password. Scroll all the way to the bottom of the ssh_config file and add the following line to the bottom of ssh_config: HostkeyAlgorithms+ssh-rsaPubkeyA

See all articles