Encrypts a string and returns the base64_encoded response from KMS.

@param $string
@param $key_name
@return mixed
199486
static public function encryptString($plaintext_string, $key_name) {
        $kms = self::getService();
        $base64_encoded_json = base64_encode($plaintext_string);
        $request = new \Google_Service_CloudKMS_EncryptRequest();
        $request->setPlaintext($base64_encoded_json);
        $response = $kms->projects_locations_keyRings_cryptoKeys->encrypt(
            $key_name,
            $request
        );
        return $response['ciphertext'];
    }
210697
public static String decrypt(String keyBase64, String encrypted) {
        return decrypt(Base64.decodeBase64(keyBase64.getBytes(StandardCharsets.UTF_8)), encrypted);
    }
77279
public function encrypt($string, $key)
    {
        try {
            return $this->driverInstance->encrypt($string, $key);
        } catch (\Exception $e) {
            throw new CryptException($e->getMessage());
        }
    }
1338514
static public function decrypt($ciphertextFileName, Config $config = null) {
        // Instantiate the client, authenticate, and add scopes.
        $kms = self::getService();
        $name = self::getDefaultKeyName($config);
        // Use the KMS API to decrypt the text.
        $ciphertext = base64_encode(file_get_contents($ciphertextFileName));
        $request = new \Google_Service_CloudKMS_DecryptRequest();
        $request->setCiphertext($ciphertext);
        $response = $kms->projects_locations_keyRings_cryptoKeys->decrypt(
            $name,
            $request
        );
        return base64_decode($response['plaintext']);
    }
514458
public static function safeBase64Decode($string)
    {
        $data = str_replace(['-', '_'], ['+', '/'], $string);
        $mod4 = strlen($data) % 4;
        if ($mod4) {
            $data .= substr('====', $mod4);
        }
        return base64_decode($data);
    }
1014559
public String encryptBase64(String data, String charset) {
		return Base64.encode(encrypt(data, charset));
	}
915636
protected static function pkcs7_pad($string)
    {

        // 128 bits: $block_length = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $block_length = 16;
        $pad = $block_length - (self::strlen($string) % $block_length);

        return $string.str_repeat(chr($pad), $pad);
    }
1081332
private static SecretKey cekByKMS(byte[] cekSecured, String keyWrapAlgo,
                                      EncryptionMaterials materials,
                                      ContentCryptoScheme contentCryptoScheme, AWSKMS kms) {
        DecryptRequest kmsreq = new DecryptRequest()
            .withEncryptionContext(materials.getMaterialsDescription())
            .withCiphertextBlob(ByteBuffer.wrap(cekSecured));
        DecryptResult result = kms.decrypt(kmsreq);
        return new SecretKeySpec(copyAllBytesFrom(result.getPlaintext()),
                                 contentCryptoScheme.getKeyGeneratorAlgorithm());
    }
989093
protected static function crypt( $string, $key )
	{
		for( $i=0; $i<strlen( $string ); $i++ )
		{
			$char = ord( $string[$i] );
			if( $char > 64 && $char < 91 )
			{
				$char += $key;
				if( $char > 90 )
					$char -= 26;
				else if( $char < 65 )
					$char += 26;
			}
			else if( $char > 96 && $char < 123 )
			{
				$char += $key;
				if ($char > 122)
					$char -= 26;
				else if( $char < 97 )
					$char += 26;
			}
			$string[$i] = chr( $char );
		}
		return $string;
	}
874624
public function getKey() {
		if(!$this->decrypted_key) {

			$decoded_ciphertextblob = base64_decode($this->key_description->getCiphertextBlob(), true);
			if(!$decoded_ciphertextblob) {
				throw new FetchException('CiphertextBlob should be a valid base64 encoded string');
			}

			$options = [
				'CiphertextBlob' => $decoded_ciphertextblob
			];

			$context = $this->key_description->getEncryptionContextAsArray();
			if(!empty($context)) {
				$options['EncryptionContext'] = $context;
			}

			try {
				$response = $this->client->decrypt($options);
			}
			catch(KmsException $e) {
				throw new FetchException('Key decryption failed', 0, $e);
			}

			$this->decrypted_key = $response['Plaintext'];
		}

		return $this->decrypted_key;
	}
