Esempio n. 1
6
 /**
  * 私钥加密
  *
  * @param $dataStr
  *
  * @return string
  */
 public static function rsaPrivateEncrypt($dataStr)
 {
     $retData = '';
     $resourceId = self::getResourceId(self::TYPE_PRIVATE_KEY);
     openssl_private_encrypt($dataStr, $retData, $resourceId);
     $retData = base64_encode($retData);
     return $retData;
 }
Esempio n. 2
0
function encryptPrivate($path, $plainText)
{
    $fcontents = file_get_contents($path);
    $privateKey = openssl_pkey_get_private($fcontents, "symelosh");
    openssl_private_encrypt($plainText, $encrypted, $privateKey);
    return $encrypted;
}
Esempio n. 3
0
  function text_enc($str,$key) {
		$fpr = fopen($key,"r");
		$pr_key = fread($fpr,1024);
		fclose($fpr);
		openssl_private_encrypt($str,$result,$pr_key);
		return $result;
	}
Esempio n. 4
0
function privateKeyEncrypt($privateKey, $content)
{
    $piKey = openssl_pkey_get_private($privateKey);
    $encrypted = "";
    openssl_private_encrypt($content, $encrypted, $piKey);
    return base64_encode($encrypted);
}
Esempio n. 5
0
 /**
  * Encrypt data using this public key. Data will be decryptable
  * only with the matching private key.
  *
  * This method can only encrypt short data (= shorter than the key,
  * see the PHP manual). To encrypt larger values, use the seal()
  * method.
  *
  * @see     php://openssl_private_encrypt
  * @param   string data
  * @return  string
  * @throws  security.crypto.CryptoException if the operation fails
  */
 public function encrypt($data)
 {
     if (false === openssl_private_encrypt($data, $crypted, $this->_hdl)) {
         throw new CryptoException('Could not decrypt data', OpenSslUtil::getErrors());
     }
     return $crypted;
 }
Esempio n. 6
0
 /**
  * 私钥加密
  * @param  [type] $data [description]
  * @return [type]       [description]
  */
 public static function privEncrypt($data)
 {
     if (!is_string($data)) {
         return null;
     }
     return openssl_private_encrypt($data, $encrypted, self::getPrivateKey()) ? base64_encode($encrypted) : null;
 }
Esempio n. 7
0
 /**
  * Método que verifica el código de autorización de folios
  * @return =true si está ok el XML cargado
  * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
  * @version 2015-10-30
  */
 public function check()
 {
     // validar firma del SII sobre los folios
     $firma = $this->getFirma();
     $idk = $this->getIDK();
     if (!$firma or !$idk) {
         return false;
     }
     $pub_key = \sasco\LibreDTE\Sii::cert($idk);
     if (!$pub_key or openssl_verify($this->xml->getFlattened('/AUTORIZACION/CAF/DA'), base64_decode($firma), $pub_key) !== 1) {
         \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::FOLIOS_ERROR_FIRMA, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::FOLIOS_ERROR_FIRMA));
         return false;
     }
     // validar clave privada y pública proporcionada por el SII
     $private_key = $this->getPrivateKey();
     if (!$private_key) {
         return false;
     }
     $plain = md5(date('U'));
     if (!openssl_private_encrypt($plain, $crypt, $private_key)) {
         \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::FOLIOS_ERROR_ENCRIPTAR, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::FOLIOS_ERROR_ENCRIPTAR));
         return false;
     }
     $public_key = $this->getPublicKey();
     if (!$public_key) {
         return false;
     }
     if (!openssl_public_decrypt($crypt, $plain_firmado, $public_key)) {
         \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::FOLIOS_ERROR_DESENCRIPTAR, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::FOLIOS_ERROR_DESENCRIPTAR));
         return false;
     }
     return $plain === $plain_firmado;
 }
