java.lang.Object
net.i2p.crypto.elgamal.impl.ElGamalPublicKeyImpl
All Implemented Interfaces:
Serializable, AsymmetricKey, BinaryEncodablePREVIEW, Key, PublicKey, DHKey, DHPublicKey, ElGamalKey, ElGamalPublicKey

public class ElGamalPublicKeyImplextends Object implements ElGamalPublicKey, DHPublicKey
Implementation of ElGamal public keys.

This class provides a concrete implementation of the ElGamalPublicKey interface, representing the public component of an ElGamal key pair. It stores the public value y = g^x mod p and the ElGamal parameters (prime p and generator g), and supports X.509 encoding for standardized key storage and transmission.

See Also:
  • Field Details

  • Constructor Details

    • ElGamalPublicKeyImpl

      public ElGamalPublicKeyImpl(ElGamalPublicKeySpec spec)
      Adopts the y value and parameters of an ElGamal key spec.
      Parameters:
      spec - the ElGamal public key spec
    • ElGamalPublicKeyImpl

      public ElGamalPublicKeyImpl(DHPublicKeySpec spec)
      Adopts the y value and parameters of a DH key spec.
      Parameters:
      spec - the DH public key spec
    • ElGamalPublicKeyImpl

      public ElGamalPublicKeyImpl(ElGamalPublicKey key)
      Copies an existing ElGamal public key.
      Parameters:
      key - the ElGamal public key to copy
    • ElGamalPublicKeyImpl

      public ElGamalPublicKeyImpl(DHPublicKey key)
      Copies an existing DH public key into ElGamal form.
      Parameters:
      key - the DH public key to copy
    • ElGamalPublicKeyImpl

      public ElGamalPublicKeyImpl(BigInteger y, ElGamalParameterSpec elSpec)
      Builds a key from a bare public value and its parameters.
      Parameters:
      y - the public value y = g^x mod p
      elSpec - the ElGamal parameters
    • ElGamalPublicKeyImpl

      public ElGamalPublicKeyImpl(X509EncodedKeySpec spec) throws InvalidKeySpecException
      Rejects X.509 encoding, which is not implemented here.
      Parameters:
      spec - the X.509 key spec
      Throws:
      InvalidKeySpecException - always, not yet implemented
  • Method Details

    • getAlgorithm

      public String getAlgorithm()
      Names the ElGamal algorithm this key belongs to.
      Specified by:
      getAlgorithm in interface Key
      Returns:
      the JCA algorithm name for this key, always "ElGamal"
    • getFormat

      public String getFormat()
      Names the X.509 encoding this key serialises to.
      Specified by:
      getFormat in interface Key
      Returns:
      the JCA key format name for this key, always "X.509"
    • getEncoded

      public byte[] getEncoded()
      Serialises p, g and y into the X.509 SubjectPublicKeyInfo form.
      Specified by:
      getEncoded in interface Key
      Returns:
      the X.509 SubjectPublicKeyInfo encoding, a DER sequence holding p, g and y
    • spaceFor

      static int spaceFor(int val)
      Calculate the TLV space for a value of the given length.
      Parameters:
      val - the length of the value, 65535 max
      Returns:
      the length of the TLV
    • getParameters

      public ElGamalParameterSpec getParameters()
      Fetches the ElGamal group parameters.
      Specified by:
      getParameters in interface ElGamalKey
      Returns:
      the ElGamal group parameters this key was built with, never null
    • getParams

      public DHParameterSpec getParams()
      Wraps the same group parameters in a fresh DH spec.
      Specified by:
      getParams in interface AsymmetricKey
      Specified by:
      getParams in interface DHKey
      Specified by:
      getParams in interface DHPublicKey
      Returns:
      a newly built DH spec carrying the same prime p and generator g
    • getY

      public BigInteger getY()
      The public value y = g^x mod p.
      Specified by:
      getY in interface DHPublicKey
      Specified by:
      getY in interface ElGamalPublicKey
      Returns:
      the public value y = g^x mod p, never null
    • equals

      public boolean equals(Object obj)
      Compare this key with another object.
      Overrides:
      equals in class Object
      Returns:
      true if the y values and DH parameters are equal
    • hashCode

      public int hashCode()
      Hash code of this key.
      Overrides:
      hashCode in class Object
      Returns:
      the y value and DH parameters hashed together
    • readObject

      private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
      Read object.
      Parameters:
      in - the stream to deserialize the y value and the ElGamal parameters from
      Throws:
      IOException - if the stream cannot be read
      ClassNotFoundException - if the serialized class is not available to this router
    • writeObject

      private void writeObject(ObjectOutputStream out) throws IOException
      Write object.
      Parameters:
      out - the stream to serialize the y value and the ElGamal parameters to
      Throws:
      IOException - if the stream cannot be written