对密文进行解密

@param string $encrypted 需要解密的密文
@param string $appId     app id

@return string 解密得到的明文
548913
public function decrypt($encrypted, $appId)
    {
        try {
            //使用BASE64对需要解密的字符串进行解码
            $ciphertext = base64_decode($encrypted, true);
            $module     = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
            $iv         = substr($this->AESKey, 0, 16);

            mcrypt_generic_init($module, $this->AESKey, $iv);

            //解密
            $decrypted = mdecrypt_generic($module, $ciphertext);
            mcrypt_generic_deinit($module);
            mcrypt_module_close($module);
        } catch (Exception $e) {
            throw new Exception($e->getMessage(), self::ERROR_DECRYPT_AES);
        }

        try {
            //去除补位字符
            $result = $this->decode($decrypted);

            //去除16位随机字符串,网络字节序和AppId
            if (strlen($result) < 16) {
                return '';
            }

            $content   = substr($result, 16, strlen($result));
            $listLen   = unpack('N', substr($content, 0, 4));
            $xmlLen    = $listLen[1];
            $xml       = substr($content, 4, $xmlLen);
            $fromAppId = trim(substr($content, $xmlLen + 4));
        } catch (Exception $e) {
            throw new Exception($e->getMessage(), self::ERROR_INVALID_XML);
        }

        if ($fromAppId !== $appId) {
            throw new Exception('Invalid appId.', self::ERROR_INVALID_APPID);
        }

        return $xml;
    }
1165919
public function Decrypt($encryptedData) {
        $decoded = base64_decode($encryptedData, true);
        $decrypted = mcrypt_decrypt($this->encryptionType, $this->encryptionKey, $decoded, $this->encryptionMode, $this->iv);
        return $decrypted;
    }
1193149
public function getUidAppid($uid = '', $type = "all") {
        $key  = md5($uid . md5($uid . 'thirdAuthorizeCallback') . "_wechat");
        $data = to_array(F($key, '[get]'));
        if ($data) {
            switch ($type) {
            case 'appid':
                return $data['authorizer_appid'];
                break;
            default:
                return $data;
                break;
            }
        } else {
            return false;
        }
    }
1371219
public static function decrypt($data) : string {
      $crypter = new Crypter();

      return $crypter->decrypt($data, self::getAppKey());
    }
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;
    }
87240
public function encrypt(string $decrypted): string {
        $status = openssl_public_encrypt($decrypted, $encrypted, $this);
        if(!$status) {
            throw new RuntimeException('Encrypt failed');
        }
        return $encrypted;
    }
1285544
public function encrypt(string $decrypted): string {
        $status = openssl_private_encrypt($decrypted, $encrypted, $this);
        if(!$status) {
            throw new RuntimeException('Encrypt failed');
        }
        return $encrypted;
    }
794830
private function getCipherMethod($encType = CryptographyProviderInterface::PROPERTY_ENCRYPTION)
    {
        $method = $this->settings['cipher_method'][$encType];
        $supportedMethods = openssl_get_cipher_methods();

        if (!in_array($method, $supportedMethods)) {
            throw new EncryptionException('Method '.$method.' not supported by openssl installation.');
        }

        return $method;
    }
564631
public static function encrypt($data = '', $key = '', $expire = 0): string
    {
        $key = md5(empty($key) ? \Yii::$app->getRequest()->cookieValidationKey : $key);
        $data = base64_encode($data);
        $x = 0;
        $len = strlen($data);
        $l = strlen($key);
        $char = '';
        
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }
        
        $str = sprintf('%010d', $expire ? $expire + time() : 0);
        
        for ($i = 0; $i < $len; $i++) {
            $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
        }
        
        return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($str));
    }
527734
public function decode($encrypted)
    {

        //$cipher_all = explode("/", $cipher_in);
        //$cipher = $cipher_all[0];

        $blocks = explode("|x", $encrypted);
        $delimiter = array_search("::", $blocks);


        $cipherStream = array_slice($blocks, 0, (int)$delimiter);


        unset($blocks[(int)$delimiter]);
        unset($blocks[(int)$delimiter + 1]);

        $publicKeyArray = array_slice($blocks, (int)$delimiter);

        $publicKey = implode('', $publicKeyArray);
        $privateKey = $this->key;

        $hashKey = sha1($privateKey . "+" . (string)$publicKey);
        $hashArray = str_split($hashKey);

        $counter = 0;
        for ($i = 0; $i < sizeof($cipherStream); $i++) {
            if ($counter > 40)
                $counter = 0;
            $cryptChar = hexdec($cipherStream[$i]) - ord((string)$hashArray[$counter]);
            $cryptChar -= floor($cryptChar / 127) * 127;
            $cipherText[$i] = chr($cryptChar);
            $counter++;
        }

        $plaintext = implode("", $cipherText);

        return $plaintext;
    }
949763
private function encrypt($data)
	{
		$key = $this->_config['appSecret'];
		// encrypt
		$key    =   md5($key);
	    $x      =   0;
	    $len    =   strlen($data);
	    $l      =   strlen($key);
	    for ($i = 0; $i < $len; $i++)
	    {
	        if ($x == $l) 
	        {
	            $x = 0;
	        }
	        $char .= $key{$x};
	        $x++;
	    }
	    for ($i = 0; $i < $len; $i++)
	    {
	        $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
	    }
	    return base64_encode($str);
	}
