Serialize and encrypt a value

@param mixed $value
@param string $key
@param boolean $base64 Whether to base64 encode the result
@return string|null
352312
public function encrypt($value, string $key, bool $base64 = false): string
    {
        $ret = $this->encryptBinary(serialize($value), $key);
        return ($base64) ? base64_encode($ret) : $ret;
    }
253261
public static String base64Encode(final String value) {
        validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
        return Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8));
    }
1029478
public function decode($value)
    {

        $prepared = unserialize(base64_decode($value));
        $value = base64_decode($prepared['value']);
        $key   = base64_decode($prepared['key']);
        $iv    = base64_decode($prepared['iv']);


        if (false !== $decrypted = openssl_decrypt($value, $this->mode, $key, false, $iv)) {
            return unserialize($decrypted);
        }

        return false;
    }
1229507
private function convertBase64ToDec($value)
    {
        $value = unpack('H*', Base64Url::decode($value));

        return $this->convertHexToDec($value[1]);
    }
1174969
public function encode($value)
    {
        $encryptedValue = $this->getEncryptor()->encrypt($value);
        $encodedValue = base64_encode($encryptedValue);

        return $encodedValue;
    }
302624
protected function serialize($value)
    {
        $result = serialize($value);

        if ($this->connection instanceof PostgresConnection && Str::contains($result, "\0")) {
            $result = base64_encode($result);
        }

        return $result;
    }
1328013
public function encrypt($value)
    {
        $iv = function_exists('random_bytes') ? random_bytes(16) : openssl_random_pseudo_bytes(16);
        $value = openssl_encrypt(serialize($value), $this->_cipher, $this->_key, 0, $iv);

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

        $mac = $this->_hash($iv = base64_encode($iv), $value);
        $json = json_encode(compact('iv', 'value', 'mac'));

        if (! is_string($json)) {
            throw new AnEncrypterException('Could not encrypt the data.');
        }

        return base64_encode($json);
    }
1176959
public function decryptBinary(string $value, string $key): string
    {
        $iv = substr($value, 0, $this->ivLength);
        $encrypted = substr($value, $this->ivLength);
        $key = $this->hashKey($key);
        $opts = OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING;
        $ret = openssl_decrypt($encrypted, $this->cipher, $key, $opts, $iv);
        if ($ret === false) {
            throw new \Exception("decryption failed; ".openssl_error_string());
        }
        return $ret;
    }
757045
public function encrypt($value)
	{
		$iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());

		$value = base64_encode($this->padAndMcrypt($value, $iv));

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

		return base64_encode(json_encode(compact('iv', 'value', 'mac')));
	}
1208289
private function _encodeConfiguration($value)
    {
        $value = is_resource($value) ? stream_get_contents($value) : $value;
        $value = base64_encode($value);

        // Cut the BOM if any. If the xml configuration would start with BOM Azure treats it as invalid XML file.
        if (strpos($value, '77u/') === 0) {
            $value = substr($value, 4);
        }

        return $value;
    }
184723
private function serialize($value)
    {
        $compressed = gzcompress($value);
        if ($compressed === false) {
            return false;
        }

        $encoded = base64_encode($compressed);
        return $encoded;
    }
564441
public function encrypt($value, $raw_binary = false)
    {
        return Crypto::encryptWithPassword($value, $this->secret, $raw_binary);
    }
936645
public static function decode($value, $key = false)
	{
		$key ? static::$crypter->setKey($key) : static::$crypter->setKey(static::safe_b64decode(static::$config['crypto_key']));
		static::$crypter->setIV(static::safe_b64decode(static::$config['crypto_iv']));

		$value = static::safe_b64decode($value);
		if ($value = static::validate_hmac($value))
		{
			return static::$crypter->decrypt($value);
		}
		else
		{
			return false;
		}
	}
594674
public function encode($value)
    {

        if (false !== $encrypted = openssl_encrypt(serialize($value), $this->mode, $key = $this->createEncryptionKey(), 0, $iv = $this->createRandomIv())) {

            return base64_encode(
                serialize(
                    [
                        'value' => base64_encode($encrypted),
                        'iv'    => base64_encode($iv),
                        'key'   => base64_encode($key),
                    ]
                )
            );
        }

        return false;
    }
883196
public function set($key, $value)
    {
        list($baseKey, $searchKey) = $this->fetchKey($key);

        $registry = $this->get($baseKey);

        if ($registry === null) {
            $registry = $this->cache->get($baseKey);
        }

        if (! is_null($registry)) {
            return $this->overwrite($key, $value);
        }

        if ($baseKey != $searchKey)
        {
            $object = array();
            $level  = '';
            $keys   = explode('.', $searchKey);

            foreach ($keys as $key)
            {
                $level .= '.'.$key;
                (trim($level, '.') == $searchKey) ? array_set($object, trim($level, '.'), $value) : array_set($object, trim($level, '.'), array());
            }

            $this->database->table($this->config['table'])->insert(array('key' => $baseKey, 'value' => json_encode($object)));

            // Add to cache
            $this->cache->add($baseKey, $object);
        }
        else
        {
            $this->database->table($this->config['table'])->insert(array('key' => $baseKey, 'value' => json_encode($value)));

            // Add to cache
            $this->cache->add($baseKey, $value);
        }

        return true;
    }
364639
public static function decodeFromBase64(string $value, string $type = null)
    {
        return self::run(self::DECODER, \base64_decode($value), $type);
    }
50895
protected function encode($value)
    {
        $key = (string) $value;
        if (!isset(static::$cacheEncode[$key])) {
            $isAngularVar = false;
            if (\mb_substr($value, 0, 2) == '{{' && \mb_substr($value, -2) == '}}') {
                $isAngularVar = true;
                $value = \trim(\mb_substr($value, 2, -2));
            }
            static::$cacheEncode[$key] = \rawurlencode($value);
            static::$cacheEncode[$key] = \strtr(static::$cacheEncode[$key], static::$urlencodeCorrectionMap);
            if ($isAngularVar === true) {
                static::$cacheEncode[$key] = '{{ ' . static::$cacheEncode[$key] . ' }}';
            }
        }

        return static::$cacheEncode[$key];
    }