Bruce
Search…
Symmetric Ciphers

Cipher

1
Cipher cipher(
2
byte[] key,
3
String keyAlgorithm,
4
String cipherAlgorithm,
5
Mode mode
6
);
Copied!
Returns a symmetric cipher for the given key, key algorithm, cipher algorithm and mode.

Usage Example

1
Random rng = new SecureRandom();
2
byte[] iv = new byte[8];
3
rng.nextBytes(iv);
4
byte[] key = symmetricKey("DESede");
5
Cipher encrypter = Bruce.cipher(key, "DESede", "DESede/CBC/PKCS5Padding", ENCRYPT);
6
Cipher decrypter = Bruce.cipher(key, "DESede", "DESede/CBC/PKCS5Padding", DECRYPT);
7
byte[] clearText = "Hi there".getBytes(UTF_8);
8
byte[] cypherText = encrypter.encrypt(iv, clearText);
9
byte[] decryptedText = decrypter.encrypt(iv, cypherText);
10
assertArrayEquals(clearText, decryptedText);
Copied!

Cipher By Key

1
CipherByKey cipher(
2
String keyAlgorithm,
3
String cipherAlgorithm,
4
Mode mode
5
);
Copied!
Returns a cipher where the key can be passed at runtime through the returned interface.

Usage Example

1
Random rng = new SecureRandom();
2
byte[] iv = new byte[8]; // initialization vector
3
rng.nextBytes(iv);
4
byte[] key = symmetricKey("DESede");
5
CipherByKey encrypter = cipher("DESede", "DESede/CBC/PKCS5Padding", ENCRYPT);
6
CipherByKey decrypter = cipher("DESede", "DESede/CBC/PKCS5Padding", DECRYPT);
7
byte[] plainText = "Hi there".getBytes(UTF_8);
8
byte[] cypherText = encrypter.encrypt(key, iv, plainText);
9
byte[] decryptedText = decrypter.encrypt(key, iv, cypherText);
10
assertArrayEquals(plainText, decryptedText);
Copied!

Encoding Cipher

1
EncodingCipher cipher(
2
String key,
3
String keyAlgorithm,
4
String cipherAlgorithm,
5
Mode mode,
6
Charset charset,
7
Encoding encoding
8
);
Copied!
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

1
Random rng = new SecureRandom();
2
byte[] ivBA = new byte[8]; // initialization vector byte array
3
rng.nextBytes(ivBA);
4
String iv = Base64.getEncoder().encodeToString(ivBA);
5
String key = symmetricKey("DESede", BASE64);
6
EncodingCipher encrypter = cipher(key, "DESede", "DESede/CBC/PKCS5Padding", ENCRYPT, UTF_8, BASE64);
7
EncodingCipher decrypter = cipher(key, "DESede", "DESede/CBC/PKCS5Padding", DECRYPT, UTF_8, BASE64);
8
String plainText = "Hi there";
9
String cypherText = encrypter.encrypt(iv, plainText);
10
String decryptedText = decrypter.encrypt(iv, cypherText);
11
assertEquals(plainText, decryptedText);
Copied!

Encoding Cipher By Key

1
EncodingCipherByKey cipherByKey(
2
String keyAlgorithm,
3
String cipherAlgorithm,
4
Mode mode,
5
Charset charset
6
);
Copied!
Same as encoding cipher but allows you to provide the keys at runtime.

Usage Example

1
Random rng = new SecureRandom();
2
byte[] ivBA = new byte[8]; // initialization vector byte array
3
rng.nextBytes(ivBA);
4
String iv = Base64.getEncoder().encodeToString(ivBA);
5
String key = symmetricKey("DESede", BASE64);
6
EncodingCipherByKey encrypter = cipherByKey("DESede", "DESede/CBC/PKCS5Padding", ENCRYPT, UTF_8);
7
EncodingCipherByKey decrypter = cipherByKey("DESede", "DESede/CBC/PKCS5Padding", DECRYPT, UTF_8);
8
String clearText = "Hi there";
9
String cypherText = encrypter.encrypt(key, iv, clearText, BASE64);
10
String decryptedText = decrypter.encrypt(key, iv, cypherText, BASE64);
11
assertEquals(clearText, decryptedText);
Copied!
Last modified 4mo ago