Translates the EncryptorInterfaces class constants to the legacy
mcrypt constants

@param int $nscaName
@return string
@throws EncryptionException
768820
protected function translateCipher(int $nscaName) : string {
        switch ($nscaName) {
            case self::ENCRYPT_DES:
                return MCRYPT_DES;
            case self::ENCRYPT_3DES:
                return MCRYPT_3DES;
            case self::ENCRYPT_CAST128:
                return MCRYPT_CAST_128;
            case self::ENCRYPT_CAST256:
                return MCRYPT_CAST_256;
            case self::ENCRYPT_XTEA:
                return MCRYPT_XTEA;
            case self::ENCRYPT_3WAY:
                return MCRYPT_THREEWAY;
            case self::ENCRYPT_BLOWFISH:
                return MCRYPT_BLOWFISH;
            case self::ENCRYPT_TWOFISH:
                return MCRYPT_TWOFISH;
            case self::ENCRYPT_LOKI97:
                return MCRYPT_LOKI97;
            case self::ENCRYPT_RC2:
                return MCRYPT_RC2;
            case self::ENCRYPT_ARCFOUR:
                return MCRYPT_ARCFOUR;
            case self::ENCRYPT_RIJNDAEL128:
                return MCRYPT_RIJNDAEL_128;
            case self::ENCRYPT_RIJNDAEL192:
                return MCRYPT_RIJNDAEL_192;
            case self::ENCRYPT_RIJNDAEL256:
                return MCRYPT_RIJNDAEL_256;
            case self::ENCRYPT_WAKE:
                return MCRYPT_WAKE;
            case self::ENCRYPT_SERPENT:
                return MCRYPT_SERPENT;
            case self::ENCRYPT_ENIGMA:
                return MCRYPT_ENIGNA;
            case self::ENCRYPT_GOST:
                return MCRYPT_GOST;
            case self::ENCRYPT_SAFER64:
                return MCRYPT_SAFER64;
            case self::ENCRYPT_SAFER128:
                return MCRYPT_SAFER128;
            case self::ENCRYPT_SAFERPLUS:
                return MCRYPT_SAFERPLUS;
        }
        throw new EncryptionException('Unknown Cipher: ' . $nscaName);
    }
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()));
    }
1166583
public function encrypt($value, $serialize = true)
    {
        $iv = random_bytes(openssl_cipher_iv_length($this->cipher));

        // First we will encrypt the value using OpenSSL. After this is encrypted we
        // will proceed to calculating a MAC for the encrypted value so that this
        // value can be verified later as not having been changed by the users.
        $value = \openssl_encrypt(
            $serialize ? serialize($value) : $value,
            $this->cipher, $this->key, 0, $iv
        );

        if ($value === false) {
            throw new EncryptException('Could not encrypt the data.');
        }

        // Once we get the encrypted value we'll go ahead and base64_encode the input
        // vector and create the MAC for the encrypted value so we can then verify
        // its authenticity. Then, we'll JSON the data into the "payload" array.
        $mac = $this->hash($iv = base64_encode($iv), $value);

        $json = json_encode(compact('iv', 'value', 'mac'));

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new EncryptException('Could not encrypt the data.');
        }

        return base64_encode($json);
    }
606175
private function getEncryptedValue(DecryptRequest $request)
    {
        if (!$request->getEncryptedValue()) {
            $plaintextValue = $request->getEncryptedQuestionAsker()->askQuestion();
        } else {
            $plaintextValue = $request->getEncryptedValue();
        }

        return $plaintextValue;
    }
1139680
public function generateEncryptedMac(ComputopApiRequestTransfer $requestTransfer): string
    {
        $value = $this->getFactory()->createComputopApiMapper()->getPlaintextMac($requestTransfer);

        return $this->getHashValue($value);
    }
955922
public function encrypt(string $plainText, ContentEncryptionKey $contentEncryptionKey, Nonce $nonce, Salt $salt, PublicKey $senderPublicKey) : Cipher
    {
        $encryptedText = openssl_encrypt($plainText, 'aes-128-gcm', $contentEncryptionKey->getRawKeyMaterial(), OPENSSL_RAW_DATA, $nonce->getRawBytes(), $tag);

        $cipherText = new CipherText(new BinaryString($encryptedText));
        $tag = new AuthenticationTag(new BinaryString($tag));

        return new Cipher($cipherText, $tag, $salt, $senderPublicKey);
    }
1175449
function encrypt($plaintext)
    {
        if ($this->engine != CRYPT_ENGINE_INTERNAL) {
            return parent::encrypt($plaintext);
        }
        return $this->_crypt($plaintext, CRYPT_RC4_ENCRYPT);
    }
