PHP implements RSA encryption class example_PHP tutorial
Example of RSA encryption class implemented in php
This article mainly introduces the implementation of RSA encryption class in php. The example analyzes the techniques of php custom RSA class to implement encryption and decryption, which is very useful. Practical value, friends in need can refer to it
The example in this article describes the implementation of RSA encryption class in PHP. Share it with everyone for your reference. The specific analysis is as follows:
Signature, signature verification, and asymmetric encryption and decryption implemented through openssl need to be used with x.509 certificate (such as crt and pem) files.
Due to various reasons, this class is not very complete, and various tests are welcome!
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
/** * RSA algorithm class * Signature and ciphertext encoding: base64 string/hex string/binary string stream * Padding method: PKCS1Padding (encryption and decryption)/NOPadding (decryption) * * Notice:Only accepts a single block. Block size is equal to the RSA key size! * If the key length is 1024 bit, the data during encryption must be less than 128 bytes, plus the 11 bytes of PKCS1Padding itself, so the plaintext must be less than 117 bytes * * @author: linvo * @version: 1.0.0 * @date: 2013/1/23 */ class RSA{ private $pubKey = null; private $priKey = null; /** * Custom error handling */ private function _error($msg){ die('RSA Error:' . $msg); //TODO } /** * Constructor * * @param string public key file (passed in during signature verification and encryption) * @param string private key file (passed in when signing and decrypting) */ public function __construct($public_key_file = '', $private_key_file = ''){ if ($public_key_file){ $this->_getPublicKey($public_key_file); } if ($private_key_file){ $this->_getPrivateKey($private_key_file); } } /** * Generate signature * * @param string signature material * @param string signature encoding (base64/hex/bin) * @return signature value */ public function sign($data, $code = 'base64'){ $ret = false; if (openssl_sign($data, $ret, $this->priKey)){ $ret = $this->_encode($ret, $code); } return $ret; } /** * Verify signature * * @param string signature material * @param string signature value * @param string signature encoding (base64/hex/bin) * @return bool */ public function verify($data, $sign, $code = 'base64'){ $ret = false; $sign = $this->_decode($sign, $code); if ($sign !== false) { switch (openssl_verify($data, $sign, $this->pubKey)){ case 1: $ret = true; break; case 0: case -1: default: $ret = false; } } return $ret; } /** * Encryption * * @param string plain text * @param string ciphertext encoding (base64/hex/bin) * @param int filling method (it seems that PHP has a bug, so it currently only supports OPENSSL_PKCS1_PADDING) * @return string ciphertext */ public function encrypt($data, $code = 'base64', $padding = OPENSSL_PKCS1_PADDING){ $ret = false; if (!$this->_checkPadding($padding, 'en')) $this->_error('padding error'); if (openssl_public_encrypt($data, $result, $this->pubKey, $padding)){ $ret = $this->_encode($result, $code); } return $ret; } /** * Decryption * * @param string ciphertext * @param string ciphertext encoding (base64/hex/bin) * @param int padding method (OPENSSL_PKCS1_PADDING / OPENSSL_NO_PADDING) * @param bool Whether to flip the plaintext (When passing Microsoft CryptoAPI-generated RSA cyphertext, revert the bytes in the block) * @return string plain text */ public function decrypt($data, $code = 'base64', $padding = OPENSSL_PKCS1_PADDING, $rev = false){ $ret = false; $data = $this->_decode($data, $code); if (!$this->_checkPadding($padding, 'de')) $this->_error('padding error'); if ($data !== false){ if (openssl_private_decrypt($data, $result, $this->priKey, $padding)){ $ret = $rev ? rtrim(strrev($result), " |

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



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

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

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.
