3 * Crypt_RSA allows to do following operations:
4 * - key pair generation
5 * - encryption and decryption
6 * - signing and sign validation
10 * LICENSE: This source file is subject to version 3.0 of the PHP license
11 * that is available through the world-wide-web at the following URI:
12 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
13 * the PHP License and are unable to obtain it through the web, please
14 * send a note to license@php.net so we can mail you a copy immediately.
16 * @category Encryption
18 * @author Alexander Valyalkin <valyala@gmail.com>
19 * @copyright 2005 Alexander Valyalkin
20 * @license http://www.php.net/license/3_0.txt PHP License 3.0
21 * @version CVS: $Id: Key.php,v 1.6 2009/01/05 08:30:29 clockwerx Exp $
22 * @link http://pear.php.net/package/Crypt_RSA
26 * RSA error handling facilities
28 require_once 'Crypt/RSA/ErrorHandler.php';
31 * loader for RSA math wrappers
33 require_once 'Crypt/RSA/MathLoader.php';
36 * Crypt_RSA_Key class, derived from Crypt_RSA_ErrorHandler
38 * Provides the following functions:
39 * - getKeyLength() - returns bit key length
40 * - getExponent() - returns key exponent as binary string
41 * - getModulus() - returns key modulus as binary string
42 * - getKeyType() - returns type of the key (public or private)
43 * - toString() - returns serialized key as string
44 * - fromString($key_str) - static function; returns key, unserialized from string
45 * - isValid($key) - static function for validating of $key
48 * // create new 1024-bit key pair
49 * $key_pair = new Crypt_RSA_KeyPair(1024);
51 * // get public key (its class is Crypt_RSA_Key)
52 * $key = $key_pair->getPublicKey();
55 * $len = $key->getKeyLength();
57 * // get modulus as string
58 * $modulus = $key->getModulus();
60 * // get exponent as string
61 * $exponent = $key->getExponent();
63 * // get string represenation of key (use it instead of serialization of Crypt_RSA_Key object)
64 * $key_in_str = $key->toString();
66 * // restore key object from string using 'BigInt' math wrapper
67 * $key = Crypt_RSA_Key::fromString($key_in_str, 'BigInt');
70 * if ($key->isError()) {
71 * echo "error while unserializing key object:\n";
72 * $erorr = $key->getLastError();
73 * echo $error->getMessage(), "\n";
77 * if (Crypt_RSA_Key::isValid($key)) echo 'valid key';
78 * else echo 'invalid key';
80 * // using factory() method instead of constructor (it returns PEAR_Error object on failure)
81 * $rsa_obj = &Crypt_RSA_Key::factory($modulus, $exp, $key_type);
82 * if (PEAR::isError($rsa_obj)) {
83 * echo "error: ", $rsa_obj->getMessage(), "\n";
86 * @category Encryption
88 * @author Alexander Valyalkin <valyala@gmail.com>
89 * @copyright 2005 Alexander Valyalkin
90 * @license http://www.php.net/license/3_0.txt PHP License 3.0
91 * @version Release: @package_version@
92 * @link http://pear.php.net/package/Crypt_RSA
95 class Crypt_RSA_Key extends Crypt_RSA_ErrorHandler
98 * Reference to math wrapper object, which is used to
99 * manipulate large integers in RSA algorithm.
101 * @var object of Crypt_RSA_Math_* class
123 * key type (private or public)
139 * Crypt_RSA_Key constructor.
141 * You should pass in the name of math wrapper, which will be used to
142 * perform different operations with big integers.
143 * See contents of Crypt/RSA/Math folder for examples of wrappers.
144 * Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
146 * @param string $modulus key modulus
147 * @param string $exp key exponent
148 * @param string $key_type type of the key (public or private)
149 * @param string $wrapper_name wrapper to use
150 * @param string $error_handler name of error handler function
154 function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
157 $this->setErrorHandler($error_handler);
158 // try to load math wrapper $wrapper_name
159 $obj = &Crypt_RSA_MathLoader::loadWrapper($wrapper_name);
160 if ($this->isError($obj)) {
161 // error during loading of math wrapper
162 $this->pushError($obj); // push error object into error list
165 $this->_math_obj = &$obj;
167 $this->_modulus = $modulus;
170 if (!in_array($key_type, array('private', 'public'))) {
171 $this->pushError('invalid key type. It must be private or public', CRYPT_RSA_ERROR_WRONG_KEY_TYPE);
174 $this->_key_type = $key_type;
176 /* check length of modulus & exponent ( abs(modulus) > abs(exp) ) */
177 $mod_num = $this->_math_obj->bin2int($this->_modulus);
178 $exp_num = $this->_math_obj->bin2int($this->_exp);
180 if ($this->_math_obj->cmpAbs($mod_num, $exp_num) <= 0) {
181 $this->pushError('modulus must be greater than exponent', CRYPT_RSA_ERROR_EXP_GE_MOD);
185 // determine key length
186 $this->_key_len = $this->_math_obj->bitLen($mod_num);
190 * Crypt_RSA_Key factory.
192 * @param string $modulus key modulus
193 * @param string $exp key exponent
194 * @param string $key_type type of the key (public or private)
195 * @param string $wrapper_name wrapper to use
196 * @param string $error_handler name of error handler function
198 * @return object new Crypt_RSA_Key object on success or PEAR_Error object on failure
201 function factory($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
203 $obj = new Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name, $error_handler);
204 if ($obj->isError()) {
205 // error during creating a new object. Retrurn PEAR_Error object
206 return $obj->getLastError();
208 // object created successfully. Return it
213 * Calculates bit length of the key
215 * @return int bit length of key
218 function getKeyLength()
220 return $this->_key_len;
224 * Returns modulus part of the key as binary string,
225 * which can be used to construct new Crypt_RSA_Key object.
227 * @return string modulus as binary string
230 function getModulus()
232 return $this->_modulus;
236 * Returns exponent part of the key as binary string,
237 * which can be used to construct new Crypt_RSA_Key object.
239 * @return string exponent as binary string
242 function getExponent()
248 * Returns key type (public, private)
250 * @return string key type (public, private)
253 function getKeyType()
255 return $this->_key_type;
259 * Returns string representation of key
261 * @return string key, serialized to string
266 return base64_encode(
268 array($this->_modulus, $this->_exp, $this->_key_type)
274 * Returns Crypt_RSA_Key object, unserialized from
275 * string representation of key.
277 * optional parameter $wrapper_name - is the name of math wrapper,
278 * which will be used during unserialization of this object.
280 * This function can be called statically:
281 * $key = Crypt_RSA_Key::fromString($key_in_string, 'BigInt');
283 * @param string $key_str RSA key, serialized into string
284 * @param string $wrapper_name optional math wrapper name
286 * @return object key as Crypt_RSA_Key object
290 function fromString($key_str, $wrapper_name = 'default')
292 list($modulus, $exponent, $key_type) = unserialize(base64_decode($key_str));
293 $obj = new Crypt_RSA_Key($modulus, $exponent, $key_type, $wrapper_name);
299 * This function can be called statically:
300 * $is_valid = Crypt_RSA_Key::isValid($key)
302 * Returns true, if $key is valid Crypt_RSA key, else returns false
304 * @param object $key Crypt_RSA_Key object for validating
306 * @return bool true if $key is valid, else false
309 function isValid($key)
311 return (is_object($key) && strtolower(get_class($key)) === strtolower(__CLASS__));