Bruce
Search…
Asymmetric Ciphers

Cipher

1
Cipher cipher(Key key, String algorithm, Mode mode);
Copied!
Returns an asymmetric cipher.

Usage Example

1
KeyStore aliceKeystore = keystore("classpath:/keystore-alice.p12", "password".toCharArray(), "PKCS12");
2
KeyStore bobKeystore = keystore("classpath:/keystore-bob.p12", "password".toCharArray(), "PKCS12");
3
Key alicePrivateKey = privateKey(aliceKeystore, "alice", "password".toCharArray());
4
Key bobPrivateKey = privateKey(bobKeystore, "bob", "password".toCharArray());
5
Key alicePublicKey = publicKey(aliceKeystore, "alice");
6
Key bobPublicKey = publicKey(bobKeystore, "bob");
7
8
Cipher encryptForAlice = cipher(alicePublicKey, "RSA", ENCRYPT);
9
Cipher decryptForAlice = cipher(alicePrivateKey, "RSA", DECRYPT);
10
Cipher encryptForBob = cipher(bobPublicKey, "RSA", ENCRYPT);
11
Cipher decryptForBob = cipher(bobPrivateKey, "RSA", DECRYPT);
12
13
// Alice writes to Bob
14
byte[] aliceMsg01 = "Hello".getBytes(UTF_8);
15
byte[] aliceMsg01Encrypted = encryptForBob.encrypt(aliceMsg01);
16
17
// Bob decrypts Alice's message
18
byte[] aliceMsg01Decrypted = decryptForBob.encrypt(aliceMsg01Encrypted);
19
20
// Bob responds to Alice's message
21
byte[] bobMsg01 = "Hey Alice, nice to hear from you.".getBytes(UTF_8);
22
byte[] bobMsg01Encrypted = encryptForAlice.encrypt(bobMsg01);
23
24
// Alice decrypts Bob's message
25
byte[] bobMsg01Decrypted = decryptForAlice.encrypt(bobMsg01Encrypted);
Copied!

Cipher By Key

1
CipherByKey cipher(Map<String, Key> keys, String algorithm);
Copied!
Returns a cipher interface for working with a map of preconfigured keys.

Usage Example

1
KeyStore aliceKeystore = keystore("classpath:/keystore-alice.p12", "password".toCharArray(), "PKCS12");
2
KeyStore bobKeystore = keystore("classpath:/keystore-bob.p12", "password".toCharArray(), "PKCS12");
3
Key alicePrivateKey = privateKey(aliceKeystore, "alice", "password".toCharArray());
4
Key bobPrivateKey = privateKey(bobKeystore, "bob", "password".toCharArray());
5
Key alicePublicKey = publicKey(aliceKeystore, "alice");
6
Key bobPublicKey = publicKey(bobKeystore, "bob");
7
8
Map<String, Key> keys = Map.of(
9
"alice-public", alicePublicKey,
10
"alice-private", alicePrivateKey,
11
"bob-public", bobPublicKey,
12
"bob-private", bobPrivateKey
13
);
14
15
CipherByKey cipher = cipher(keys, "RSA");
16
17
// Alice writes to Bob
18
byte[] aliceMsg01 = "Hello".getBytes(UTF_8);
19
byte[] aliceMsg01Encrypted = cipher.encrypt("bob-public", ENCRYPT, aliceMsg01);
20
21
// Bob decrypts Alice's message
22
byte[] aliceMsg01Decrypted = cipher.encrypt("bob-private", DECRYPT, aliceMsg01Encrypted);
23
assertArrayEquals(aliceMsg01, aliceMsg01Decrypted);
24
25
// Bob responds to Alice's message
26
byte[] bobMsg01 = "Hey Alice, nice to hear from you.".getBytes(UTF_8);
27
byte[] bobMsg01Encrypted = cipher.encrypt("alice-public", ENCRYPT, bobMsg01);
28
29
// Alice decrypts Bob's message
30
byte[] bobMsg01Decrypted = cipher.encrypt("alice-private", DECRYPT, bobMsg01Encrypted);
31
assertArrayEquals(bobMsg01, bobMsg01Decrypted);
Copied!

Encoding Cipher

1
EncodingCipher cipher(
2
Key key,
3
String algorithm,
4
Mode mode,
5
Encoding encoding,
6
Charset charset
7
);
Copied!
Returns an encoding cipher. The character set refers to the plain text message string encoding.

