Class KeyGate

java.lang.Object
net.vincent.communidirect.common.crypto.KeyGate

public final class KeyGate extends Object
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
    Constructor
    Description
    KeyGate(byte[] key)
    Creates a KeyGate from an existing key.
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    decrypt(byte[] data)
    Decrypts data using the stored key.
    byte[]
    encrypt(byte[] data)
    Encrypts data using the stored key (XOR cipher).
    Returns a 5×5 symmetric ASCII art string that visually identifies this key in a terminal.
    byte[]
    Returns a defensive copy of the raw key bytes, suitable for transmission to a peer during key exchange.
    static KeyGate
    withNewKey(int size)
    Factory method – generates a new cryptographically-strong random key.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • KeyGate

      public KeyGate(byte[] key)
      Creates a KeyGate from an existing key.
      Parameters:
      key - non-null, non-empty key bytes; a defensive copy is stored internally
      Throws:
      IllegalArgumentException - if key is null or empty
  • Method Details

    • withNewKey

      public static KeyGate withNewKey(int size)
      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)
      Encrypts data using the stored key (XOR cipher).
      Parameters:
      data - plaintext bytes
      Returns:
      ciphertext bytes
    • decrypt

      public byte[] decrypt(byte[] data)
      Decrypts data using the stored key. Because XOR is its own inverse, this is identical to encrypt(byte[]).
      Parameters:
      data - ciphertext bytes
      Returns:
      plaintext bytes
    • getAvatar

      public String 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