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.
- /**
- * 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 information 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 during signature and decryption)
- */
- 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 plaintext
- * @param string ciphertext encoding (base64/hex/bin)
- * @param int padding method (it seems that PHP has a bug, so currently only OPENSSL_PKCS1_PADDING is supported)
- * @return string Secret text
- */
- 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 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 ? rtrim(strrev($result), "
|