This article describes the implementation of the RSA encryption class PHP. Share to everyone for your reference. The specific analysis is as follows:
Signed, checked, asymmetric, and decrypted via OpenSSL need to be used in conjunction with X.509 certificates (such as CRT and PEM) files.
Due to various reasons, this class is not very perfect, welcome to all kinds of tests!
<?php/** * RSA algorithm class * Signature and ciphertext encoding: Base64 string/16 binary string/binary string stream * Fill mode: pkcs1padding (add-Decrypt)/nopadding (decrypt) * * Notice:only A Ccepts a single block.
Block size is equal to the RSA key size! * If the key length is 1024 bit, then the data should be less than 128 bytes when encrypted, plus 11 bytes of pkcs1padding itself, so plaintext needs to be less than 117 bytes * * @author: Linvo * @version: 1.0.0 * @date: 201
3/1/23 * * Class rsa{private $pubKey = null;
Private $priKey = null; /** * Custom error handling/Private function _error ($msg) {die (' RSA error: '. $msg);//todo}/** * constructor * @para M string Public key file (passed in when checking and encrypting) * @param string private key file (passed in when signing and decrypting) * * * __CONSTRUCT ($public _key_file = ' ", $privat
E_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 funct
Ion sign ($data, $code = ' base64 ') {$ret = false; if (Openssl_sign ($dATA, $ret, $this->prikey)) {$ret = $this->_encode ($ret, $code);
return $ret;
/** * Verification Signature * * @param string Signature material * @param string Signature value * @param string Signature Code (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;
Case 0:CASE-1: Default: $ret = false;
} return $ret; /** * Encryption * * @param string plaintext * @param string cipher Code (BASE64/HEX/BIN) * @param int padding (seemingly php has bugs, so currently only supports OPENSS l_pkcs1_padding) * @return String redaction */Public Function Encrypt ($data, $code = ' base64 ', $padding = Openssl_pkcs1_pa
dding) {$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 redaction * @param string cipher Code (BASE64/HEX/BIN) * @param int Fill (openssl_pkcs1_padding/ openssl_no_padding) * @param bool Flip plaintext (When passing Microsoft cryptoapi-generated RSA cyphertext, revert the bytes in The block) * @return String plaintext */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? rtri
M ($result), "Strrev"): '. $result;
} return $ret; }//Private method/** * Detect fill type * Encryption only supports pkcs1_padding * decryption support pkcs1_padding and no_padding * * @param int fill mode * @param s Tring Encryption en/decryption de * @return bool/Private Function _checkpadding ($padding, $type) {if ($type = = ' en ') {switch ($padding) {case openssl_pkcs1_padding: $ret = true;
Break
Default: $ret = false;
} else {switch ($padding) {case Openssl_pkcs1_padding:case openssl_no_padding: $ret = true;
Break
Default: $ret = false;
} return $ret; Private Function _encode ($data, $code) {switch (Strtolower ($code)) {case ' base64 ': $data = Base64_encode ('
'. $data);
Break
Case ' hex ': $data = Bin2Hex ($data);
Break
Case ' bin ': Default:} return $data; Private Function _decode ($data, $code) {switch (Strtolower ($code)) {case ' base64 ': $data = Base64_decode ($
data);
Break
Case ' hex ': $data = $this->_hex2bin ($data);
Break
Case ' bin ': Default:} return $data;
Private Function _getpublickey ($file) {$key _content = $this->_readfile ($file);
if ($key _content) {$this->pubkey = Openssl_get_publickey ($key _content);
}Private Function _getprivatekey ($file) {$key _content = $this->_readfile ($file);
if ($key _content) {$this->prikey = Openssl_get_privatekey ($key _content);
The Private Function _readfile ($file) {$ret = false;
if (!file_exists ($file)) {$this->_error ("The file {$file} is not exists");
else {$ret = file_get_contents ($file);
return $ret; Private Function _hex2bin ($hex = False) {$ret = $hex!== false && preg_match ('/^[0-9a-fa-f]+$/i ', $hex)? p
ACK ("h*", $hex): false;
return $ret; }
}
Test Demo:
<?php
header (' content-type:text/html; Charset=utf-8; ');
Include "rsa.php";
Echo ' <pre> ';
$a = isset ($_get[' a '])? $_get[' A ']: ' Test 123 ';
$pubfile = ' E:\ssl\cert\pwd.crt ';
$prifile = ' E:\ssl\cert\pwd.pem ';
$m = new RSA ($pubfile, $prifile);
$x = $m->sign ($a);
$y = $m->verify ($a, $x);
Var_dump ($x, $y);
$x = $m->encrypt ($a);
$y = $m->decrypt ($x);
Var_dump ($x, $y);
I hope this article will help you with your PHP program design.