Esempio n. 8
0
File: Crypt.php Progetto: spinit/osy
 function enc_pri($str)
 {
     $key = uniqid();
     $res = openssl_get_privatekey($this->pri,$this->pra);
     openssl_private_encrypt($key,$cry,$res);
     $ret = $this->enc_sym($key,$str);
     return base64_encode($cry).':'.base64_encode($ret);
 }
 /**
  * Encrypt the given text with the private key
  *
  * @param KeyPair $key
  * @param string $plainText
  * @throws EncryptionException
  * @return string
  */
 public function encrypt(KeyPair $key, $plainText)
 {
     $success = openssl_private_encrypt($plainText, $result, $key->getPrivateKey());
     if ($success !== TRUE) {
         throw new EncryptionException('Encryption failed');
     }
     return $result;
 }
Esempio n. 10
0
 public function encrypt($data, $prikey)
 {
     $rs = '';
     if (@openssl_private_encrypt($data, $rs, $prikey) === FALSE) {
         return NULL;
     }
     return $rs;
 }
Esempio n. 11
0
 public static function privateEncrypt($privateKey, $data)
 {
     if (!strstr($privateKey, 'BEGIN PRIVATE KEY')) {
         $privateKey = self::lengthenPrivateKey($privateKey);
     }
     $key_resource = openssl_get_privatekey($privateKey);
     openssl_private_encrypt($data, $crypttext, $key_resource);
     return base64_encode($crypttext);
 }
Esempio n. 12
0
 public function encrypt1($data)
 {
     if (openssl_private_encrypt($data, $encrypted, $this->privkey)) {
         $data = base64_encode($encrypted);
     } else {
         echo '错误';
     }
     return $data;
 }
Esempio n. 13
0
 /**
  * Encrypts the given data.
  *
  * @param string $rawData The data to encrypt.
  * @param int $padding The padding to use for encryption.
  * @return string when the given data cannot be encrypted.
  */
 public function encrypt(string $rawData, int $padding = OPENSSL_PKCS1_PADDING) : string
 {
     OpenSSL::resetErrors();
     if (openssl_private_encrypt($rawData, $encrypted, $this->resource, $padding) === false) {
         // @codeCoverageIgnoreStart
         throw new OpenSSLException(OpenSSL::getErrors(), 'Could not encrypt the given data with this private key.');
         // @codeCoverageIgnoreEnd
     }
     return (string) $encrypted;
 }
Esempio n. 14
0
 public function encrypt($text)
 {
     if ($this->key) {
         $ret = '';
         if (openssl_private_encrypt($text, $ret, $this->key)) {
             return base64_encode($ret);
         }
     }
     return null;
 }
Esempio n. 15
0
 public static function encryptByPrivateKey($data)
 {
     $pi_key = openssl_pkey_get_private(file_get_contents(dirname(__DIR__) . '/configure/seller_rsa_private_key.pem'));
     //这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
     $encrypted = "";
     openssl_private_encrypt($data, $encrypted, $pi_key, OPENSSL_PKCS1_PADDING);
     //私钥加密
     return base64_encode($encrypted);
     //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
 }
Esempio n. 16
0
 /**
  * Rsa加密
  * @param string $data 原数据
  * @return null|string 加密结果
  */
 public function encrypt($data)
 {
     $this->_makesure_provider();
     if ($this->isPrivate) {
         $r = openssl_private_encrypt($data, $encrypted, $this->keyProvider, OPENSSL_PKCS1_PADDING);
     } else {
         $r = openssl_public_encrypt($data, $encrypted, $this->keyProvider, OPENSSL_PKCS1_PADDING);
     }
     return $r ? $data = base64_encode($encrypted) : null;
 }
Esempio n. 17
0
 public static function encryptByPrivateKey($data)
 {
     $pi_key = openssl_pkey_get_private(file_get_contents(APP_ROOT_PATH . 'system/payment/jdpay/config/seller_rsa_private_key.pem'));
     //这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
     $encrypted = "";
     openssl_private_encrypt($data, $encrypted, $pi_key, OPENSSL_PKCS1_PADDING);
     //私钥加密
     $encrypted = base64_encode($encrypted);
     //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
     return $encrypted;
 }
