Esempio n. 1
0
function decrypt($encriptedMessage, $privateKey)
{
    ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . S3DB_SERVER_ROOT);
    require_once 'pearlib/RSACrypt/RSA.php';
    $enc_text = $encriptedMessage;
    $private_key = $privateKey;
    $key = Crypt_RSA_Key::fromString($private_key);
    check_error($key);
    $rsa_obj = new Crypt_RSA();
    check_error($rsa_obj);
    $rsa_obj->setParams(array('dec_key' => $key));
    check_error($rsa_obj);
    $plain_text = $rsa_obj->decrypt($enc_text);
    check_error($rsa_obj);
    return $plain_text;
}
Esempio n. 2
0
 /**
  * Validates $signature for document $document with public key $this->_public_key
  * or $public_key and hash function $this->_hash_func or $hash_func.
  *
  * @param string $document    document, signature of which must be validated
  * @param string $signature   signature, which must be validated
  * @param object $public_key  public key (object of Crypt_RSA_Key class)
  * @param string $hash_func   hash function, which will be used during validating signature
  * @return mixed
  *         true, if signature of document is valid
  *         false, if signature of document is invalid
  *         null on error
  *
  * @access public
  */
 function validateSign($document, $signature, $public_key = null, $hash_func = null)
 {
     // check public key
     if (is_null($public_key)) {
         $public_key = $this->_public_key;
     } elseif (!Crypt_RSA_Key::isValid($public_key)) {
         $obj = PEAR::raiseError('invalid public key. It must be an object of Crypt_RSA_Key class', CRYPT_RSA_ERROR_WRONG_KEY);
         $this->pushError($obj);
         return null;
     }
     if ($public_key->getKeyType() != 'public') {
         $obj = PEAR::raiseError('validating key must be public', CRYPT_RSA_ERROR_NEED_PUB_KEY);
         $this->pushError($obj);
         return null;
     }
     // check hash_func
     if (is_null($hash_func)) {
         $hash_func = $this->_hash_func;
     }
     if (!function_exists($hash_func)) {
         $obj = PEAR::raiseError('cannot find hash function with name [' . $hash_func . ']', CRYPT_RSA_ERROR_WRONG_HASH_FUNC);
         $this->pushError($obj);
         return null;
     }
     return $hash_func($document) == $this->decrypt($signature, $public_key);
 }
