Get Osf Crypt object. Parameters are usefull ONLY for the first call
@param string $cryptKey
@param string $mode
@return \Osf\Crypt\Crypt
351392
public static function getCrypt($cryptKey = Crypt::DEFAULT_KEY, $mode = Crypt::MODE_ASCII): \Osf\Crypt\Crypt
    {
        return self::buildObject('\Osf\Crypt\Crypt', [$cryptKey, $mode]);
    }
1145311
public static function decrypt(
        string $cipherText,
        string $key,
        string $ad = '',
        string $format = null,
        $password = false
    ): string {
        $cipherText = self::toRaw($cipherText, $format);
        if ($cipherText === false) {
            throw new CryptoException('Ciphertext has invalid base64 encoding.');
        }

        if (\mb_strlen($cipherText, '8bit') < self::MINIMUM_CIPHERTEXT_SIZE) {
            throw new CryptoException('Ciphertext is too short.');
        }

        // Get and check the version header.
        $header = \mb_substr($cipherText, 0, self::HEADER_VERSION_SIZE, '8bit');
        if ($header !== self::CURRENT_VERSION) {
            throw new CryptoException('Bad version header.');
        }

        // Get the salt.
        $salt = \mb_substr(
            $cipherText,
            self::HEADER_VERSION_SIZE,
            self::SALT_BYTE_SIZE,
            '8bit'
        );
        if ($salt === false) {
            throw new CryptoException('Environment is broken');
        }

        // Get the IV.
        $iv = \mb_substr(
            $cipherText,
            self::HEADER_VERSION_SIZE + self::SALT_BYTE_SIZE,
            self::BLOCK_BYTE_SIZE,
            '8bit'
        );
        if ($iv === false) {
            throw new CryptoException('Environment is broken');
        }

        // Get the HMAC.
        $hmac = \mb_substr(
            $cipherText,
            \mb_strlen($cipherText, '8bit') - self::MAC_BYTE_SIZE,
            self::MAC_BYTE_SIZE,
            '8bit'
        );
        if ($hmac === false) {
            throw new CryptoException('Environment is broken');
        }

        // Get the actual encrypted ciphertext.
        $encrypted = \mb_substr(
            $cipherText,
            self::HEADER_VERSION_SIZE + self::SALT_BYTE_SIZE +
            self::BLOCK_BYTE_SIZE,
            \mb_strlen($cipherText, '8bit') - self::MAC_BYTE_SIZE - self::SALT_BYTE_SIZE -
            self::BLOCK_BYTE_SIZE - self::HEADER_VERSION_SIZE,
            '8bit'
        );
        if ($encrypted === false) {
            throw new CryptoException('Environment is broken');
        }
        // Derive the separate encryption and authentication keys from the key
        // or password, whichever it is.
        [$authKey, $encryptKey] = self::deriveKeys($key, $salt, $password);

        if (false === \hash_equals($hmac,
                \hash_hmac(self::HASH_TYPE, $header . $salt . $iv . $encrypted, $authKey, true))
        ) {
            throw new CryptoException('Integrity check failed.');
        }

        $ad = self::getAd($ad);

        if (self::$sodium) {
            $plaintext = \sodium_crypto_aead_aes256gcm_decrypt($encrypted, $ad, $iv, $encryptKey);
        } else {
            $tag = \mb_substr($encrypted, -self::TAG_BYTE_SIZE, null, '8bit');
            $encrypted = \mb_substr($encrypted, 0, -self::TAG_BYTE_SIZE, '8bit');

            $plaintext = \openssl_decrypt(
                $encrypted,
                self::CIPHER_METHOD,
                $encryptKey,
                \OPENSSL_RAW_DATA,
                $iv, $tag, $ad
            );
        }

        if ($plaintext === false) {
            throw new CryptoException('Decrypt failed.');
        }

        return $plaintext;
    }
