Example #1
0
 /**
  * プライベートキー取得時使用
  * @return array
  * @access	public
  */
 function &getEncryptionKeys()
 {
     // 有効期限が切れてないものを取得
     $int_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d") - $this->expiration_day, date("Y"));
     $time = date("YmdHis", $int_time);
     $where_params = array("expiration_time >= " . $time => null);
     $result = $this->_db->selectExecute("encryption", $where_params, null, 1);
     if ($result === false) {
         return $result;
     }
     if (!isset($result[0])) {
         // 有効期限が切れている or 新規作成
         $key_pair = new Crypt_RSA_KeyPair($this->key_length);
         $public_key = $key_pair->getPublicKey();
         $private_key = $key_pair->getPrivateKey();
         //insert
         $update_time = timezone_date();
         $container =& DIContainerFactory::getContainer();
         $session =& $container->getComponent("Session");
         $user_id = $session->getParameter("_user_id");
         $int_time = mktime(date("H"), date("i"), date("s"), date("m"), date("d") + $this->expiration_day, date("Y"));
         $time = date("YmdHis", $int_time);
         $params = array("public_key" => $public_key->toString(), "private_key" => $private_key->toString(), "key_length" => $this->key_length, "expiration_time" => $time, "update_time" => $update_time, "update_user" => $user_id);
         $result = $this->_db->insertExecute("encryption", $params, false);
         if ($result === false) {
             return $result;
         }
     } else {
         $params = $result[0];
         //$private_key = $result[0]['private_key'];
     }
     return $params;
 }
Example #2
0
function go($math_wrapper)
{
    echo "Test {$math_wrapper}: ";
    mt_srand(1);
    $start = getmicrotime();
    $keypair =& Crypt_RSA_KeyPair::factory(KEY_LENGTH, $math_wrapper, '', 'mt_rand');
    if (PEAR::isError($obj)) {
        echo 'failed: ', $obj->getMessage(), "\n";
        return;
    }
    $time = getmicrotime() - $start;
    printf("done. Time: %.3f seconds\n", $time);
}
Example #3
0
 function wrap_rsa_get_key_size($keypair = '')
 {
     global $last_rsa_error;
     global $wrap_rsa_default_key_size;
     if ($keypair === '') {
         return $wrap_rsa_default_key_size;
     }
     $key_pair = Crypt_RSA_KeyPair::fromPEMString($keypair, 'default', 'wrap_rsa_cerr');
     if ($last_rsa_error !== false) {
         return false;
     }
     $public_key = $key_pair->getPublicKey();
     if (wrap_rsa_cerr($public_key)) {
         return false;
     }
     $length = (int) $public_key->getKeyLength();
     if (wrap_rsa_cerr($public_key)) {
         return false;
     }
     return $length;
 }
 public static function getPublicRSAKey()
 {
     $errorSettings = error_reporting(0);
     $pemString = file_get_contents('key.pem');
     $keyPair = Crypt_RSA_KeyPair::fromPEMString($pemString);
     $math_obj =& Crypt_RSA_MathLoader::loadWrapper('default');
     $publicKey = $keyPair->getPublicKey();
     $keyArray = array('exponent' => $math_obj->bin2int($publicKey->getExponent()), 'modulus' => $math_obj->bin2int($publicKey->getModulus()));
     if ($math_obj->getWrapperName() == 'GMP') {
         $keyArray = array_map('gmp_strval', $keyArray);
     }
     error_reporting($errorSettings);
     return $keyArray;
 }
Example #5
0
 /**
  * converts keypair to PEM-encoded string, which can be stroed in 
  * .pem compatible files, contianing RSA private key.
  *
  * @return string PEM-encoded keypair on success, false on error
  * @access public
  */
 function toPEMString()
 {
     // store RSA private key attributes into ASN.1 string
     $str = '';
     $attr_names = $this->_get_attr_names();
     $n = sizeof($attr_names);
     $rsa_attrs = $this->_attrs;
     for ($i = 0; $i < $n; $i++) {
         $attr = $attr_names[$i];
         if (!isset($rsa_attrs[$attr])) {
             $this->pushError("Cannot find value for ASN.1 attribute [{$attr}]");
             return false;
         }
         $tmp = $rsa_attrs[$attr];
         $str .= Crypt_RSA_KeyPair::_ASN1StoreInt($tmp);
     }
     // prepend $str by ASN.1 SEQUENCE (0x10) header
     $str = Crypt_RSA_KeyPair::_ASN1Store($str, 0x10, true);
     // encode and format PEM string
     $str = base64_encode($str);
     $str = chunk_split($str, 64, "\n");
     return "-----BEGIN RSA PRIVATE KEY-----\n{$str}-----END RSA PRIVATE KEY-----\n";
 }