Esempio n. 3
0
// extra small key pair (32-bit)
$public_key = $key_pair->getPublicKey();
$private_key = $key_pair->getPrivateKey();
// check the length of public key
if ($public_key->getKeyLength() != 32) {
    $errors[] = "wrong result returned from Crypt_RSA_Key::getKeyLength() function";
}
// construct copy of $public_key
$public_key1 = new Crypt_RSA_Key($public_key->getModulus(), $public_key->getExponent(), $public_key->getKeyType(), MATH_LIBRARY, 'check_error');
// serialize $private_key
$private_key_str = $private_key->toString();
// 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';
Esempio n. 4
0
 /**
  * Crypt_RSA_Key factory.
  *
  * @param string $modulus   key modulus
  * @param string $exp       key exponent
  * @param string $key_type  type of the key (public or private)
  *
  * @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_Key object on success or PEAR_Error object on failure
  * @access public
  */
 function &factory($modulus, $exp, $key_type, $wrapper_name = 'default')
 {
     $obj = new Crypt_RSA_Key($modulus, $exp, $key_type, $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;
 }
Esempio n. 5
0
 /**
  * Generates new Crypt_RSA key pair with length $key_len.
  * If $key_len is missed, use an old key length from $this->_key_len
  *
  * @param int $key_len  bit length of key pair, which will be generated
  * @return bool         true on success or false on error
  * @access public
  */
 function generate($key_len = null)
 {
     if (is_null($key_len)) {
         // use an old key length
         $key_len = $this->_key_len;
         if (is_null($key_len)) {
             $obj = PEAR::raiseError('missing key_len parameter', CRYPT_RSA_ERROR_MISSING_KEY_LEN);
             $this->pushError($obj);
             return false;
         }
     }
     // align $key_len to 8 bits
     if ($key_len & 7) {
         $key_len += 8 - $key_len % 8;
     }
     // store key length in the _key_len property
     $this->_key_len = $key_len;
     // generate two primes p and q
     $p_len = (int) ($key_len / 2) + 1;
     $q_len = $key_len - $p_len;
     $p = $this->_math_obj->getRand($p_len, $this->_random_generator, true);
     $p = $this->_math_obj->nextPrime($p);
     do {
         do {
             $q = $this->_math_obj->getRand($q_len, $this->_random_generator, true);
             $tmp_len = $this->_math_obj->bitLen($this->_math_obj->mul($p, $q));
             if ($tmp_len < $key_len) {
                 $q_len++;
             } elseif ($tmp_len > $key_len) {
                 $q_len--;
             }
         } while ($tmp_len != $key_len);
         $q = $this->_math_obj->nextPrime($q);
         $tmp = $this->_math_obj->mul($p, $q);
     } while ($this->_math_obj->bitLen($tmp) != $key_len);
     // $n - is shared modulus
     $n = $this->_math_obj->mul($p, $q);
     // generate public ($e) and private ($d) keys
     $pq = $this->_math_obj->mul($this->_math_obj->dec($p), $this->_math_obj->dec($q));
     do {
         $e = $this->_math_obj->getRand($q_len, $this->_random_generator);
         if ($this->_math_obj->isZero($e) || $this->_math_obj->isZero($this->_math_obj->dec($e))) {
             // exponent cannot be equal to 0 or 1
             continue;
         }
         if ($this->_math_obj->isZero($this->_math_obj->dec($this->_math_obj->gcd($e, $pq)))) {
             // exponent is found
             break;
         }
     } while (true);
     $d = $this->_math_obj->invmod($e, $pq);
     $modulus = $this->_math_obj->int2bin($n);
     $public_exp = $this->_math_obj->int2bin($e);
     $private_exp = $this->_math_obj->int2bin($d);
     // try to create public key object
     $obj = new Crypt_RSA_Key($modulus, $public_exp, 'public', $this->_math_obj->getWrapperName(), $this->_error_handler);
     if ($obj->isError()) {
         // error during creating public object
         $this->pushError($obj->getLastError());
         return false;
     }
     $this->_public_key = $obj;
     // try to create private key object
     $obj = new Crypt_RSA_Key($modulus, $private_exp, 'private', $this->_math_obj->getWrapperName(), $this->_error_handler);
     if ($obj->isError()) {
         // error during creating private key object
         $this->pushError($obj->getLastError());
         return false;
     }
     $this->_private_key = $obj;
     return true;
     // key pair successfully generated
 }
Esempio n. 6
0
 function generate($key_len = null)
 {
     if (is_null($key_len)) {
         // use an old key length
         $key_len = $this->_key_len;
         if (is_null($key_len)) {
             $this->pushError('missing key_len parameter', CRYPT_RSA_ERROR_MISSING_KEY_LEN);
             return false;
         }
     }
     // minimal key length is 8 bit ;)
     if ($key_len < 8) {
         $key_len = 8;
     }
     // store key length in the _key_len property
     $this->_key_len = $key_len;
     // set [e] to 0x10001 (65537)
     $e = $this->_math_obj->bin2int("");
     // generate [p], [q] and [n]
     $p_len = intval(($key_len + 1) / 2);
     $q_len = $key_len - $p_len;
     $p1 = $q1 = 0;
     do {
         // generate prime number [$p] with length [$p_len] with the following condition:
         // GCD($e, $p - 1) = 1
         do {
             $p = $this->_math_obj->getPrime($p_len, $this->_random_generator);
             $p1 = $this->_math_obj->dec($p);
             $tmp = $this->_math_obj->GCD($e, $p1);
         } while (!$this->_math_obj->isOne($tmp));
         // generate prime number [$q] with length [$q_len] with the following conditions:
         // GCD($e, $q - 1) = 1
         // $q != $p
         do {
             $q = $this->_math_obj->getPrime($q_len, $this->_random_generator);
             $q1 = $this->_math_obj->dec($q);
             $tmp = $this->_math_obj->GCD($e, $q1);
         } while (!$this->_math_obj->isOne($tmp) && !$this->_math_obj->cmpAbs($q, $p));
         // if (p < q), then exchange them
         if ($this->_math_obj->cmpAbs($p, $q) < 0) {
             $tmp = $p;
             $p = $q;
             $q = $tmp;
             $tmp = $p1;
             $p1 = $q1;
             $q1 = $tmp;
         }
         // calculate n = p * q
         $n = $this->_math_obj->mul($p, $q);
     } while ($this->_math_obj->bitLen($n) != $key_len);
     // calculate d = 1/e mod (p - 1) * (q - 1)
     $pq = $this->_math_obj->mul($p1, $q1);
     $d = $this->_math_obj->invmod($e, $pq);
     // calculate dmp1 = d mod (p - 1)
     $dmp1 = $this->_math_obj->mod($d, $p1);
     // calculate dmq1 = d mod (q - 1)
     $dmq1 = $this->_math_obj->mod($d, $q1);
     // calculate iqmp = 1/q mod p
     $iqmp = $this->_math_obj->invmod($q, $p);
     // store RSA keypair attributes
     $this->_attrs = array('version' => "", 'n' => $this->_math_obj->int2bin($n), 'e' => $this->_math_obj->int2bin($e), 'd' => $this->_math_obj->int2bin($d), 'p' => $this->_math_obj->int2bin($p), 'q' => $this->_math_obj->int2bin($q), 'dmp1' => $this->_math_obj->int2bin($dmp1), 'dmq1' => $this->_math_obj->int2bin($dmq1), 'iqmp' => $this->_math_obj->int2bin($iqmp));
     $n = $this->_attrs['n'];
     $e = $this->_attrs['e'];
     $d = $this->_attrs['d'];
     // try to create public key object
     $obj = new Crypt_RSA_Key($n, $e, 'public', $this->_math_obj->getWrapperName(), $this->_error_handler);
     if ($obj->isError()) {
         // error during creating public object
         $this->pushError($obj->getLastError());
         return false;
     }
     $this->_public_key =& $obj;
     // try to create private key object
     $obj = new Crypt_RSA_Key($n, $d, 'private', $this->_math_obj->getWrapperName(), $this->_error_handler);
     if ($obj->isError()) {
         // error during creating private key object
         $this->pushError($obj->getLastError());
         return false;
     }
     $this->_private_key =& $obj;
     return true;
     // key pair successfully generated
 }
Esempio n. 7
0
//print_r($res);
//die();
$num = "4271261397543976199";
$bi = new Math_BigInteger($num, 10);
$res = $bi->toBytes();
//echo $res;die();
function check_error(&$obj)
{
    if ($obj->isError()) {
        $error = $obj->getLastError();
        switch ($error->getCode()) {
            case CRYPT_RSA_ERROR_WRONG_TAIL:
                // nothing to do
                break;
            default:
                // echo error message and exit
                die('error: ' . $error->getMessage());
        }
    }
}
$open_pgp_key = "YTozOntpOjA7czoxNjoisVaMWPXkdG2K4FmUHjqQxiI7aToxO3M6ODoiB31yZ5yRRjsiO2k6MjtzOjY6InB1YmxpYyI7fQ==";
$public_key = Crypt_RSA_Key::fromString($open_pgp_key);
check_error($public_key);
$rsa_obj = new Crypt_RSA();
check_error($rsa_obj);
$ServerInfo = "ABCD2007.01.22";
//print_r($rsa_obj);die("333");
$encrypted_string = $rsa_obj->encrypt($ServerInfo, $public_key);
check_error($rsa_obj);
echo "TEST PASSED SUCCESSFULLY<br>";
echo "encrypted string='" . $encrypted_string . "'";
Esempio n. 8
0
 function decrypt($enc_text, $public_key)
 {
     $key = Crypt_RSA_Key::fromString($public_key);
     $this->_check_error($key);
     $rsa_obj = new Crypt_RSA();
     $this->_check_error($rsa_obj);
     $rsa_obj->setParams(array('dec_key' => $key));
     $this->_check_error($rsa_obj);
     $plain_text = $rsa_obj->decrypt($enc_text);
     $this->_check_error($rsa_obj);
     return $plain_text;
 }
Esempio n. 9
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";
Esempio n. 10
0
function decrypt()
{
    $enc_text = $_POST['enc_text'];
    $private_key = $_POST['private_key'];
    $key = Crypt_RSA_Key::fromString($private_key);
    check_error($key);
    $rsa_obj = new Crypt_RSA();
    check_error($rsa_obj);
    $rsa_obj->setParams(array('dec_key' => $key));
    check_error($rsa_obj);
    $_SESSION['plain_text'] = $rsa_obj->decrypt($enc_text);
    check_error($rsa_obj);
    $_SESSION['private_key'] = $private_key;
    $_SESSION['enc_text'] = $enc_text;
    header('Location: ' . $_SERVER['PHP_SELF']);
}
Esempio n. 11
0
function encrypt($message, $publicKey)
{
    $plain_text = $message;
    $public_key = $publicKey;
    $key = Crypt_RSA_Key::fromString($public_key);
    if ($key->isError()) {
        return "";
    }
    check_error($key);
    $rsa_obj = new Crypt_RSA();
    check_error($rsa_obj);
    $enc_text = $rsa_obj->encrypt($plain_text, $key);
    check_error($rsa_obj);
    return $enc_text;
}