Encrypt and decrypt a string using the Rijndael 128 algorithm with CBC mode.

@param string $string
@param string $key
@param int $operation
@return string
548624
public static function rijndael($string, $key, $operation = self::ENCRYPT) {
        if ($operation === static::ENCRYPT) {
            return static::encrypt($string, $key, static::RIJNDAEL);
        } else {
            return static::decrypt($string, $key, static::RIJNDAEL);
        }
    }
160233
public static function decrypt(RSAKey $key, $ciphertext, $hash_algorithm)
    {
        Assertion::greaterThan($key->getModulusLength(), 0);
        $hash = Hash::$hash_algorithm();
        $ciphertext = str_split($ciphertext, $key->getModulusLength());
        $ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $key->getModulusLength(), chr(0), STR_PAD_LEFT);
        $plaintext = '';
        foreach ($ciphertext as $c) {
            $temp = self::getRSAESOAEP($key, $c, $hash);
            $plaintext .= $temp;
        }

        return $plaintext;
    }
922748
public static function decrypt($string, $key, $cipher, $mode = MCRYPT_MODE_CBC) {
        list($key, $iv) = static::vector($key, $cipher, $mode);

        return rtrim(mcrypt_decrypt($cipher, $key, $string, $mode, $iv), "\0");
    }
1332672
public static function encrypt(string $data, string $secret='', string $algorithm='AES-256-CBC')
  {
    // # If AES we will add a random 16 byte IV before the encrypted data
    // if ( strtoupper(substr($algorithm,0,3))==='AES' ) {
    //   $iv = openssl_random_pseudo_bytes(16);
    // } else {
    //   $iv = '';
    // }
    # Add a random Initialization Vector
    $iv = \openssl_random_pseudo_bytes(16);

    # If no secret provided, use the one in config
    if (empty($secret))
      $secret = \config()->get('application.secret');

    # Encrypt
    $result = \openssl_encrypt($data,$algorithm,$secret,0,$iv);

    if ( !$result===false ) {
      return \base64_encode($iv.$result);
    } else {
      return null;
    }

  }
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;
    }
197734
public static function decrypt($text, $key, $algo = MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_CBC)
        {
            $hash = substr($text, 0, 64);
            $text = substr($text, 64);

            // Invalid HMAC?
            if(hash('sha256', $key . $text) != $hash) {
                return;
            }

            // Get IV off end of encrypted string
            $iv = substr($text, -mcrypt_get_iv_size($algo, $mode));

            // Decrypt string using IV and remove trailing \x0 padding added by mcrypt
            return rtrim(
                mcrypt_decrypt(
                    $algo,
                    hash('sha256', $key, true),
                    substr($text, 0, -strlen($iv)),
                    $mode,
                    $iv
                ),
                "\x0"
            );
        }
978382
public static function decrypt($message, $key, $encoded = false)
    {
        if ($encoded) {
            $message = base64_decode($message, true);
            if ($message === false) {
                throw new Exception('Encryption failure');
            }
        }

        $nonceSize = openssl_cipher_iv_length(self::METHOD);
        $nonce = mb_substr($message, 0, $nonceSize, '8bit');
        $ciphertext = mb_substr($message, $nonceSize, null, '8bit');

        $plaintext = openssl_decrypt(
            $ciphertext,
            self::METHOD,
            $key,
            OPENSSL_RAW_DATA,
            $nonce
        );

        return $plaintext;
    }
370296
public function decrypt($text, $key)
    {
        $ivSize = mcrypt_enc_get_block_size($this->_mcrypt);

        if (strlen($text) < $ivSize * 3) {
            throw new CryptException('encrypted data is too short.');
        }

        $encryptKey = md5($key, true);

        mcrypt_generic_init($this->_mcrypt, $encryptKey, substr($text, 0, $ivSize));

        $decrypted = mdecrypt_generic($this->_mcrypt, substr($text, $ivSize));
        $length = unpack('N', $decrypted)[1];

        if ($length < 16 || 4 + $length > strlen($decrypted)) {
            throw new CryptException('decrypted data length is too short.');
        }

        $decrypted = substr($decrypted, 4, $length);
        $plainText = substr($decrypted, 0, -16);

        if (md5($plainText, true) !== substr($decrypted, -16)) {
            throw new CryptException('decrypted md5 is not valid.');
        }

        return $plainText;
    }
