RSA Private Encrypt

@param $str
@param $privateKeyPath
@param string $passPhrase
@param int $padding
@param $format
@return string
325683
public static function rsaPrivateEncrypt(
        $str,
        $privateKeyPath,
        $passPhrase = '',
        $padding = OPENSSL_PKCS1_PADDING,
        $format = self::FORMAT_HEX
    )
    {
        openssl_private_encrypt(
            trim($str),
            $cryrted,
            openssl_get_privatekey(file_get_contents($privateKeyPath), $passPhrase),
            $padding
        );

        $cryrted = trim($cryrted);

        switch ($format) {
            case self::FORMAT_HEX:
                return bin2hex($cryrted);
            case self::FORMAT_BASE64:
                return EncodeHelper::base64Encode($cryrted);
        }

        return bin2hex($cryrted);
    }
890665
public function decrypt($str = '')
    {
        $str   = base64_decode($str);
        $str   = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->config[self::CSP_CONFIG]['keyEncrypt'], $str, MCRYPT_MODE_ECB);
        $block = mcrypt_get_block_size('rijndael_128', 'ecb');
        $pad   = ord($str[($len = strlen($str)) - 1]);
        $len   = strlen($str);
        $pad   = ord($str[$len - 1]);

        return substr($str, 0, strlen($str) - $pad);
    }
1365096
public function decrypt(string $str): string
    {
        return openssl_decrypt($str, self::CIPHER_METHOD, $this->key, 0, $this->getIv());
    }
1299218
public function create($str) {

    $rand = $this->psl['crypt/rand'];
    $crypto = $this->psl['crypt/crypto'];

    switch($this->method) {
      case self::BCRYPT:
        $saltRnd = $rand->str(22, $this->charsets['itoa64']);
        $salt = sprintf('%s%s$%s', self::BCRYPT, $this->bcrypt_cost, $saltRnd);
        $hash = crypt($str, $salt);
      break;

      case self::PBKDF2:
        $salt = $rand->bytes(64);
        $hash = $crypto->pbkdf2($str, $salt, $this->pbkdf2_c, $this->pbkdf2_dkLen, $this->pbkdf2_prf);

        $hash = sprintf('$pbkdf2$c=%s&dk=%s&f=%s$%s$%s',
                       $this->pbkdf2_c,
                       $this->pbkdf2_dkLen,
                       $this->pbkdf2_prf,
                       base64_encode($hash),
                       base64_encode($salt)
                       );
      break;

      case self::DRUPAL;
        $setting  = '$S$';
        $setting .= $this->charsets['itoa64'][$this->drupal_count];
        $setting .= $this->b64Encode($rand->bytes(6), 6);

        return substr($this->phpassHash($str, $setting), 0, $this->drupal_hashLen);
      break;

      case self::SHA256:
      case self::SHA512:
        $saltRnd = $rand->str(16, $this->charsets['itoa64']);
        $salt = sprintf('%srounds=%s$%s', $this->method, $this->sha2_c, $saltRnd);
        $hash = crypt($str, $salt);
      break;
    }

    if(strlen($hash) > 13) {
      return $hash;
    }
    return false;
  }
328100
public function encrypt(string $str): string
    {
        return openssl_encrypt($str, self::CIPHER_METHOD, $this->key, 0, $this->getIv());
    }
963505
public static String rsaSign(String content, String privateKey) {

        if (null == content || privateKey == null) {
            return null;
        }

        try {
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey, Base64.DEFAULT));
            KeyFactory keyf = KeyFactory.getInstance("RSA");
            PrivateKey priKey = keyf.generatePrivate(priPKCS8);
            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);
            signature.initSign(priKey);
            signature.update(content.getBytes(CHARSET));
            byte[] signed = signature.sign();
            return Base64.encodeToString(signed, Base64.DEFAULT);
        } catch (Exception e) {
            //  这里是安全算法，为避免出现异常时泄露加密信息，这里不打印具体日志
            HMSAgentLog.e("rsaSign error");
        }

        return null;
    }
1265024
public String encryptAndDecorate( final String str, final String passPhrase )
        throws PlexusCipherException
    {
        return decorate( encrypt( str, passPhrase ) );
    }
933800
public static function opensslEncrypt(
        $str,
        $key,
        $method = 'AES-256-CBC',
        $options = OPENSSL_RAW_DATA,
        $iv = self::ZERO_IV,
        $format = self::FORMAT_HEX
    )
    {
        if ($iv == self::ZERO_IV) {
            $zeroPack = pack('i*', 0);
            $iv = str_repeat($zeroPack, 4);
        }
        $encryptedStr = trim(openssl_encrypt(trim($str), $method, $key, $options, $iv));
        switch ($format) {
            case self::FORMAT_HEX:
                return bin2hex($encryptedStr);
            case self::FORMAT_BASE64:
                return EncodeHelper::base64Encode($encryptedStr);
        }

        return bin2hex($encryptedStr);
    }