254786
public function encrypt($text, $appid)
    {
        try {
            // 获得16位随机字符串，填充到明文之前
            $random = $this->getRandomStr();
            $text = $random . pack("N", strlen($text)) . $text . $appid;
            // 网络字节序
            $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
            $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
            $iv = substr($this->key, 0, 16);
            // 使用自定义的填充方式对明文进行补位填充
            $pkc_encoder = new PKCS7Encoder();
            $text = $pkc_encoder->encode($text);
            mcrypt_generic_init($module, $this->key, $iv);
            // 加密
            $encrypted = mcrypt_generic($module, $text);
            mcrypt_generic_deinit($module);
            mcrypt_module_close($module);
            
            // print(base64_encode($encrypted));
            // 使用BASE64对加密后的字符串进行编码
            return array(
                ErrorCode::$OK,
                base64_encode($encrypted)
            );
        } catch (\Exception $e) {
            // print $e;
            return array(
                ErrorCode::$EncryptAESError,
                null
            );
        }
    }
923408
def decrypt(self, enctext, key_file=None, id_attr=''):
        
        if not id_attr:
            id_attr = self.id_attr

        if not isinstance(key_file, list):
            key_file = [key_file]

        key_files = [
            key for key in itertools.chain(key_file, self.enc_key_files) if key
        ]
        for key_file in key_files:
            try:
                dectext = self.crypto.decrypt(enctext, key_file, id_attr)
            except XmlsecError as e:
                continue
            else:
                if dectext:
                    return dectext

        errmsg = "No key was able to decrypt the ciphertext. Keys tried: {keys}"
        errmsg = errmsg.format(keys=key_files)
        raise DecryptError(errmsg)
1313432
static function Encrypt($data, $key, $encode = true)
	{
		McryptUtil::Init();
		
		$encrypted = mcrypt_encrypt(McryptUtil::$CIPHER, $key, $data, McryptUtil::$MODE, McryptUtil::$IV);
		
		return ($encode) ? base64_encode($encrypted) : $encrypted;
	}
111023
public function decrypt($encrypted, $key)
	{
		if (empty($encrypted))
			return null;

		list($identifier, $input) = explode(self::SEPARATOR_ALGORITHM, $encrypted, 2);

		if ($identifier !== self::IDENTIFIER)
			throw new Exception('Encryption can not be decrypted. Unsupported identifier: ' . $identifier);

		// Split the input into its parts
		$cipherSplit = explode(self::SEPARATOR_CRYPTO_PARTS, $input);
		$originalSize = intval($cipherSplit[0]);
		$iv = cryptoHelpers::toNumbers($cipherSplit[1]);
		$cipherText = $cipherSplit[2];

		// Set up encryption parameters
		$cipherIn = cryptoHelpers::toNumbers($cipherText);
		$keyAsNumbers = cryptoHelpers::toNumbers(bin2hex($key));
		$keyLength = count($keyAsNumbers);

		$decrypted = AES::decrypt(
			$cipherIn,
			$originalSize,
			AES::modeOfOperation_CBC,
			$keyAsNumbers,
			$keyLength,
			$iv
		);

		// Byte-array to text.
		$hexDecrypted = cryptoHelpers::toHex($decrypted);
		$retVal = pack("H*", $hexDecrypted);

		return $retVal;
	}
1316600
public function findKYCById($appUserId, $id)
    {
        $url = sprintf('users/%s/kyc/%s', $appUserId, $id);
        return $this->getOne($url, $this->KYCEntityClassName);
    }
514184
protected function prpcryptDecrypt($encrypted, $encodingAesKey, $appId)
    {
        try {
            $key = base64_decode($encodingAesKey . '=');
            $iv = substr($key, 0, 16);
            $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($key, 0, 32), OPENSSL_ZERO_PADDING, $iv);
        } catch (\Exception $e) {
            return array('code' => -2002, 'message' => 'AES解密失败', 'e' => (string) $e);
        }

        try {
            // 去除补位字符
            $result = $this->pkcs7Decode($decrypted);
            // 去除16位随机字符串,网络字节序和AppId
            if (strlen($result) < 16) {
                return array('code' => -2003, 'message' => '解密后结果不能小于16位', 'result' => $result);
            }
            $content = substr($result, 16, strlen($result));
            $lenList = unpack('N', substr($content, 0, 4));
            $xmlLen = $lenList[1];
            $xml = substr($content, 4, $xmlLen);
            $fromAppId = substr($content, $xmlLen + 4);
        } catch (\Exception $e) {
            return array('code' => -2004, 'message' => '解密后得到的buffer非法', 'e' => (string) $e);
        }

        if ($fromAppId != $appId) {
            return array('code' => -2005, 'message' => 'AppId 校验错误', 'appId' => $appId, 'fromAppId' => $fromAppId);
        }

        return array('code' => 1, 'message' => '解密成功', 'xml' => $xml);
    }