org.logi.crypto.keys
Class DHPublicKey

java.lang.Object
  |
  +--org.logi.crypto.Crypto
        |
        +--org.logi.crypto.keys.K
              |
              +--org.logi.crypto.keys.DHPublicKey
All Implemented Interfaces:
EncryptionKey, Key, java.security.Key, PrettyPrintable, java.io.Serializable, VerificationKey

public class DHPublicKey
extends org.logi.crypto.keys.K
implements EncryptionKey, VerificationKey

This object holds one Diffie-Hellman key. They can be used for Diffie-Hellman key-exchange with the DHKeyExNoninter and related classes or directly for encryption and signatures, in which case it uses the ElGamal algorithm.

The modulus and generator for the group from which the key is chosen are fixed for a given key-size. They are pre-calculated for a few bit-sizes, but take long to claculate for others.

The CDS for a Diffie-Hellman key is DHKey(x,g,m,pub) for a public key or DHKey(x,g,m,pri) for a private key. In both cases x,g and m are hexadecimal numbers.

Author:
Logi Ragnarsson (logir@logi.org)
See Also:
DHKeyExNoninter, Serialized Form

Fields inherited from class org.logi.crypto.Crypto
BIT, cdsPath, EMPTY_ARRAY, FOUR, keySource, NIBBLE, ONE, primeCertainty, random, TWO, ZERO
 
Fields inherited from interface java.security.Key
serialVersionUID
 
Constructor Summary
DHPublicKey(java.math.BigInteger y, java.math.BigInteger g, java.math.BigInteger m)
          Create a new Diffie-Hellman key object.
 
Method Summary
 int cipherBlockSize()
          Returns the size of the blocks that can be decrypted in one call to decrypt().
 void encrypt(byte[] source, int i, byte[] dest, int j)
          Encrypt one block of data.
 boolean equals(java.lang.Object o)
          Return true iff the two keys are equivalent.
 java.lang.String getAlgorithm()
          The name of the algorithm is "Diffie-Hellman".
 byte[] getEncoded()
          Return encoded version.
 Fingerprint getFingerprint()
          Return the key's fingerprint using the default hash function.
 Fingerprint getFingerprint(java.lang.String algorithm)
          Return the key's fingerprint using the specified hash function.
 java.lang.String getFormat()
          Returns the name of the key-encoding format.
 java.math.BigInteger getG()
          Return the generator for this key.
 java.math.BigInteger getM()
          Return the modulus for this key.
 int getSize()
          Return the "size" of the key.
 java.math.BigInteger getY()
          Return y.
 int hashCode()
          Return a hash-code based on the keys SHA1 fingerprint.
 boolean matches(Key key)
          Check if a key mathces this.
 Fingerprint matchFingerprint()
          Returns the default fingerprint of the matching key in the key-pair.
 Fingerprint matchFingerprint(java.lang.String algorithm)
          Returns the default fingerprint of the matching key in the key-pair.
static DHPublicKey parseCDS(java.lang.String[] param)
          Used by Crypto.fromString when parsing a CDS.
 int plainBlockSize()
          Returns the size of the blocks that can be encrypted in one call to encrypt().
 void prettyPrint(java.io.PrintWriter out, int ind, int rec)
          Print this object to out, indented with ind tabs, going down at most rec levels of recursion.
 int signatureSize()
          Returns the length of a signature in bytes.
 int signBlockSize()
          Returns the maximum size in bytes of the fingerprints that can be signed.
 java.lang.String toString()
          Return a CDS for this key.
 boolean verify(Signature s, Fingerprint fp)
          Verify a Signature on a Fingerprint with a public key.
 
Methods inherited from class org.logi.crypto.Crypto
binString, binString, ensureArrayLength, ensureArrayLength, equal, equalRelaxed, equalSub, fromHexNibble, fromHexString, fromString, fromString, hexString, hexString, hexString, hexString, hexString, initRandom, initRandom, makeClass, makeInt, makeLong, makeSessionKey, pastSpace, pickBits, pickBits, prettyPrint, readBlock, readInt, trimArrayLength, trimArrayLength, trimLeadingZeroes, writeBytes, writeBytes, writeInt
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.logi.crypto.keys.Key
getFingerprint, getFingerprint, hashCode, matchFingerprint, matchFingerprint
 
Methods inherited from interface org.logi.crypto.io.PrettyPrintable
prettyPrint
 
Methods inherited from interface java.security.Key
getEncoded, getFormat
 

Constructor Detail

DHPublicKey

public DHPublicKey(java.math.BigInteger y,
                   java.math.BigInteger g,
                   java.math.BigInteger m)