852100
public function encrypt($unencrypted)
    {
        if (!is_scalar($unencrypted)) {
            throw new CryptoException(sprintf(self::ERR_CANNOT_ENCRYPT, gettype($unencrypted)));
        }

        // Generate 24 byte nonce
        $nonce = random_bytes(self::NONCE_SIZE_BYTES);

        // Encrypt payload
        try {
            $encrypted = $this->sodiumSecretBox($unencrypted, $nonce, $this->cryptoSecret->getValue());
        } catch (Exception $ex) {
            throw new CryptoException(sprintf(self::ERR_ENCRYPT, $ex->getMessage()), $ex->getCode(), $ex);
        }

        // Calculate MAC
        try {
            $mac = $this->sodiumCryptoAuth($nonce . $encrypted, $this->authSecret->getValue());
        } catch (Exception $ex) {
            throw new CryptoException(sprintf(self::ERR_ENCODE, $ex->getMessage()), $ex->getCode(), $ex);
        }

        // Return appended binary string
        return $nonce . $mac . $encrypted;
    }
1213494
public function encrypt(string $message, string $key): string
    {
        $key = Key::loadFromAsciiSafeString($key);
        return 'ENC[' . self::ENCRYPTION_TYPE_DEFUSE_PHP_ENCRYPTION . ',' . Crypto::encrypt($message, $key) . ']';
    }
1274559
public function decrypt($encrypted)
    {
        if (!$encrypted || !is_string($encrypted)) {
            throw new CryptoException(sprintf(self::ERR_CANNOT_DECRYPT, gettype($encrypted)));
        }

        // Sanity check size of payload is larger than MAC + NONCE
        if (ByteString::strlen($encrypted) < self::NONCE_SIZE_BYTES + $this->sodiumCryptoBytes()) {
            throw new CryptoException(self::ERR_SIZE);
        }

        // Split into nonce, mac, and encrypted payload
        $nonce = ByteString::substr($encrypted, 0, self::NONCE_SIZE_BYTES);
        $mac = ByteString::substr($encrypted, self::NONCE_SIZE_BYTES, $this->sodiumCryptoBytes());
        $encrypted = ByteString::substr($encrypted, self::NONCE_SIZE_BYTES + $this->sodiumCryptoBytes());

        // Verify MAC
        try {
            $isVerified = $this->sodiumCryptoAuthVerify($mac, $nonce . $encrypted, $this->authSecret->getValue());
        } catch (Exception $ex) {
            throw new CryptoException(sprintf(self::ERR_DECODE_UNEXPECTED, $ex->getMessage()), $ex->getCode(), $ex);
        }

        if (!$isVerified) {
            throw new CryptoException(self::ERR_DECODE);
        }

        // Decrypt authenticated payload
        try {
            $unencrypted = $this->sodiumSecretBoxOpen($encrypted, $nonce, $this->cryptoSecret->getValue());
        } catch (Exception $ex) {
            throw new CryptoException(sprintf(self::ERR_DECRYPT, $ex->getMessage()), $ex->getCode(), $ex);
        }

        return $unencrypted;
    }
322848
public static function encrypt($plainText, $key, $cipher)
    {
        $cipher = strtolower($cipher);

        if (false === in_array($cipher, openssl_get_cipher_methods())) {
            throw new \LogicException(sprintf('Cipher %s not supported', $cipher));
        }

        $ivLen = openssl_cipher_iv_length($cipher);
        $iv = openssl_random_pseudo_bytes($ivLen);

        $cipherTextRaw = openssl_encrypt($plainText, $cipher, $key, OPENSSL_RAW_DATA, $iv);
        $hmac = hash_hmac('sha256', $cipherTextRaw, $key, true);

        return base64_encode(sprintf('%s%s%s', $iv, $hmac, $cipherTextRaw));
    }
710317
public static function EncryptRandom ($message, $password, $salt, $hmac_key, $secret_key_length = self::SECRET_KEY_LENGTH, $algo = self::DEFAULT_HASH_ALGORITHM) {
		//randamizer
		$secret_hmac_key = Hash::HmacStringStretching(rand(2, 5), microtime(true), mt_rand(), $algo);

		//passwrod1
		$password1 = static::CreateForwardPassword($message, $password, $salt, $hmac_key, $secret_key_length, $algo);
		if (strlen($password1) > 56) {
			$password1 = substr($password1, -56);
		}

		//binary passwrod 1
		$binary_passwrod_1 = static::CreateForwardBinaryPassword($message, $password, $salt, $hmac_key, $secret_key_length, $algo);

		//マスタ
		$encoded_message = base64_encode(static::EncryptMessage($message, $password1, static::GetRandomEncryptOptions($binary_passwrod_1)));

		//secret_key
		$secret_key = substr(base64_encode($secret_hmac_key), $secret_key_length, $secret_key_length);

		//passwrod2
		$password2 = Hash::HmacStringStretching($secret_key_length, $password, $secret_key);
		if (strlen($password2) > 56) {
			$password2 = substr($password2, -56);
		}

		//binary passwrod 2
		$binary_passwrod_2 = Hash::HmacBinaryStretching($secret_key_length, $password, $secret_key);

		//ダミー
		$encoded_message = base64_encode(static::EncryptMessage(CompressionGz::Compress($encoded_message), $password2, static::GetRandomEncryptOptions($binary_passwrod_2)));
		$encoded_message = rtrim($encoded_message, '=');

		//アウトプット
		$encoded_message = substr($encoded_message, 0, $secret_key_length) . $secret_key . substr($encoded_message, $secret_key_length);

		//加工
		$encoded_message_length = strlen($encoded_message);

		$encoded_message = str_pad($encoded_message, $encoded_message_length + $encoded_message_length % 4, '=');

		//処理の終了
		return str_pad($encoded_message, $encoded_message_length + $encoded_message_length % 4,  '=');
	}