Esempio n. 18
0
 /**
  * * encrypt with the private key
  */
 public function privEncrypt($data)
 {
     if (!is_string($data) || empty($this->_privKey)) {
         return null;
     }
     $r = openssl_private_encrypt($data, $encrypted, $this->_privKey);
     if ($r) {
         return base64_encode($encrypted);
     }
     return null;
 }
Esempio n. 19
0
 public static function encryptByPrivateKey($data)
 {
     $pi_key = openssl_pkey_get_private(file_get_contents(getcwd() . '/WebApp/Common/Lib/Jdpay/rsa_private_key.pem'));
     //这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
     $encrypted = "";
     openssl_private_encrypt($data, $encrypted, $pi_key, OPENSSL_PKCS1_PADDING);
     //私钥加密
     $encrypted = base64_encode($encrypted);
     //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
     return $encrypted;
 }
Esempio n. 20
0
 public function privateEncrypt($message)
 {
     $key = \Clips\get_default($this, 'privateKey');
     if ($key) {
         $ret = '';
         if (openssl_private_encrypt($message, $ret, $key)) {
             return base64_encode($ret);
         }
     }
     return false;
 }
Esempio n. 21
0
 public static function encryptByPrivateKey($data)
 {
     $pi_key = openssl_pkey_get_private(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/system/modules/pay/lib/cbjpay/config/my_rsa_private_key.pem'));
     //这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
     $encrypted = "";
     openssl_private_encrypt($data, $encrypted, $pi_key, OPENSSL_PKCS1_PADDING);
     //私钥加密
     $encrypted = base64_encode($encrypted);
     //加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
     return $encrypted;
 }
Esempio n. 22
0
 public static function encrypt($data, $key, $choose = true)
 {
     if ($choose) {
         $res = openssl_pkey_get_private($key);
         openssl_private_encrypt($data, $crypt, $res);
     } else {
         $res = openssl_pkey_get_public($key);
         openssl_public_encrypt($data, $crypt, $res);
     }
     return base64_encode($crypt);
 }
Esempio n. 23
0
 /**
  * Create a passphrase file
  *
  * @param   string  $passphrase            The passphrase to store in the passphrase file.
  * @param   string  $passphraseFile        Path to the passphrase file to create.
  * @param   string  $privateKeyFile        Path to the private key file to encrypt the passphrase file.
  * @param   string  $privateKeyPassphrase  The passphrase for the private key.
  *
  * @return  boolean  Result of writing the passphrase file to disk.
  *
  * @since   12.3
  * @throws  RuntimeException
  */
 public function createPassphraseFile($passphrase, $passphraseFile, $privateKeyFile, $privateKeyPassphrase)
 {
     $privateKey = openssl_get_privatekey(file_get_contents($privateKeyFile), $privateKeyPassphrase);
     if (!$privateKey) {
         throw new RuntimeException("Failed to load private key.");
     }
     $crypted = '';
     if (!openssl_private_encrypt($passphrase, $crypted, $privateKey)) {
         throw new RuntimeException("Failed to encrypt data using private key.");
     }
     return file_put_contents($passphraseFile, $crypted);
 }
Esempio n. 24
0
    /**
     * Encrypt using this key
     *
     * @param string $data
     * @return string
     * @throws Exception\RuntimeException
     */
    public function encrypt($data)
    {
        $encrypted = '';
        $result = openssl_private_encrypt($data, $encrypted, $this->getOpensslKeyResource());
        if (false === $result) {
            throw new Exception\RuntimeException(
                'Can not encrypt; openssl ' . openssl_error_string()
            );
        }

        return $encrypted;
    }
Esempio n. 25
0
 /**
  * Encrypt using this key
  *
  * @param  string $data
  * @return string
  * @throws Exception\RuntimeException
  * @throws Exception\InvalidArgumentException
  */
 public function encrypt($data)
 {
     if (empty($data)) {
         throw new Exception\InvalidArgumentException('The data to encrypt cannot be empty');
     }
     $encrypted = '';
     $result = openssl_private_encrypt($data, $encrypted, $this->getOpensslKeyResource());
     if (false === $result) {
         throw new Exception\RuntimeException('Can not encrypt; openssl ' . openssl_error_string());
     }
     return $encrypted;
 }
Esempio n. 26
0
 function my_openssl_sign($data, &$signature, $priv_key_id, $signature_alg = 'sha256WithRSAEncryption')
 {
     $pinfo = openssl_pkey_get_details($priv_key_id);
     $hash = hash('sha256', $data);
     $t = '3031300d060960864801650304020105000420';
     # sha256
     $t .= $hash;
     $pslen = $pinfo['bits'] / 8 - (strlen($t) / 2 + 3);
     $eb = '0001' . str_repeat('FF', $pslen) . '00' . $t;
     $eb = pack('H*', $eb);
     return openssl_private_encrypt($eb, $signature, $priv_key_id, OPENSSL_NO_PADDING);
 }
Esempio n. 27
0
 /**
  * 通过私钥加密
  * @param $content 明文
  * @param $privateKey 私钥
  * @return string base64_encode后的密文
  * @throws \Exception
  */
 public static function encryptWithPrivateKey($content, $privateKey)
 {
     self::isSupportRSA();
     $pi_key = openssl_pkey_get_private($privateKey);
     //这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
     if (empty($pi_key)) {
         throw new \Exception("私钥不可用!");
     }
     $encrypted = "";
     openssl_private_encrypt($content, $encrypted, $pi_key);
     //私钥加密
     return base64_encode($encrypted);
 }
Esempio n. 28
0
 /**
  * 基于私钥加密
  *
  * @param string $code 加密字符串
  * @param string $file 私钥文件绝对路径
  *
  * @return string 返回基于私钥加密的字符串
  */
 public static function opensslPrivateEncrypt($code, $file)
 {
     $encrypt = '';
     if (!file_exists($file)) {
         return $encrypt;
     }
     $pri_key = openssl_pkey_get_private('file://' . $file);
     if (!$pri_key) {
         return $encrypt;
     }
     openssl_private_encrypt($code, $encrypt, $pri_key);
     return base64_encode($encrypt);
 }
Esempio n. 29
0
 public function sign(Document $object, $passphrase)
 {
     $document = $object->toDocument();
     $hash = hash('sha256', $this->signedDateTime . " " . $document);
     $userKey = new UserKey();
     $userKey->userId = $this->signingUserId;
     $userKey->populate();
     $privateKeyString = $userKey->getDecryptedPrivateKey($passphrase);
     $privateKey = openssl_pkey_get_private($privateKeyString);
     openssl_private_encrypt($hash, $signedHash, $privateKey);
     $this->signature = base64_encode($signedHash);
     openssl_free_key($privateKey);
 }
 private function testKeys($params)
 {
     $str = 'test string';
     if (!function_exists('openssl_public_decrypt')) {
         // зашифруем строку
         openssl_private_encrypt($str, $sign, $params['private']);
         // проверим подпись
         openssl_public_decrypt($sign, $str2, $params['public']);
         $ret = $str == $str2;
     } else {
         set_include_path(get_include_path() . PATH_SEPARATOR . WPAdm_Core::getPluginDir() . '/modules/phpseclib');
         require_once 'Crypt/RSA.php';
         // зашифруем строку
         define('CRYPT_RSA_PKCS15_COMPAT', true);
         $rsa = new Crypt_RSA();
         $rsa->loadKey($params['private']);
         $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
         $ciphertext = $rsa->encrypt($str);
         // проверим подпись
         $rsa = new Crypt_RSA();
         $rsa->loadKey($params['public']);
         $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
         $ret = $str == $rsa->decrypt($ciphertext);
     }
     $this->result->setResult(WPAdm_result::WPADM_RESULT_SUCCESS);
     $this->result->setData(array('match' => (int) $ret));
 }