Create a new Diffie-Hellman key object. An object is created for x in the group modulo m with generator g. It is a private key iff pri is true.
Method Detail

parseCDS

public static DHPublicKey parseCDS(java.lang.String[] param)
                            throws InvalidCDSException
Used by Crypto.fromString when parsing a CDS.

A valid CDS can be created by calling the toString() method.

Throws:
InvalidCDSException - if the CDS is malformed.
See Also:
Crypto.fromString(String)

getSize

public int getSize()
Return the "size" of the key. This is a (fairly inaccurate) measure of how difficult it is to break and is heavily dependant on the algorithm used.
Specified by:
getSize in interface Key

getAlgorithm

public java.lang.String getAlgorithm()
The name of the algorithm is "Diffie-Hellman".
Specified by:
getAlgorithm in interface Key

getY

public java.math.BigInteger getY()
Return y.

getM

public java.math.BigInteger getM()
Return the modulus for this key.

getG

public java.math.BigInteger getG()
Return the generator for this key.

equals

public final boolean equals(java.lang.Object o)
Return true iff the two keys are equivalent.
Overrides:
equals in class java.lang.Object

matches

public boolean matches(Key key)
Check if a key mathces this. This is true if this and key are a matched pair of public/private.
Specified by:
matches in interface Key

toString

public java.lang.String toString()
Return a CDS for this key.
Overrides:
toString in class java.lang.Object

prettyPrint

public void prettyPrint(java.io.PrintWriter out,
                        int ind,
                        int rec)
                 throws java.io.IOException
Print this object to out, indented with ind tabs, going down at most rec levels of recursion.
Specified by:
prettyPrint in interface PrettyPrintable
Overrides:
prettyPrint in class Crypto

plainBlockSize

public int plainBlockSize()
Returns the size of the blocks that can be encrypted in one call to encrypt(). For ElGamal keys this depends on the size of the key.
Specified by:
plainBlockSize in interface EncryptionKey

cipherBlockSize

public int cipherBlockSize()
Returns the size of the blocks that can be decrypted in one call to decrypt(). For ElGamal keys this depends on the size of the key.
Specified by:
cipherBlockSize in interface EncryptionKey

encrypt

public void encrypt(byte[] source,
                    int i,
                    byte[] dest,
                    int j)
Encrypt one block of data. The plaintext is taken from source starting at offset i and ciphertext is written to dest, starting at offset j.

The amount of data read and written will match the values returned by plainBlockSize() and cipherBlockSize().

Specified by:
encrypt in interface EncryptionKey

signBlockSize

public int signBlockSize()
Returns the maximum size in bytes of the fingerprints that can be signed.
Specified by:
signBlockSize in interface VerificationKey

signatureSize

public int signatureSize()
Returns the length of a signature in bytes.
Specified by:
signatureSize in interface VerificationKey

verify

public boolean verify(Signature s,
                      Fingerprint fp)
               throws KeyException
Verify a Signature on a Fingerprint with a public key.

The method returns true iff s is a signature for fp created with the mathcin private key.

Specified by:
verify in interface VerificationKey
Throws:
KeyException - if this is not a public key

getFingerprint

public final Fingerprint getFingerprint()
Return the key's fingerprint using the default hash function.
Specified by:
getFingerprint in interface Key

getFingerprint

public final Fingerprint getFingerprint(java.lang.String algorithm)
                                 throws InvalidCDSException
Return the key's fingerprint using the specified hash function.
Specified by:
getFingerprint in interface Key
Throws:
InvalidCDSException - if the specified hash function is not available.

matchFingerprint

public Fingerprint matchFingerprint()
Returns the default fingerprint of the matching key in the key-pair.
Specified by:
matchFingerprint in interface Key

matchFingerprint

public Fingerprint matchFingerprint(java.lang.String algorithm)
                             throws InvalidCDSException
Returns the default fingerprint of the matching key in the key-pair.
Specified by:
matchFingerprint in interface Key
Throws:
InvalidCDSException - if the specified hash function is not available.

hashCode

public final int hashCode()
Return a hash-code based on the keys SHA1 fingerprint.
Specified by:
hashCode in interface Key
Overrides:
hashCode in class java.lang.Object

getEncoded

public byte[] getEncoded()
Return encoded version.
Specified by:
getEncoded in interface java.security.Key

getFormat

public java.lang.String getFormat()
Returns the name of the key-encoding format.
Specified by:
getFormat in interface java.security.Key


Copyright (c) 1997-2001 Logi Ragnarsson - Distributed under the GPL