609785
private function encryptBody(string $request, string $publicKey, string $nonce) : string
    {
        if (\strlen($publicKey) !== SODIUM_CRYPTO_BOX_PUBLICKEYBYTES) {
            throw new InvalidArgumentException(sprintf("Public key should be %d bytes.", SODIUM_CRYPTO_BOX_PUBLICKEYBYTES));
        }

        if (\strlen($nonce) !== SODIUM_CRYPTO_BOX_NONCEBYTES) {
            throw new InvalidArgumentException(sprintf("Nonce should be %d bytes.", SODIUM_CRYPTO_BOX_NONCEBYTES));
        }

        try {
            $keypair = new Keypair(
                $this->secretKey,
                $publicKey
            );
            return \sodium_crypto_box(
                $request,
                $nonce,
                $keypair->getSodiumKeypair()
            );
        } catch (SodiumException $e) {
            throw new InvalidArgumentException($e->getMessage());
        }
    }
1303868
private function getContentEncryptionAlgorithm(JWEInterface $jwe)
    {
        $algorithm = null;

        foreach ($jwe->getRecipients() as $recipient) {
            $complete_headers = array_merge($jwe->getSharedProtectedHeaders(), $jwe->getSharedHeaders(), $recipient->getHeaders());
            Assertion::keyExists($complete_headers, 'enc', 'Parameter "enc" is missing.');
            if (null === $algorithm) {
                $algorithm = $complete_headers['enc'];
            } else {
                Assertion::eq($algorithm, $complete_headers['enc'], 'Foreign content encryption algorithms are not allowed.');
            }
        }

        $content_encryption_algorithm = $this->getJWAManager()->getAlgorithm($algorithm);
        Assertion::isInstanceOf($content_encryption_algorithm, Algorithm\ContentEncryptionAlgorithmInterface::class, sprintf('The content encryption algorithm "%s" is not supported or not a content encryption algorithm instance.', $algorithm));

        return $content_encryption_algorithm;
    }
955873
public static function decrypt($encryptedText, $key, $cipher)
    {
        $cipher = strtolower($cipher);

        if (false === in_array($cipher, openssl_get_cipher_methods())) {
            throw new \LogicException(sprintf('Cipher %s not supported', $cipher));
        }

        $encryptedText = base64_decode($encryptedText);

        $ivLen = openssl_cipher_iv_length($cipher);
        $iv = substr($encryptedText, 0, $ivLen);

        $hmac = substr($encryptedText, $ivLen, 32);
        $cipherTextRaw = substr($encryptedText, $ivLen + 32);

        $plainText = openssl_decrypt($cipherTextRaw, $cipher, $key, OPENSSL_RAW_DATA, $iv);
        $calculatedHmac = hash_hmac('sha256', $cipherTextRaw, $key, true);

        if (false === hash_equals($hmac, $calculatedHmac)) {
            throw new \LogicException('Decrypt failed.');
        }

        return $plainText;
    }
164740
public function decrypt($encryptedToken)
    {
        $decryptedToken = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($this->key), base64_decode(urldecode($encryptedToken)), MCRYPT_MODE_CBC, md5(md5($this->key))), "\0");

        if (false === $decryptedToken ) {
            throw new \InvalidArgumentException('Invalid decrypted token. Check the encryption key');
        }

        return $decryptedToken;
    }
1153503
public static function decrypt(
        string $ciphertext,
        SharedEncryptionKey $key
    ): string {
        $nonce = \ParagonIE_Sodium_Core_Util::substr($ciphertext, 0, 24);
        $result = \ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt(
            \ParagonIE_Sodium_Core_Util::substr($ciphertext, 24),
            $nonce,
            $nonce,
            $key->getString(true)
        );
        if (!\is_string($result)) {
            throw new InvalidMessageException('Message authentication failed.');
        }
        return $result;
    }