Encrypt a string

@param string $text to encrypt
@param string $key a cryptographically random string
@param int $algo the encryption algorithm
@param int $mode the block cipher mode
@return string
14842
public static function encrypt($text, $key, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC)
        {
            // Create IV for encryption
            $iv = mcrypt_create_iv(
                mcrypt_get_iv_size(
                    $algo,
                    $mode
                ),
                MCRYPT_RAND
            );

            // Encrypt text and append IV so it can be decrypted later
            $text = mcrypt_encrypt(
                $algo,
                hash('sha256', $key, true),
                $text,
                $mode,
                $iv
            ) . $iv;

            // Prefix text with HMAC so that IV cannot be changed
            return hash(
                'sha256',
                $key . $text
            ) . $text;
        }
1125172
public function encrypt($string, $key)
    {
        $string = "ox{$string}id";

        $key = $this->_formKey($key, $string);

        $string = $string ^ $key;
        $string = base64_encode($string);
        $string = str_replace("=", "!", $string);

        return "ox_$string";
    }
370138
public String encryptText(String text)
	throws Exception
	{
		return toByteText(this.encrypt(text.getBytes(IO.CHARSET)));

	}
1044983
public static function encrypt(string $text, string $key, string $iv, int $option = OPENSSL_RAW_DATA): string
    {
        self::validateKey($key);
        self::validateIv($iv);

        return openssl_encrypt($text, self::getMode($key), $key, $option, $iv);
    }
166818
public static function blowfish($string, $key, $operation = self::ENCRYPT) {
        if ($operation === static::ENCRYPT) {
            return static::encrypt($string, $key, static::BLOWFISH);
        } else {
            return static::decrypt($string, $key, static::BLOWFISH);
        }
    }
991591
public function hash($string, $algo)
    {
        if (true === empty($string)) {
            $string = microtime() . uniqid();
        }

        return hash($algo, $string);
    }
1244930
public function encrypt($data, Key $key)
	{
		// Validate key.
		if ($key->type != $this->keyType)
		{
			throw new \InvalidArgumentException('Invalid key of type: ' . $key->type . '.  Expected ' . $this->keyType . '.');
		}

		// Encrypt the data.
		$encrypted = mcrypt_encrypt($this->type, $key->private, $data, $this->mode, $key->public);

		return $encrypted;
	}
566552
protected function decrypt($key, $text)
    {
        $td = mcrypt_module_open('blowfish', '', 'ecb', '');
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key , $iv);

        $data = mdecrypt_generic($td, $text);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);

        $data = $this->pkcs5_unpad($data);

        return $data;
    }
561935
public static function encrypt(string $input, string $method, string $key, string $iv, string &$tag): string
    {
        if (OpensslStatic::tagRequired($method)) {
            $ret = \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv, $tag, '', 4);
        } else {
            $ret = \openssl_encrypt($input, $method, $key, OPENSSL_RAW_DATA, $iv);
        }

        return self::returnOrException($ret);
    }
717330
public function write($key, $message)
    {
        $message = strval($message);
        return (strlen($key) && strlen($message))
            ? $this->_getCipherOb($key)->encrypt($message)
            : '';
    }
301842
public static function opensslSign(
        $str,
        $privateKeyPath,
        $algo = OPENSSL_ALGO_SHA1,
        $passPhrase = '',
        $format = self::FORMAT_HEX

    )
    {
        $privateKey = openssl_pkey_get_private(file_get_contents($privateKeyPath), $passPhrase);

        openssl_sign(trim($str), $singature, $privateKey, $algo);

        openssl_free_key($privateKey);

        $singature = trim($singature);

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

        return bin2hex(trim($singature));
    }
1014458
protected function _algoToCipher(CipherAlgorithmIdentifier $algo): string
    {
        $oid = $algo->oid();
        if (array_key_exists($oid, self::MAP_CIPHER_OID_TO_NAME)) {
            return self::MAP_CIPHER_OID_TO_NAME[$oid];
        }
        if (AlgorithmIdentifier::OID_RC2_CBC == $oid) {
            if (!$algo instanceof RC2CBCAlgorithmIdentifier) {
                throw new \UnexpectedValueException('Not an RC2-CBC algorithm.');
            }
            return $this->_rc2AlgoToCipher($algo);
        }
        throw new \UnexpectedValueException(
            sprintf('Cipher method %s not supported.', $algo->name()));
    }
581355
public static function opensslDecrypt(
        $encryptedStr,
        $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($encryptedStr);

        switch ($format) {
            case self::FORMAT_HEX:
                $encryptedStr = hex2bin($encryptedStr);
                break;
            case self::FORMAT_BASE64:
                $encryptedStr = EncodeHelper::base64Decode($encryptedStr);
                break;
            default:
                $encryptedStr = hex2bin($encryptedStr);
        }

        return trim(openssl_decrypt($encryptedStr, $method, $key, $options, $iv));
    }
1331862
private function encrypt($text, $corpId)
    {
        try {
            $key = $this->getAESKey();
            $random = $this->getRandomStr();
            $text = $this->encode($random.pack('N', strlen($text)).$text.$corpId);

            $iv = substr($key, 0, 16);

            $encrypted = openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);

            return base64_encode($encrypted);
        } catch (BaseException $e) {
            throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_ENCRYPT_AES);
        }
    }
133937
protected function encrypt($text): string
    {
        $length = strlen($text);
        $plain = '';
        for ($iteration = 0; $iteration < $length; $iteration += 8) {
            $lBlock = $this->asc2int(substr($text, $iteration, 4));
            $rBlock = $this->asc2int(substr($text, $iteration + 4, 4));
            list($lBlock, $rBlock) = $this->bfEncrypt($lBlock, $rBlock);
            $plain .= $this->int2asc($lBlock) . $this->int2asc($rBlock);
        }
        return $plain;
    }
477670
public static function HmacStringFromFile ($file_path, $key, $algo = self::DEFAULT_HASH_ALGORITHM) {
		return hash_hmac_file($algo, $file_path, $key, false);
	}
1056997
public function decrypt( $text, $privateKey ) {
		$this->resetHome();

		$privateKeyId = $this->addKey( $privateKey );
		if ( !$privateKeyId ) {
			return false;
		}

		$cipherFile = $this->homeDir->makeTempFile( 'ciphertext-', $text );
		$clearFile = $this->homeDir->makeTempFile( 'cleartext-' );

		$args = array( '--decrypt', '--output', $clearFile, $cipherFile );
		$output = $this->call( $args, $retval );
		if ( $retval ) {
			$this->logger->warning( 'GPG encrypt call returned error',
				array( 'retval' => $retval, 'output' => $output ) );
			return false;
		}

		$clearText = file_get_contents( $clearFile );
		if ( !$clearText ) {
			$this->logger->warning( 'failed to load cleartext file' );
			return false;
		}

		return $clearText;
	}