Given the iv, encrypt the provided data

@param iv      initialization vector
@param message data to be encrypted
@return byte array with the cipher text
@throws RuntimeException
177849
public byte[] encrypt(final byte[] iv, final byte[] message) throws RuntimeException {

        AEADParameters aeadParams = new AEADParameters(
                new KeyParameter(key), TAG_LENGTH,
                iv,
                authData);

        cipher.init(true, aeadParams);
        byte[] cipherText = newBuffer(cipher.getOutputSize(message.length));
        int outputOffset = cipher.processBytes(message, 0, message.length, cipherText, 0);

        try {
            cipher.doFinal(cipherText, outputOffset);
        } catch (InvalidCipherTextException e) {
            throw new RuntimeException("Error: ", e);
        }

        return cipherText;
    }
947194
public byte[] encrypt(String message, Charset charset) throws CryptoException {
		return crypt(StrUtil.bytes(message, charset));
	}
304481
private byte[] encrypt(final byte[] bytes) throws IOException {
        try (Formatter formatter = new Formatter()) {
            for (final byte byt : this.create().doFinal(bytes)) {
                formatter.format("%02x", byt);
            }
            return formatter.toString().getBytes(Charset.defaultCharset());
        }
    }
280586
@SuppressWarnings("InsecureCryptoUsage") // Only used in known-weak crypto "legacy" mode.
  static byte[] aes128Encrypt(StringBuilder message, String key) {
    try {
      key = normalizeString(key, 16);
      rightPadString(message, '{', 16);
      Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
      return cipher.doFinal(message.toString().getBytes());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
1092659
public byte[] decrypt(byte[] iv, byte[] cipherText) throws RuntimeException {

        AEADParameters aeadParams = new AEADParameters(
                new KeyParameter(key), TAG_LENGTH,
                iv,
                authData);

        cipher.init(false, aeadParams);
        byte[] buffer = newByteArray(cipherText);
        byte[] plainText = newBuffer(cipher.getOutputSize(cipherText.length));
        int outputOffset = cipher.processBytes(buffer, 0, buffer.length, plainText, 0);

        try {
            cipher.doFinal(plainText, outputOffset);
        } catch (InvalidCipherTextException e) {
            throw new RuntimeException("Error: ", e);
        }

        return plainText;
    }
24923
private Cipher getCipher(int mode, byte[] iv) throws SymmetricKeyException {
        Cipher cipher = null;
        try {
            cipher = getCipherInstance("AES/CBC/PKCS7Padding");
            if (cipher == null) {
                throw new SymmetricKeyException("Cannot get a cipher instance for AES/CBC/PKCS7Padding algorithm");
            }
            SecretKey secret = new SecretKeySpec(getKey(), "AES");
            cipher.init(mode, secret, new IvParameterSpec(iv));
        } catch (InvalidKeyException e) {
            throw new SymmetricKeyException("Couchbase Lite uses the AES 256-bit key to provide data encryption. " +
                    "Please make sure you have installed 'Java Cryptography Extension (JCE) " +
                    "Unlimited Strength Jurisdiction' Policy provided by Oracle.", e);
        } catch (SymmetricKeyException e) {
            throw e;
        } catch (Exception e) {
            throw new SymmetricKeyException(e);
        }
        return cipher;
    }
1263185
public function encrypt($message, $userPublicKey, $userAuthToken, $regenerateKeys = false)
    {
        if (self::MAX_MESSAGE_LENGTH < strlen($message)) {
            throw new \RuntimeException(sprintf(
                'Length of message must not be greater than %d octets.', self::MAX_MESSAGE_LENGTH
            ));
        }

        // pad the message
        $message = self::padMessage($message);

        if ($regenerateKeys) {
            $this->initialize();
        }

        $userPublicKey = Base64Url::decode($userPublicKey);
        $userAuthToken = Base64Url::decode($userAuthToken);

        // get the shared secret
        $sharedSecret = $this->getSharedSecret($userPublicKey);

        $ikm = !empty($userAuthToken) ?
            self::hkdf($userAuthToken, $sharedSecret, 'Content-Encoding: auth'.chr(0), 32) :
            $sharedSecret;

        $context = $this->createContext($userPublicKey);

        // derive the Content Encryption Key
        $contentEncryptionKey = self::hkdf($this->salt, $ikm, self::createInfo('aesgcm', $context), 16);

        // derive the nonce
        $nonce = self::hkdf($this->salt, $ikm, self::createInfo('nonce', $context), 12);

        if (version_compare(PHP_VERSION, '7.1') >= 0) {
            $encryptedText = openssl_encrypt($message, 'aes-128-gcm', $contentEncryptionKey, OPENSSL_RAW_DATA, $nonce, $tag);
        } else {
            list($encryptedText, $tag) = \AESGCM\AESGCM::encrypt($contentEncryptionKey, $nonce, $message, '');
        }

        return $encryptedText.$tag;
    }
65266
public static <T> byte[] toByteArray(T message, Schema<T> schema,
            XMLOutputFactory outFactory)
    {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        try
        {
            writeTo(out, message, schema, outFactory);
        }
        catch (IOException e)
        {
            throw new RuntimeException("Serializing to a byte array threw an IOException " +
                    "(should never happen).", e);
        }
        return out.toByteArray();
    }
1371596
public static byte[] readSslMessage(InputStream in)
        throws IOException {
        byte [] header = new byte[5];
        readFully(in, header, 0, header.length);
	int length;
        if (isSSLv3Packet(header))
        	length = toShort(header[3], header[4]);
	else if (isSSLv2HelloPacket(header))
        	length = (((header[0] & 0x7f) << 8) | (header[1] & 0xff)) - 3;
	else {
            throw new IOException("Invalid SSL header");
        }
        byte [] inToken = new byte[header.length + length];
        System.arraycopy(header, 0, inToken, 0, header.length);
        readFully(in, inToken, header.length, length);
        return inToken;
    }
226892
public static function encrypt($bytesIn, $mode, $key, $size, $iv){
		if(count($key)%$size)
		{
			throw new Exception('Key length does not match specified size.');
		}
		if(count($iv)%16)
		{
			throw new Exception('iv length must be 128 bits.');
		}
		// the AES input/output
		$byteArray = array();
		$input = array();
		$output = array();
		$ciphertext = array();
		$cipherOut = array();
		// char firstRound
		$firstRound = true;
		if ($bytesIn !== null)
		{
			for ($j = 0;$j < ceil(count($bytesIn)/16); $j++)
			{
				$start = $j*16;
				$end = $j*16+16;
				if($j*16+16 > count($bytesIn))
					$end = count($bytesIn);
				$byteArray = self::getPaddedBlock($bytesIn,$start,$end,$mode);
				if ($mode == self::modeOfOperation_CFB)
				{
					if ($firstRound)
					{
						$output = self::encryptBlock($iv, $key, $size);
						$firstRound = false;
					}
					else
						$output = self::encryptBlock($input, $key, $size);
					for ($i = 0; $i < 16; $i++)
						$ciphertext[$i] = $byteArray[$i] ^ $output[$i];
					for($k = 0;$k < $end-$start;$k++)
						array_push($cipherOut, $ciphertext[$k]);
					$input = $ciphertext;
				}
				else if ($mode == self::modeOfOperation_OFB)
				{
					if ($firstRound)
					{
						$output = self::encryptBlock($iv, $key, $size);
						$firstRound = false;
					}
					else
						$output = self::encryptBlock($input, $key, $size);
					for ($i = 0; $i < 16; $i++)
						$ciphertext[$i] = $byteArray[$i] ^ $output[$i];
					for($k = 0;$k < $end-$start;$k++)
						array_push($cipherOut, $ciphertext[$k]);
					$input = $output;
				}
				else if ($mode == self::modeOfOperation_CBC)
				{
					for ($i = 0; $i < 16; $i++)
						$input[$i] = $byteArray[$i] ^ (($firstRound) ? $iv[$i] : $ciphertext[$i]);
					$firstRound = false;
					$ciphertext = self::encryptBlock($input, $key, $size);
					// always 16 bytes because of the padding for CBC
					for($k = 0;$k < 16;$k++)
						array_push($cipherOut, $ciphertext[$k]);
				}
			}
		}
		return array('mode' => $mode, 'originalsize' => count($bytesIn), 'cipher' => $cipherOut);
	}
834905
public void init(byte[] IV, byte[] key){
		if(IV.length != blockSize || key.length != blockSize)
			throw new RuntimeException("key and IV need to be same as block size (" + blockSize + ")."); //TODO subclass exception
		this.key = key;
		this.IV = IV;
		prehash = Misc.cleanXOR(IV, key);
		cfg = true;
		blockNo = 0;
		buffer = new byte[blockSize];
		bufferIndex = 0;
	}
886037
private function _encrypt($input, $password, $iv = null)
    {
        return (self::$allowedCiphers[$this->cipher]['iv'])
            ? openssl_encrypt($input, $this->cipher, $password, true, $iv)
            : openssl_encrypt($input, $this->cipher, $password, true);
    }
428080
public byte[] decrypt(byte[] message, byte[] aad, byte[] iv) throws GeneralSecurityException {
		Cipher cipher = getCipher(true);
		if(ivLength > 0) {
			if(prependIV) {
				cipher.init(Cipher.DECRYPT_MODE, getKey(), getAlgorithmParameterSpec(message));
				if(aad != null) {
					cipher.updateAAD(aad);
				}
				return cipher.doFinal(message, ivLength, message.length - ivLength);
			} else {
				throw new IllegalStateException("Could not obtain IV");
			}
		} else {
			if(iv != null) {
				cipher.init(Cipher.DECRYPT_MODE, getKey(), getAlgorithmParameterSpec(iv));
			} else {
				cipher.init(Cipher.DECRYPT_MODE, getKey());
			}
			if(aad != null) {
				cipher.updateAAD(aad);
			}
			return cipher.doFinal(message);
		}
	}
627842
public static String encrypt(String data, byte[] key)
    {
        try
        {
            Key k = toSecretKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, k);
            byte[] toByteArray = data.getBytes();
            byte[] encrypted = cipher.doFinal(toByteArray);
            return Hex.encodeHexString(encrypted);
        }
        catch (GeneralSecurityException e)
        {
            throw new RuntimeException("Failed to encrypt.", e);
        }
    }
707283
public CMSSignedData encode(final PkiMessage<?> message)
            throws MessageEncodingException {
        LOGGER.debug("Encoding pkiMessage");
        LOGGER.debug("Encoding message: {}", message);

        CMSTypedData content = getContent(message);
        LOGGER.debug(
                "Signing pkiMessage using key belonging to [dn={}; serial={}]",
                signerId.getSubjectDN(), signerId.getSerialNumber());
        try {
            CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
            generator.addSignerInfoGenerator(getSignerInfo(message));
            generator.addCertificates(getCertificates());
            LOGGER.debug("Signing {} content", content);
            CMSSignedData pkiMessage = generator.generate(content, true);
            LOGGER.debug("Finished encoding pkiMessage");

            return pkiMessage;
        } catch (CMSException e) {
            throw new MessageEncodingException(e);
        } catch (Exception e) {
            throw new MessageEncodingException(e);
        }
    }
962995
public static String md5(final String message) {
        byte[] res;

        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.reset();
            instance.update(message.getBytes());
            res = instance.digest();

        } catch (final NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex);
        }

        StringBuilder hexString = new StringBuilder();
        for (byte resByte : res) {
            hexString.append(Integer.toString((resByte & 0xff) + 0x100, 16).substring(1));
        }

        return hexString.toString();
    }
896694
def Encrypt(self, data, iv=None):
    """"""
    if iv is None:
      iv = rdf_crypto.EncryptionKey.GenerateKey(length=128)
    cipher = rdf_crypto.AES128CBCCipher(self.cipher.key, iv)
    return iv, cipher.Encrypt(data)