1342899
public static function decryptExpressiveConfig($config, $cryptKey = null): callable
    {
        // Return as a generator to deal with merging individual file configs correctly.
        return function () use ($config, $cryptKey) {
            $crypt = new self();
            $decryptConfig = function ($data, $dataKey = null) use (&$decryptConfig, $crypt, $cryptKey) {
                if (is_array($data)) {
                    foreach ($data as $key => &$value) {
                        if ($dataKey) {
                            $dataKey .= "/$key";
                        } else {
                            $dataKey = $key;
                        }
                        $value = $decryptConfig($value, $dataKey);
                    }
                    unset($value);
                } else {
                    if (!is_null($cryptKey) && preg_match('/^ENC\[[^,]+,.*\]/', $data)) {
                        try {
                            $data = $crypt->decrypt($data, $cryptKey);
                        } catch (\Exception $e) {
                            $message = "Error decrypting configuration key \"$dataKey\".";
                            throw new Exception\RuntimeException($message, 0, $e);
                        }
                    }
                }
                return $data;
            };

            if (is_callable($config)) {
                foreach ($config() as $data) {
                    yield $decryptConfig($data);
                }
            } else {
                yield $decryptConfig($config);
            }
        };
    }
914976
private function getKey(array $config, $password)
    {
        $key = rtrim(file_get_contents($config['rsa_key']));
        $rsa = new \Crypt_RSA();
        if (null !== $password) {
            $rsa->setPassword($password);
        }
        $rsa->loadKey($key);

        return $rsa;
    }
964050
def getCiphertext(self, version, messageKeys, plainText):
        
        cipher = None
        if version >= 3:
            cipher = self.getCipher(messageKeys.getCipherKey(), messageKeys.getIv())
        else:
            cipher = self.getCipher_v2(messageKeys.getCipherKey(), messageKeys.getCounter())

        return cipher.encrypt(bytes(plainText))
490668
private static function decryptStandard(
        $hash,
        $crypt_text,
        $method,
        $iv
    ) {
        if (substr($iv, 0, strlen($method)) !== $method) {
            return null;
        }

        /**
         * ------------------------------------
         * Doing convert encrypted string
         * ------------------------------------
         */
        /**
         * Doing decode of input encryption
         */
        $crypt_text = self::safeBase64Decode($crypt_text);
        $enc_arr  = str_split($crypt_text);
        $add = 0;
        $div = strlen($crypt_text) / strlen($hash);
        $new_pass = '';
        while ($add <= $div) {
            $new_pass .= $hash;
            $add++;
        }
        $pass_arr = str_split($new_pass);
        $ascii ='';
        foreach ($enc_arr as $key => $asc) {
            $pass_int = ord($pass_arr[$key]);
            $enc_int = ord($asc);
            $str_int = $enc_int - $pass_int;
            $ascii .= chr(($str_int-strlen($crypt_text)));
        }

        /* --------------------------------
         * reversing
         * ------------------------------ */
        // unpack
        $unpack     = unpack('a*', trim($ascii));

        /**
         * if empty return here
         */
        if (empty($unpack)) {
            return null;
        }

        // implode the unpacking array
        $unpack = implode('', (array) $unpack);
        $iv     = substr($iv, strlen($method));
        if (strlen($hash) < strlen($unpack)) {
            $sub_iv = substr($unpack, strlen($hash), strlen($iv));
        } else {
            $sub_iv = substr($unpack, -strlen($iv));
        }
        if ($sub_iv !== $iv) {
            return null;
        }
        $crypt_text = substr_replace($unpack, '', strlen($hash), strlen($iv));
        return self::rotate($crypt_text, -(strlen($hash) % 13));
    }
