Home Backend Development PHP Tutorial Example of implementing rsa asymmetric encryption algorithm using openssl_PHP tutorial

Example of implementing rsa asymmetric encryption algorithm using openssl_PHP tutorial

Jul 13, 2016 am 10:40 AM
openssl rs rsa main use encryption accomplish article Example algorithm

This article mainly introduces the example of using openssl to implement the rsa asymmetric encryption algorithm. Please refer to it

 代码如下: _keyPath = $path;         }           /**         * create the key pair,save the key to $this->_keyPath          */         public function createKey()         {                 $r = openssl_pkey_new();                 openssl_pkey_export($r, $privKey);                 file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey);                 $this->_privKey = openssl_pkey_get_public($privKey);                   $rp = openssl_pkey_get_details($r);                 $pubKey = $rp['key'];                 file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR .  'pub.key', $pubKey);                 $this->_pubKey = openssl_pkey_get_public($pubKey);         }           /**         * setup the private key          */         public function setupPrivKey()         {                 if(is_resource($this->_privKey)){                         return true;                 }                 $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key';                 $prk = file_get_contents($file);                 $this->_privKey = openssl_pkey_get_private($prk);                 return true;         }           /**         * setup the public key          */         public function setupPubKey()         {                 if(is_resource($this->_pubKey)){                         return true;                 }                 $file = $this->_keyPath . DIRECTORY_SEPARATOR .  'pub.key';                 $puk = file_get_contents($file);                 $this->_pubKey = openssl_pkey_get_public($puk);                 return true;         }           /**         * encrypt with the private key          */         public function privEncrypt($data)         {                 if(!is_string($data)){                         return null;                 }                  $this->setupPrivKey();                   $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);                 if($r){                         return base64_encode($encrypted);                 }                 return null;         }           /**         * decrypt with the private key          */         public function privDecrypt($encrypted)         {                 if(!is_string($encrypted)){                         return null;                 }                   $this->setupPrivKey();                   $encrypted = base64_decode($encrypted);                   $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey);                 if($r){                         return $decrypted;                 }                 return null;         }           /**         * encrypt with public key          */         public function pubEncrypt($data)         {                 if(!is_string($data)){                         return null;                 }                   $this->setupPubKey();                   $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey);                 if($r){                         return base64_encode($encrypted);                 }                 return null;         }           /**         * decrypt with the public key          */         public function pubDecrypt($crypted)         {                 if(!is_string($crypted)){                         return null;                 }                   $this->setupPubKey();                   $crypted = base64_decode($crypted);                   $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey);                 if($r){                         return $decrypted;                 }                 return null;         }           public function __destruct()         {                 @ fclose($this->_privKey);                 @ fclose($this->_pubKey);         }   }   //以下是一个简单的测试demo,如果不需要请删除 $rsa = new Rsa('ssl-key');   //私钥加密,公钥解密 echo 'source:我是老鳖
'; $pre = $rsa->privEncrypt('我是老鳖'); echo 'private encrypted:
' . $pre . '
';   $pud = $rsa->pubDecrypt($pre); echo 'public decrypted:' . $pud . '
';   //公钥加密,私钥解密 echo 'source:干IT的
'; $pue = $rsa->pubEncrypt('干IT的'); echo 'public encrypt:
' . $pue . '
';   $prd = $rsa->privDecrypt($pue); echo 'private decrypt:' . $prd; ?>     需要注意的是apache要支持OpenSSL  

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/727549.htmlTechArticleThis article mainly introduces the example of using openssl to implement the rsa asymmetric encryption algorithm. Please refer to the following code: ?php/*** Use openssl to implement asymmetric encryption* @since 2010-07-08*...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance Mar 26, 2024 pm 12:41 PM

Written above & the author’s personal understanding: At present, in the entire autonomous driving system, the perception module plays a vital role. The autonomous vehicle driving on the road can only obtain accurate perception results through the perception module. The downstream regulation and control module in the autonomous driving system makes timely and correct judgments and behavioral decisions. Currently, cars with autonomous driving functions are usually equipped with a variety of data information sensors including surround-view camera sensors, lidar sensors, and millimeter-wave radar sensors to collect information in different modalities to achieve accurate perception tasks. The BEV perception algorithm based on pure vision is favored by the industry because of its low hardware cost and easy deployment, and its output results can be easily applied to various downstream tasks.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Common challenges faced by machine learning algorithms in C++ include memory management, multi-threading, performance optimization, and maintainability. Solutions include using smart pointers, modern threading libraries, SIMD instructions and third-party libraries, as well as following coding style guidelines and using automation tools. Practical cases show how to use the Eigen library to implement linear regression algorithms, effectively manage memory and use high-performance matrix operations.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

The bottom layer of the C++sort function uses merge sort, its complexity is O(nlogn), and provides different sorting algorithm choices, including quick sort, heap sort and stable sort.

How to use Xiaomi Auto app How to use Xiaomi Auto app Apr 01, 2024 pm 09:19 PM

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

01 Outlook Summary Currently, it is difficult to achieve an appropriate balance between detection efficiency and detection results. We have developed an enhanced YOLOv5 algorithm for target detection in high-resolution optical remote sensing images, using multi-layer feature pyramids, multi-detection head strategies and hybrid attention modules to improve the effect of the target detection network in optical remote sensing images. According to the SIMD data set, the mAP of the new algorithm is 2.2% better than YOLOv5 and 8.48% better than YOLOX, achieving a better balance between detection results and speed. 02 Background & Motivation With the rapid development of remote sensing technology, high-resolution optical remote sensing images have been used to describe many objects on the earth’s surface, including aircraft, cars, buildings, etc. Object detection in the interpretation of remote sensing images

See all articles