370610
public static function decrypt(string $data, string $secret='', string $algorithm='AES-256-CBC')
  {
    # if AES we extract the 16 bytes in the beginning as the IV
    if ( strtoupper(substr($algorithm,0,3))==='AES' ) {
      $encoded  = base64_decode($data);
      $iv       = substr($encoded,0,16);
      $encoded  = substr($encoded,16);
    } else {
      $encoded = $data;
      $iv = '';
    }

    # If no secret provided, use the one in config
    if (empty($secret))
      $secret = config()->get('application.secret');

    # Decrypt
    $result = openssl_decrypt($encoded,$algorithm,$secret,0,$iv);

    return $result;
  }
994489
protected function doDecrypt($data, $key, $iv)
    {
        return openssl_decrypt($data, $this->getMethod(), $key, OPENSSL_RAW_DATA, $iv);
    }
669122
public function decrypt($data, $key) : string {
      $this->validateData($data);
      $this->validateKey($key);
     
      if (empty($data)) {
          return '';
      }
 
      list($encrypted, $iv) = explode(self::ENCRYPTED_IV_SEPERATOR, base64_decode($data), 2);

      return openssl_decrypt($encrypted, self::CIPHER_METHOD, $key, 0, $iv);
    }
266789
public static function decrypt(string $cipherText, string $key, string $iv, int $option = OPENSSL_RAW_DATA, $method = null): string
    {
        self::validateKey($key);
        self::validateIv($iv);

        return openssl_decrypt($cipherText, $method ?: self::getMode($key), $key, $option, $iv);
    }
21863
function decrypt($encryptedText)
    {
        $parts = explode(':', $encryptedText);

        if (count($parts) != 2) {
            throw new DecryptionException();
        }

        $main = base64_decode($parts[0]);
        $iv = base64_decode($parts[1]);
        return openssl_decrypt($main, $this->method, $this->key, $this->options, $iv);
    }
161261
public function decrypt($data, $key = null, $iv = null)
    {
        include_once __DIR__ . '/../lib/aes.class.php';

        return \AesCtr::decrypt(base64_decode($data), $key, $this->keyLength);
    }
545245
public function decrypt($crypttext, $key = null) {
        if(null !== $key) {
            $this->_key = md5($key);
        }

        $crypttext = base64_decode($crypttext);
        $plaintext = '';
        $td        = mcrypt_module_open($this->_cypher, '', self::MODE, '');
        $ivsize    = mcrypt_enc_get_iv_size($td);
        $iv        = substr($crypttext, 0, $ivsize);
        $crypttext = substr($crypttext, $ivsize);
        if ($iv) {
            mcrypt_generic_init($td, $this->_key, $iv);
            $plaintext = mdecrypt_generic($td, $crypttext);
        }
        return trim($plaintext);
    }
1094167
public function decrypt($text, $key = null)
    {
        if (!function_exists('openssl_cipher_iv_length')) {
            throw new RuntimeException('openssl extension is required');
        }

        if ($key === null) {
            $decryptKey = $this->key;
        } else {
            $decryptKey = $key;
        }

        if (empty($decryptKey)) {
            throw new InvalidArgumentException('Decryption key cannot be empty');
        }

        $cipher = $this->cipher;
        $mode   = strtolower(substr($cipher, strrpos($cipher, '-') - strlen($cipher)));

        if (!in_array($cipher, openssl_get_cipher_methods(true))) {
            throw new UnexpectedValueException("Cipher algorithm is unknown [$cipher]");
        }

        $ivSize = openssl_cipher_iv_length($cipher);
        if ($ivSize > 0) {
            $blockSize = $ivSize;
        } else {
            $blockSize = openssl_cipher_iv_length(str_ireplace('-' . $mode, '', $cipher));
        }

        $decrypted = openssl_decrypt(substr($text, $ivSize), $cipher, $decryptKey, OPENSSL_RAW_DATA, substr($text, 0, $ivSize));

        $paddingType = $this->padding;

        if ($mode === 'cbc' || $mode === 'ecb') {
            return $this->unpadText($decrypted, $mode, $blockSize, $paddingType);
        }

        return $decrypted;
    }
328834
function decrypt(key, iv, data) {
    if(!Buffer.isBuffer(key)) {
        assert(typeof key === 'string');
        key = Buffer.from(key, 'binary')
    }

    if(!Buffer.isBuffer(iv)) {
        assert(typeof iv === 'string');
        iv = Buffer.from(iv, 'binary')
    }

    if(!Buffer.isBuffer(data)) {
        assert(typeof data === 'string');
        data = Buffer.from(data, 'binary')
    }

    let ret = Buffer.alloc(data.length/2);
    for(let i = 0; i < ret.length; i++){
        ret[i] = ((data[i*2]-97)<<4) | (data[i*2+1]-97);
    }

    var decoded = aes.decipher(ret, key, iv);
    return decoded.toString('binary');
}