440605
function loadX509($cert, $mode = self::FORMAT_AUTO_DETECT)
    {
        if (is_array($cert) && isset($cert['tbsCertificate'])) {
            unset($this->currentCert);
            unset($this->currentKeyIdentifier);
            $this->dn = $cert['tbsCertificate']['subject'];
            if (!isset($this->dn)) {
                return false;
            }
            $this->currentCert = $cert;

            $currentKeyIdentifier = $this->getExtension('id-ce-subjectKeyIdentifier');
            $this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : null;

            unset($this->signatureSubject);

            return $cert;
        }

        $asn1 = new ASN1();

        if ($mode != self::FORMAT_DER) {
            $newcert = $this->_extractBER($cert);
            if ($mode == self::FORMAT_PEM && $cert == $newcert) {
                return false;
            }
            $cert = $newcert;
        }

        if ($cert === false) {
            $this->currentCert = false;
            return false;
        }

        $asn1->loadOIDs($this->oids);
        $decoded = $asn1->decodeBER($cert);

        if (!empty($decoded)) {
            $x509 = $asn1->asn1map($decoded[0], $this->Certificate);
        }
        if (!isset($x509) || $x509 === false) {
            $this->currentCert = false;
            return false;
        }

        $this->signatureSubject = substr($cert, $decoded[0]['content'][0]['start'], $decoded[0]['content'][0]['length']);

        if ($this->_isSubArrayValid($x509, 'tbsCertificate/extensions')) {
            $this->_mapInExtensions($x509, 'tbsCertificate/extensions', $asn1);
        }
        $this->_mapInDNs($x509, 'tbsCertificate/issuer/rdnSequence', $asn1);
        $this->_mapInDNs($x509, 'tbsCertificate/subject/rdnSequence', $asn1);

        $key = &$x509['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'];
        $key = $this->_reformatKey($x509['tbsCertificate']['subjectPublicKeyInfo']['algorithm']['algorithm'], $key);

        $this->currentCert = $x509;
        $this->dn = $x509['tbsCertificate']['subject'];

        $currentKeyIdentifier = $this->getExtension('id-ce-subjectKeyIdentifier');
        $this->currentKeyIdentifier = is_string($currentKeyIdentifier) ? $currentKeyIdentifier : null;

        return $x509;
    }
1280381
public static function crypt(string $str, string $key): string
    {
        $s = self::initializeState($key);
        $i = $j = 0;
        $res = '';
        $size = Str::strlen($str);
        for ($y = 0; $y < $size; $y++) {
            $i = ($i + 1) % 256;
            $j = ($j + $s[$i]) % 256;
            $x = $s[$i];
            $s[$i] = $s[$j];
            $s[$j] = $x;
            $res .= $str[$y] ^ \chr($s[($s[$i] + $s[$j]) % 256]);
        }

        return $res;
    }
230350
final static public function get_secret_key() {
        global $CFG;

        if (!isset($CFG->reposecretkey)) {
            set_config('reposecretkey', time() . random_string(32));
        }
        return $CFG->reposecretkey;
    }
824437
public static function get($key, $default = null)
    {
        if (static::has($key)) {
            return Crypto::decrypt($_COOKIE[$key]);
        }

        if (is_callable($default)) {
            return $default();
        }

        return  $default;
    }
490759
function sign($text, $key, $mode = null)
    {
        try {
            $this->gpg->addSignKey($key->reference, $key->password);

            $res     = $this->gpg->sign($text, $mode, CRYPT_GPG::ARMOR_ASCII, true);
            $sigInfo = $this->gpg->getLastSignatureInfo();

            $this->last_sig_algorithm = $sigInfo->getHashAlgorithmName();

            return $res;
        }
        catch (Exception $e) {
            return $this->get_error_from_exception($e);
        }
    }
632809
private byte[] doEncryptionOrDecryption(byte[] crypt, Key key, int mode) {
        Cipher rsaCipher;
        try {
            rsaCipher = Cipher.getInstance(CIPHER);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw SeedException.wrap(e, CryptoErrorCode.UNABLE_TO_GET_CIPHER)
                    .put("alias", alias)
                    .put("cipher", CIPHER);
        }
        try {
            rsaCipher.init(mode, key);
        } catch (InvalidKeyException e) {
            throw SeedException.wrap(e, CryptoErrorCode.INVALID_KEY)
                    .put("alias", alias);
        }
        try {
            return rsaCipher.doFinal(crypt);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw SeedException.wrap(e, CryptoErrorCode.UNEXPECTED_EXCEPTION);
        }
    }
