Symmetric Ciphers

Cipher

Cipher cipher(
byte[] key,
String keyAlgorithm,
String cipherAlgorithm,
Mode mode
);

Returns a symmetric cipher for the given key, key algorithm, cipher algorithm and mode.

Usage Example

Random rng = new SecureRandom();
byte[] iv = new byte[8];
rng.nextBytes(iv);
byte[] key = symmetricKey("DESede");
Cipher encrypter = Bruce.cipher(key, "DESede", "DESede/CBC/PKCS5Padding", ENCRYPT);
Cipher decrypter = Bruce.cipher(key, "DESede", "DESede/CBC/PKCS5Padding", DECRYPT);
byte[] clearText = "Hi there".getBytes(UTF_8);
byte[] cypherText = encrypter.encrypt(iv, clearText);
byte[] decryptedText = decrypter.encrypt(iv, cypherText);
assertArrayEquals(clearText, decryptedText);

Cipher By Key

CipherByKey cipher(
String keyAlgorithm,
String cipherAlgorithm,
Mode mode
);

Returns a cipher where the key can be passed at runtime through the returned interface.

Usage Example

Random rng = new SecureRandom();
byte[] iv = new byte[8]; // initialization vector
rng.nextBytes(iv);
byte[] key = symmetricKey("DESede");
CipherByKey encrypter = cipher("DESede", "DESede/CBC/PKCS5Padding", ENCRYPT);
CipherByKey decrypter = cipher("DESede", "DESede/CBC/PKCS5Padding", DECRYPT);
byte[] plainText = "Hi there".getBytes(UTF_8);
byte[] cypherText = encrypter.encrypt(key, iv, plainText);
byte[] decryptedText = decrypter.encrypt(key, iv, cypherText);
assertArrayEquals(plainText, decryptedText);

Encoding Cipher

EncodingCipher cipher(
String key,
String keyAlgorithm,
String cipherAlgorithm,
Mode mode,
Charset charset,
Encoding encoding
);

Returns an encoding cipher. The key must also be encoded with the specified encoding. The character set refers to the plain text message string encoding.

Usage Example

Random rng = new SecureRandom();
byte[] ivBA = new byte[8]; // initialization vector byte array
rng.nextBytes(ivBA);
String iv = Base64.getEncoder().encodeToString(ivBA);
String key = symmetricKey("DESede", BASE64);
EncodingCipher encrypter = cipher(key, "DESede", "DESede/CBC/PKCS5Padding", ENCRYPT, UTF_8, BASE64);
EncodingCipher decrypter = cipher(key, "DESede", "DESede/CBC/PKCS5Padding", DECRYPT, UTF_8, BASE64);
String plainText = "Hi there";
String cypherText = encrypter.encrypt(iv, plainText);
String decryptedText = decrypter.encrypt(iv, cypherText);
assertEquals(plainText, decryptedText);

Encoding Cipher By Key

EncodingCipherByKey cipherByKey(
String keyAlgorithm,
String cipherAlgorithm,
Mode mode,
Charset charset
);

Same as encoding cipher but allows you to provide the keys at runtime.

Usage Example

Random rng = new SecureRandom();
byte[] ivBA = new byte[8]; // initialization vector byte array
rng.nextBytes(ivBA);
String iv = Base64.getEncoder().encodeToString(ivBA);
String key = symmetricKey("DESede", BASE64);
EncodingCipherByKey encrypter = cipherByKey("DESede", "DESede/CBC/PKCS5Padding", ENCRYPT, UTF_8);
EncodingCipherByKey decrypter = cipherByKey("DESede", "DESede/CBC/PKCS5Padding", DECRYPT, UTF_8);
String clearText = "Hi there";
String cypherText = encrypter.encrypt(key, iv, clearText, BASE64);
String decryptedText = decrypter.encrypt(key, iv, cypherText, BASE64);
assertEquals(clearText, decryptedText);