Creates a Cipher from the passed in key, using the passed in mode.
@param key  SecretKeySpec
@param mode Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE
@return Cipher instance created with the passed in key and mode.
503429
public static Cipher createAesCipher(Key key, int mode) throws Exception
    {
        // Use password key as seed for IV (must be 16 bytes)
        MessageDigest d = getMD5Digest();
        d.update(key.getEncoded());
        byte[] iv = d.digest();

        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        // CBC faster than CFB8/NoPadding (but file length changes)
        cipher.init(mode, key, paramSpec);
        return cipher;
    }
335779
public HMac init(String algorithm, byte[] key){
		return init(algorithm, (null == key) ? null : new SecretKeySpec(key, algorithm));
	}
35545
private String cipher(String input, int mode)
    {
        if (input == null)
        {
            return null;
        }

        // Convert the input string into a byte array.
        byte[] inputBytes = Utils.getBytesUTF8(input);

        // 
        byte[] outputBytes = cipher(inputBytes, mode);

        // Build a string from the byte array.
        return Utils.toStringUTF8(outputBytes);
    }
1192702
public static OutputStream encrypt(OutputStream outputStream, byte[] key) {
        checkNotNull(outputStream);
        checkNotNull(key);
        checkArgument(key.length >= 5 && key.length <= 256);
        StreamCipher rc4 = new RC4Engine();
        rc4.init(true, new KeyParameter(key));
        return new CipherOutputStream(outputStream, rc4);
    }
659431
@Override
    public String[] get(String key) {
        String[] cipher = super.get(key);
        
        if (cipher == null) {
            throw new IllegalArgumentException("Cipher is unknown.");
        }
        
        return cipher;
    }
797154
public static byte[] encypt(String str, byte[] key) {
        byte[] b = null;
        try {
            SecretKey secretKey = new SecretKeySpec(key, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            b = cipher.doFinal(str.getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return b;
    }
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;
    }
634958
public static String decrypt(Key key, String content) throws Exception {
        try {
            Cipher cipher = Cipher.getInstance(CRYPTO_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(Base64.decodeBase64(content)));
        } catch (Exception e) {
            log.error("Error while decrypting", e);
            throw e;
            
        }
    }
811669
public static OutputStream encrypt(OutputStream outputStream, byte[] key) {
        checkNotNull(outputStream);
        checkNotNull(key);
        AESFastEngine aesEngine = new AESFastEngine();
        PaddedBufferedBlockCipher cipher =
                new PaddedBufferedBlockCipher(aesEngine, new PKCS7Padding());
        cipher.init(true, new KeyParameter(key));
        return new CipherOutputStream(outputStream, cipher);
    }
30928
public byte[] decrypt(byte[] msg) {
        if (msg == null) return null;
        try {
            Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM_IMPLEMENTATION);
            SecretKeySpec key = new SecretKeySpec(getMd5(), ENCRYPTION_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(getIv());
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            return cipher.doFinal(msg);
        } catch (Exception e) {
            return null;
        }
    }
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);
        }
    }
299248
@Override
    public Cipher encrypt() {
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, getKey());
            return cipher;
        } catch (GeneralSecurityException e) {
            throw new AssertionError(e);
        }
    }
294960
public static InputStream decrypt(InputStream inputStream, byte[] key) {
        checkNotNull(inputStream);
        checkNotNull(key);
        AESFastEngine aesEngine = new AESFastEngine();
        PaddedBufferedBlockCipher cipher =
                new PaddedBufferedBlockCipher(aesEngine, new PKCS7Padding());
        cipher.init(false, new KeyParameter(key));
        return new CipherInputStream(inputStream, cipher);
    }
934339
public static CipherUtil buildInstance(String privateKey, String publicKey) {
        return buildInstance(privateKey.getBytes(), publicKey.getBytes());
    }
174631
def _cryptography_cipher(key, iv):
    
    return Cipher(
        algorithm=algorithms.AES(key),
        mode=modes.CFB(iv),
        backend=default_backend()
    )
1331093
public static KeySpec newPBEKeySpec(final String privateKey)
	{
		if (privateKey == null)
		{
			return new PBEKeySpec(CompoundAlgorithm.PRIVATE_KEY.toCharArray());
		}
		return new PBEKeySpec(privateKey.toCharArray());
	}
1004941
public static byte[] aesEncrypt(byte[] input, byte[] key, byte[] iv) {
		return aes(input, key, iv, Cipher.ENCRYPT_MODE);
	}