Example #6
0
// try to use $public_key1 for encryption and unserialized form
// $private_key_str key for decryption
$text = '1234567890';
$enc_text = $rsa_obj->encrypt($text, $public_key1);
$private_key = Crypt_RSA_Key::fromString($private_key_str, MATH_LIBRARY, 'check_error');
$text1 = $rsa_obj->decrypt($enc_text, $private_key);
if ($text != $text1) {
    $errors[] = "error in Crypt_RSA_Key class methods";
}
///////////////////////////////////////////////
// test all functionality of Crypt_RSA class
///////////////////////////////////////////////
// create Crypt_RSA object
$rsa_obj = new Crypt_RSA(array(), MATH_LIBRARY, 'check_error');
// create Crypt_RSA_KeyPair object
$key_pair = new Crypt_RSA_KeyPair(256, MATH_LIBRARY, 'check_error');
// check encrypting/decrypting function's behaviour
$params = array('enc_key' => $key_pair->getPublicKey(), 'dec_key' => $key_pair->getPrivateKey());
$rsa_obj->setParams($params);
$text = '1234567890';
$enc_text = $rsa_obj->encrypt($text);
$text1 = $rsa_obj->decrypt($enc_text);
if ($text != $text1) {
    $errors[] = "error in encrypting/decrypting functions";
}
// check signing/sign validating
$params = array('public_key' => $key_pair->getPublicKey(), 'private_key' => $key_pair->getPrivateKey());
$rsa_obj->setParams($params);
$text = '1234567890';
$sign = $rsa_obj->createSign($text);
if (!$rsa_obj->validateSign($text, $sign)) {
Example #7
0
 /**
  * Crypt_RSA_KeyPair factory.
  *
  * @param int $key_len bit length of key pair, which will be generated in constructor
  * @param string $wrapper_name
  *        Name of math wrapper, which will be used to
  *        perform different operations with big integers.
  *        See contents of Crypt/RSA/Math folder for examples of wrappers.
  *        Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
  *
  * @return object   new Crypt_RSA_KeyPair object on success or PEAR_Error object on failure
  * @access public
  */
 function &factory($key_len, $wrapper_name = 'default')
 {
     $obj = new Crypt_RSA_KeyPair($key_len, $wrapper_name);
     if ($obj->isError()) {
         // error during creating a new object. Retrurn PEAR_Error object
         return $obj->getLastError();
     }
     // object created successfully. Return it
     return $obj;
 }
Example #8
0
 * @category   Encryption
 * @package    Crypt_RSA
 * @author     Alexander Valyalkin <*****@*****.**>
 * @copyright  2005 Alexander Valyalkin
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 * @version    1.0.0
 * @link       http://pear.php.net/package/Crypt_RSA
 */
/**
 * this test script checks factory() methods functionality
 * for Crypt_RSA, Crypt_RSA_Key and Crypt_RSA_KeyPair classes
 */
require_once dirname(__FILE__) . '/../RSA.php';
echo "Start of testing factory() methods...\n";
// try to create a Crypt_RSA object using factory() static call
$obj =& Crypt_RSA::factory();
if (PEAR::isError($obj)) {
    echo 'error in Crypt_RSA factory(): ', $obj->getMessage(), "\n";
}
// try to create a Crypt_RSA_KeyPair object using factory() static call
$obj =& Crypt_RSA_KeyPair::factory(128);
if (PEAR::isError($obj)) {
    echo 'error in Crypt_RSA_KeyPair factory(): ', $obj->getMessage(), "\n";
}
$key = $obj->getPrivateKey();
// try to create a Crypt_RSA_Key object using factory() static call
$obj =& Crypt_RSA_Key::factory($key->getModulus(), $key->getExponent(), $key->getKeyType());
if (PEAR::isError($obj)) {
    echo 'error in Crypt_RSA_KeyPair factory(): ', $obj->getMessage(), "\n";
}
echo "end\n";