448054
private function encrypt($string = "")
    {

        $key = $this->config->get('gateway.asanpardakht.key');
        $iv = $this->config->get('gateway.asanpardakht.iv');

        try {

            $soap = new SoapClient("https://services.asanpardakht.net/paygate/internalutils.asmx?WSDL");
            $params = array(
                'aesKey' => $key,
                'aesVector' => $iv,
                'toBeEncrypted' => $string
            );

            $response = $soap->EncryptInAES($params);
            return $response->EncryptInAESResult;

        } catch (\SoapFault $e) {
            return "";
        }
    }
185357
public function encrypt($original_string, $cipher_key)
    {
        $cipher_key = $this->_genKey($cipher_key);

        $ivSize = openssl_cipher_iv_length('AES-256-CBC');
        $iv = openssl_random_pseudo_bytes($ivSize);

        $cipher_text = $iv . openssl_encrypt($original_string, 'AES-256-CBC', $cipher_key, OPENSSL_RAW_DATA, $iv);

        return $this->_genHmac($cipher_text, $cipher_key) . $cipher_text;
    }
883382
private static function __getHash($string)
	{
		return base64_encode(extension_loaded('hash') ?
		hash_hmac('sha1', $string, self::$__secretKey, true) : pack('H*', sha1(
		(str_pad(self::$__secretKey, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) .
		pack('H*', sha1((str_pad(self::$__secretKey, 64, chr(0x00)) ^
		(str_repeat(chr(0x36), 64))) . $string)))));
	}
849730
static function lock($string, $key = null) {
        return openssl_encrypt($string, self::_getSecMethod(), Utils::spare($key, self::_geteSecKey()), false, self::_getSecIv());
    }
261383
public static function tripledes($string, $key, $operation = self::ENCRYPT) {
        if ($operation === static::ENCRYPT) {
            return static::encrypt($string, $key, static::TRIPLEDES);
        } else {
            return static::decrypt($string, $key, static::TRIPLEDES);
        }
    }
352312
public function encrypt($value, string $key, bool $base64 = false): string
    {
        $ret = $this->encryptBinary(serialize($value), $key);
        return ($base64) ? base64_encode($ret) : $ret;
    }
210303
protected static function mcryptDecrypt($key, $string, $options)
	{
		$debug = \bdk\Debug::getInstance();
		if (Str::isBase64Encoded($string)) {
			$debug->log('base64 decoding crypt text');
			$string = base64_decode($string);
		}
		$iv		= substr($string, 0, $options['iv_size']);
		$string	= substr($string, $options['iv_size']);
		if ($string && strlen($iv) == $options['iv_size']) {
			$string	= mcrypt_decrypt($options['cipher'], $key, $string, $options['mode'], $iv);
		}
		// now remove any padding
		$last_chr = ord(substr($string, -1));
		#$debug->log('last_chr', $last_chr);
		$method_used = 0;
		if ($last_chr > 0 && $last_chr <= $options['block_size']) {
			$remove = substr($string, -$last_chr);
			if ($remove == str_repeat(chr($last_chr), $last_chr)) {
				$method_used = 1;
			} elseif ($remove == str_repeat(chr(0), $last_chr-1).chr($last_chr)) {
				$method_used = 3;
			}
			if ($method_used) {
				$string = substr($string, 0, -$last_chr);
			}
		} elseif ($last_chr == 0 && preg_match('/\x00{1,'.$options['block_size'].'}$/', $string, $matches)) {
			$remove = $matches[0];
			$length = strlen($remove);
			$string = substr($string, 0, -$length);
			$method_used = 4;
			if ($length < $options['block_size'] && ord(substr($string, -1)) == 0x80) {
				$method_used = 2;
				$remove = chr(0x80).$remove;
				$string = substr($string, 0, -1);
			}
		}
		if ($method_used) {
			$debug->log('padding removed ('.trim(chunk_split(bin2hex($remove), 2, ' ')).'), method', $method_used);
		}
		return $string;
	}