How to implement asymmetric encryption in php
How to implement asymmetric encryption in php: First create a PHP sample file; then use openssl to implement asymmetric encryption; finally test it through "$rsa = new Rsa('ssl-key');" .
Recommended: "PHP Video Tutorial"
PHP implements asymmetric encryption
#As for what asymmetric encryption is, I won’t go into it here, just Google it. What is explained here is that I have recently been working on an external recharge encryption service, so when it comes to the encryption processing, I encountered a few small problems in the process, so I recorded it for my convenience to check next time.
Detailed code
<?php /** * 使用openssl实现非对称加密 * * @since 2015-11-10 */ class Rsa { /** * 私钥 * */ private $_privKey; /** * 公钥 * */ private $_pubKey; /** * 保存文件地址 */ private $_keyPath; /** * 指定密钥文件地址 * */ public function __construct($path) { if (empty($path) || !is_dir($path)) { throw new Exception('请指定密钥文件地址目录'); } $this->_keyPath = $path; } /** * 创建公钥和私钥 * */ public function createKey() { $config = [ "config" => 'D:\wamp\bin\apache\apache2.4.9\conf\openssl.cnf', "digest_alg" => "sha512", "private_key_bits" => 4096, "private_key_type" => OPENSSL_KEYTYPE_RSA, ]; // 生成私钥 $rsa = openssl_pkey_new($config); openssl_pkey_export($rsa, $privKey, NULL, $config); file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); $this->_privKey = openssl_pkey_get_public($privKey); // 生成公钥 $rsaPri = openssl_pkey_get_details($rsa); $pubKey = $rsaPri['key']; file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); $this->_pubKey = openssl_pkey_get_public($pubKey); } /** * 设置私钥 * */ public function setupPrivKey() { if (is_resource($this->_privKey)) { return true; } $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'; $privKey = file_get_contents($file); $this->_privKey = openssl_pkey_get_private($privKey); return true; } /** * 设置公钥 * */ public function setupPubKey() { if (is_resource($this->_pubKey)) { return true; } $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'; $pubKey = file_get_contents($file); $this->_pubKey = openssl_pkey_get_public($pubKey); return true; } /** * 用私钥加密 * */ public function privEncrypt($data) { if (!is_string($data)) { return null; } $this->setupPrivKey(); $result = openssl_private_encrypt($data, $encrypted, $this->_privKey); if ($result) { return base64_encode($encrypted); } return null; } /** * 私钥解密 * */ public function privDecrypt($encrypted) { if (!is_string($encrypted)) { return null; } $this->setupPrivKey(); $encrypted = base64_decode($encrypted); $result = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); if ($result) { return $decrypted; } return null; } /** * 公钥加密 * */ public function pubEncrypt($data) { if (!is_string($data)) { return null; } $this->setupPubKey(); $result = openssl_public_encrypt($data, $encrypted, $this->_pubKey); if ($result) { return base64_encode($encrypted); } return null; } /** * 公钥解密 * */ public function pubDecrypt($crypted) { if (!is_string($crypted)) { return null; } $this->setupPubKey(); $crypted = base64_decode($crypted); $result = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); if ($result) { return $decrypted; } return null; } /** * __destruct * */ public function __destruct() { @fclose($this->_privKey); @fclose($this->_pubKey); } } ?>
Test
$rsa = new Rsa('ssl-key'); //私钥加密,公钥解密 echo "待加密数据:segmentfault.com\n"; $pre = $rsa->privEncrypt("segmentfault.com"); echo "加密后的密文:\n" . $pre . "\n"; $pud = $rsa->pubDecrypt($pre); echo "解密后数据:" . $pud . "\n"; //公钥加密,私钥解密 echo "待加密数据:segmentfault.com\n"; $pue = $rsa->pubEncrypt("segmentfault.com"); echo "加密后的密文:\n" . $pue . "\n"; $prd = $rsa->privDecrypt($pue); echo "解密后数据:" . $prd;
Important issues
Special attention should be paid here to specify the file address of openssl.cnf in the configuration, or Just set the OPENSSL_CONF global variable.
The above is the detailed content of How to implement asymmetric encryption in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