179577
public static String encryptByPrivateKey(String data, String privateKey) throws Exception {
		PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(base64.decode(privateKey));
		KeyFactory keyf = KeyFactory.getInstance("RSA");
		PrivateKey priKey = keyf.generatePrivate(priPKCS8);
		java.security.Signature signature = java.security.Signature.getInstance("SHA256WithRSA");

		signature.initSign(priKey);
		signature.update(data.getBytes("UTf-8"));
		byte[] signed = signature.sign();
		return base64.encodeToString(signed);
	}
905926
public static function encryptRsaPublic(string $data, string $publicKey, string $format = null): string
    {
        $key = \openssl_pkey_get_public($publicKey);

        $encrypted = '';
        if (!\openssl_public_encrypt($data, $encrypted, $key, OPENSSL_PKCS1_OAEP_PADDING)) {
            throw new CryptoException('RSA public encrypt error: ' . openssl_error_string());
        }
        \openssl_pkey_free($key);

        return self::fromRaw($encrypted, $format);
    }
1175657
private function decryptWithPrivateKey($cipher, OpenSslRsaKey $privateKey)
    {
        $decrypted = '';
        $key = openssl_pkey_get_private($privateKey->getKeyString());
        if (openssl_private_decrypt($cipher, $decrypted, $key, $this->paddingAlgorithm) === false) {
            // Fallback for data that was encrypted with old default OPENSSL_PKCS1_PADDING
            if ($this->paddingAlgorithm !== OPENSSL_PKCS1_PADDING) {
                if (openssl_private_decrypt($cipher, $decrypted, $key, OPENSSL_PKCS1_PADDING) !== false) {
                    return $decrypted;
                }
            }
            $openSslErrors = [];
            while (($errorMessage = openssl_error_string()) !== false) {
                $openSslErrors[] = $errorMessage;
            }
            throw new SecurityException(sprintf('Decryption failed, OpenSSL error: %s', implode(chr(10), $openSslErrors)), 1556609762);
        }

        return $decrypted;
    }
1292032
public static function rsaDecrypt($content, $private_key_path) {
        $priKey = file_get_contents($private_key_path);
        $res = openssl_get_privatekey($priKey);
        //用base64将内容还原成二进制
        $content = base64_decode($content);
        //把需要解密的内容，按128位拆开解密
        $result  = '';
        for($i = 0; $i < strlen($content)/128; $i++  ) {
            $data = substr($content, $i * 128, 128);
            openssl_private_decrypt($data, $decrypt, $res);
            $result .= $decrypt;
        }
        openssl_free_key($res);
        return $result;
    }
925167
def _get_private_key_obj(private_key, passphrase=None):
    '''
    
    '''
    private_key = _text_or_file(private_key)
    private_key = get_pem_entry(private_key, pem_type='(?:RSA )?PRIVATE KEY')
    rsaprivkey = M2Crypto.RSA.load_key_string(
        private_key, callback=_passphrase_callback(passphrase))
    evpprivkey = M2Crypto.EVP.PKey()
    evpprivkey.assign_rsa(rsaprivkey)
    return evpprivkey
991964
public static function decryptRsaPublic(string $data, string $publicKey, string $format = null): string
    {
        $data = self::toRaw($data, $format);
        $key = \openssl_pkey_get_public($publicKey);

        $decrypted = '';
        if (!\openssl_public_decrypt($data, $decrypted, $key, OPENSSL_PKCS1_OAEP_PADDING)) {
            throw new CryptoException('RSA public decrypt error: ' . openssl_error_string());
        }
        \openssl_pkey_free($key);

        return $decrypted;
    }
333632
public static function encryptG($str, $secret = 'ePHP')
    {
        if (!$str) {
            return false;
        }

        $key = md5($secret . "30f7384ac1");

        $ciphertext_raw = openssl_encrypt($str, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
        return \ePHP\Misc\Func::safe_b64encode($ciphertext_raw);
    }
908294
public static function encrypt($str, $key,$expire=0) {
        $expire = sprintf('%010d', $expire ? $expire + time():0);
        $str    =   $expire.$str;
        $v = self::str2long($str, true);
        $k = self::str2long($key, false);
        $n = count($v) - 1;

        $z = $v[$n];
        $y = $v[0];
        $delta = 0x9E3779B9;
        $q = floor(6 + 52 / ($n + 1));
        $sum = 0;
        while (0 < $q--) {
            $sum = self::int32($sum + $delta);
            $e = $sum >> 2 & 3;
            for ($p = 0; $p < $n; $p++) {
                $y = $v[$p + 1];
                $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
                $z = $v[$p] = self::int32($v[$p] + $mx);
            }
            $y = $v[0];
            $mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
            $z = $v[$n] = self::int32($v[$n] + $mx);
        }
        return self::long2str($v, false);
    }
395637
public function encrypt($str, $code = self::CODE_URL)
    {
        $result = openssl_encrypt($str, $this->method, $this->key, 0, $this->iv);

        switch ($code) {
            case self::CODE_BASE64:
                $ret = base64_encode($result);
                break;
            case self::CODE_HEX:
                $ret = bin2hex($result);
                break;
            case self::CODE_BIN:
                $ret = $result;
                break;
            case self::CODE_URL:
            default:
                $ret = $this->urlEncode($result);
                break;
        }

        return $ret;
    }