977210
public static final String decrypt(String secretKey, String encryptedText) {
        if (encryptedText == null) {
            return null;
        }
        if (encryptedText.startsWith("$CRYPT::")) { //$NON-NLS-1$
            byte[] decoded = Base64.decodeBase64(encryptedText.substring(8));
            Cipher cipher;
            try {
                SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);
                
                cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
                cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
                throw new RuntimeException(e);
            }
            try {
                return new String(cipher.doFinal(decoded));
            } catch (IllegalBlockSizeException | BadPaddingException e) {
                throw new RuntimeException(e);
            }
        } else {
            return encryptedText;
        }
    }
1292789
private static function cryptStandard(
        $hash,
        $crypt_text,
        $method = self::DEFAULT_CHIPPER
    ) {
        /**
         * ------------------------------------
         * Doing convert string
         * ------------------------------------
         */
        $crypt_text = self::rotate($crypt_text, (strlen($hash) % 13));
        $add = 0;
        $iv = self::safeBase64Decode(sha1($method . @time()));
        if (strlen($hash) < strlen($crypt_text)) {
            $crypt_text = substr_replace($crypt_text, $iv, strlen($hash), 0);
        } else {
            $crypt_text = $crypt_text . $iv;
        }
        $div = strlen($crypt_text) / strlen($hash);
        $new_pass = '';
        while ($add <= $div) {
            $new_pass .= $hash;
            $add++;
        }

        $pass_arr = str_split($new_pass);
        $str_arr  = str_split($crypt_text);
        $ascii = '';
        foreach ($str_arr as $key => $asc) {
            $pass_int = ord($pass_arr[$key]);
            $str_int = ord($asc);
            $int_add = $str_int + $pass_int;
            $ascii .= chr(($int_add+strlen($crypt_text)));
        }

        return [
            self::safeBase64Encode($ascii),
            $method, // for what?
            $iv
        ];
    }
1146283
public static function EncryptFile ($file_path, $key, $options = []) {
		$options = static::_AdjustOptions($options);
		return openssl_encrypt(file_get_contents($file_path), $options['method'], $key, $options['openssl_options'], $options['iv']);
	}
1241462
public static function getRsa($mode)
    {
        if (self::$rsa) {
            $rsa = self::$rsa;
        } else {
            $rsa = new RSA();
            $rsa->setMGFHash('sha256');
        }

        $rsa->setSignatureMode($mode);

        return $rsa;
    }
1137896
public function decrypt(string $ciphertext, string $key): string
    {
        $numMatches = preg_match_all('%^ENC\[([^,]+),(.*)\]$%', $ciphertext, $matches);
        if (0 == $numMatches) {
            throw new Exception\RuntimeException(sprintf(
                "\$ciphertext must be in the format ENC[\$encryptionType,\$ciphertext].\n"
                . "Provided ciphertext: %s",
                $ciphertext
            ));
        }

        $encryptionType = $matches[1][0];
        $ciphertext = $matches[2][0];

        switch ($encryptionType) {
            case self::ENCRYPTION_TYPE_DEFUSE_PHP_ENCRYPTION:
                $key = Key::loadFromAsciiSafeString($key);
                return Crypto::decrypt($ciphertext, $key);

            default:
                throw new Exception\RuntimeException(sprintf(
                    'Unsupported encryption type "%s". Supported encryption types: "%s".',
                    $encryptionType,
                    implode('", "', [self::ENCRYPTION_TYPE_DEFUSE_PHP_ENCRYPTION])
                ));
        }


    }