Class KeyGate
java.lang.Object
net.vincent.communidirect.common.crypto.KeyGate
Stateful wrapper around
CryptoEngine that holds a symmetric key and
exposes high-level encrypt / decrypt / avatar operations.
Both the server and client modules depend on communidirect-common, so
this single class provides a unified cryptographic entry-point for both sides
of a connection without duplicating logic.
Usage example:
// Server – generate a fresh session key
KeyGate gate = KeyGate.withNewKey(32);
byte[] cipher = gate.encrypt("Hello, peer!".getBytes());
// Client – reconstruct gate from the exchanged key bytes
KeyGate gate = new KeyGate(receivedKeyBytes);
String plain = new String(gate.decrypt(cipher));
// Show visual identity in terminal
System.out.println(gate.getAvatar());
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] data) Decryptsdatausing the stored key.byte[]encrypt(byte[] data) Encryptsdatausing the stored key (XOR cipher).Returns a 5×5 symmetric ASCII art string that visually identifies this key in a terminal.byte[]getKey()Returns a defensive copy of the raw key bytes, suitable for transmission to a peer during key exchange.static KeyGatewithNewKey(int size) Factory method – generates a new cryptographically-strong random key.
-
Constructor Details
-
KeyGate
public KeyGate(byte[] key) Creates aKeyGatefrom an existing key.- Parameters:
key- non-null, non-empty key bytes; a defensive copy is stored internally- Throws:
IllegalArgumentException- ifkeyis null or empty
-
-
Method Details
-
withNewKey
Factory method – generates a new cryptographically-strong random key.- Parameters:
size- key length in bytes (must be > 0)- Returns:
- a ready-to-use
KeyGate
-
encrypt
public byte[] encrypt(byte[] data) Encryptsdatausing the stored key (XOR cipher).- Parameters:
data- plaintext bytes- Returns:
- ciphertext bytes
-
decrypt
public byte[] decrypt(byte[] data) Decryptsdatausing the stored key. Because XOR is its own inverse, this is identical toencrypt(byte[]).- Parameters:
data- ciphertext bytes- Returns:
- plaintext bytes
-
getAvatar
Returns a 5×5 symmetric ASCII art string that visually identifies this key in a terminal. Peers sharing the same key will see the same avatar.- Returns:
- multi-line avatar string
-
getKey
public byte[] getKey()Returns a defensive copy of the raw key bytes, suitable for transmission to a peer during key exchange.- Returns:
- copy of the key bytes
-