Usage Example

1
KeyStore aliceKeystore = keystore("classpath:/keystore-alice.p12", "password".toCharArray(), "PKCS12");
2
KeyStore bobKeystore = keystore("classpath:/keystore-bob.p12", "password".toCharArray(), "PKCS12");
3
Key alicePrivateKey = privateKey(aliceKeystore, "alice", "password".toCharArray());
4
Key bobPrivateKey = privateKey(bobKeystore, "bob", "password".toCharArray());
5
Key alicePublicKey = publicKey(aliceKeystore, "alice");
6
Key bobPublicKey = publicKey(bobKeystore, "bob");
7
8
EncodingCipher encryptForAlice = cipher(alicePublicKey, "RSA", ENCRYPT, BASE64, UTF_8);
9
EncodingCipher decryptForAlice = cipher(alicePrivateKey, "RSA", DECRYPT, BASE64, UTF_8);
10
EncodingCipher encryptForBob = cipher(bobPublicKey, "RSA", ENCRYPT, BASE64, UTF_8);
11
EncodingCipher decryptForBob = cipher(bobPrivateKey, "RSA", DECRYPT, BASE64, UTF_8);
12
13
// Alice writes to Bob
14
String aliceMsg01 = "Hello";
15
String aliceMsg01Encrypted = encryptForBob.encrypt(aliceMsg01);
16
17
// Bob decrypts Alice's message
18
String aliceMsg01Decrypted = decryptForBob.encrypt(aliceMsg01Encrypted);
19
assertEquals(aliceMsg01, aliceMsg01Decrypted);
20
21
// Bob responds to Alice's message
22
String bobMsg01 = "Hey Alice, nice to hear from you.";
23
String bobMsg01Encrypted = encryptForAlice.encrypt(bobMsg01);
24
25
// Alice decrypts Bob's message
26
String bobMsg01Decrypted = decryptForAlice.encrypt(bobMsg01Encrypted);
27
assertEquals(bobMsg01, bobMsg01Decrypted);
Copied!

Encoding Cipher By Key

1
EncodingCipherByKey cipher(
2
Map<String, Key> keys,
3
String algorithm,
4
Encoding encoding,
5
Charset charset
6
);
Copied!
Returns an encoding cipher with a set of preconfigured keys.

Usage Example

1
KeyStore aliceKeystore = keystore("classpath:/keystore-alice.p12", "password".toCharArray(), "PKCS12");
2
KeyStore bobKeystore = keystore("classpath:/keystore-bob.p12", "password".toCharArray(), "PKCS12");
3
Key alicePrivateKey = privateKey(aliceKeystore, "alice", "password".toCharArray());
4
Key bobPrivateKey = privateKey(bobKeystore, "bob", "password".toCharArray());
5
Key alicePublicKey = publicKey(aliceKeystore, "alice");
6
Key bobPublicKey = publicKey(bobKeystore, "bob");
7
8
Map<String, Key> keys = Map.of(
9
"alice-public", alicePublicKey,
10
"alice-private", alicePrivateKey,
11
"bob-public", bobPublicKey,
12
"bob-private", bobPrivateKey
13
);
14
15
EncodingCipherByKey cipher = cipher(keys, "RSA", BASE64, UTF_8);
16
17
// Alice writes to Bob
18
String aliceMsg01 = "Hello";
19
String aliceMsg01Encrypted = cipher.encrypt("bob-public", ENCRYPT, aliceMsg01);
20
21
// Bob decrypts Alice's message
22
String aliceMsg01Decrypted = cipher.encrypt("bob-private", DECRYPT, aliceMsg01Encrypted);
23
assertEquals(aliceMsg01, aliceMsg01Decrypted);
24
25
// Bob responds to Alice's message
26
String bobMsg01 = "Hey Alice, nice to hear from you.";
27
String bobMsg01Encrypted = cipher.encrypt("alice-public", ENCRYPT, bobMsg01);
28
29
// Alice decrypts Bob's message
30
String bobMsg01Decrypted = cipher.encrypt("alice-private", DECRYPT, bobMsg01Encrypted);
31
assertEquals(bobMsg01, bobMsg01Decrypted);
Copied!
Last modified 4mo ago