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);

Builder Pattern Alternative

For complex symmetric cipher configurations, use the fluent builder API:

Basic Symmetric Cipher Builder

Advanced Symmetric Cipher Builder

��Cipher By Key

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

Usage Example

�Encoding Cipher

�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

Encoding Cipher By Key

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

Usage Example

Last updated

Was this helpful?