org.logi.crypto.keys
Class BlowfishKey

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

public class BlowfishKey
extends org.logi.crypto.keys.SymmetricKey
implements CipherKey

This is the class for Blowfish keys. It is a feistel blockcipher with a 64-bit block size and variable key-size. It was designed by Bruce Schneier and is in the public domain. (This implementation is licensed in the same way as the rest of the logi.crypto package.)

This implementation was made from the description given in Schneier's book Applied Cryptography.

The CDS for a Blowfish key is BlowfishKey(key) with key a string of hexadecimal digits to create a specific key or BlowfishKey(?) for a random BlowfishKey object.

Author:
Logi Ragnarsson (logir@logi.org)
See Also:
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
BlowfishKey()
          Create a new random 448 bit Blowfish key.
BlowfishKey(byte[] key)
          Create a new Blowfish key with the key bits from key.
BlowfishKey(int n)
          Create a new random n-bit Blowfish key.
 
Method Summary
 int cipherBlockSize()
          The block-size for the Blowfish cipher is 8 bytes.
 void decrypt(byte[] source, int i, byte[] dest, int j)
          Decrypt one block of data.
 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 "Blowfish".
 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.
 byte[] getKey()
          Return the key-bits for this key as an array of bytes.
 int getSize()
          Return the size of this key.
 int hashCode()
          Return a hash-code based on the keys SHA1 fingerprint.
 boolean matches(Key key)
          Returns true if this and key are the same symmetric key.
 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 BlowfishKey parseCDS(java.lang.String[] param)
          Used by Crypto.fromString when parsing a CDS.
 int plainBlockSize()
          The block-size for the Blowfish cipher is 8 bytes.
 java.lang.String toString()
          Return a CDS for this 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, 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, matches, matchFingerprint, matchFingerprint
 
Methods inherited from interface org.logi.crypto.io.PrettyPrintable
prettyPrint, prettyPrint
 
Methods inherited from interface java.security.Key
getEncoded, getFormat
 

Constructor Detail

BlowfishKey

public BlowfishKey(int n)
Create a new random n-bit Blowfish key. The size will be rounded up to the next multiple of 8.

BlowfishKey

public BlowfishKey()
Create a new random 448 bit Blowfish key.

BlowfishKey

public BlowfishKey(byte[] key)
Create a new Blowfish key with the key bits from key.
Method Detail

parseCDS

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

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

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

plainBlockSize

public int plainBlockSize()
The block-size for the Blowfish cipher is 8 bytes.
Specified by:
plainBlockSize in interface EncryptionKey

cipherBlockSize

public int cipherBlockSize()
The block-size for the Blowfish cipher is 8 bytes.
Specified by:
cipherBlockSize in interface EncryptionKey

getSize

public int getSize()
Return the size of this key.
Specified by:
getSize in interface Key

getAlgorithm

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

equals

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

getKey

public byte[] getKey()
Return the key-bits for this key as an array of bytes.

toString

public java.lang.String toString()
Return a CDS for this key.
Overrides:
toString in class java.lang.Object
See Also:
Crypto.fromString(java.io.Reader)

encrypt

public void encrypt(byte[] source,
                    int i,
                    byte[] dest,
                    int j)
Encrypt one block of data. The plaintext is taken from source[i..i+7] and ciphertext is written to dest[j..j+7]
Specified by:
encrypt in interface EncryptionKey

decrypt

public void decrypt(byte[] source,
                    int i,
                    byte[] dest,
                    int j)
Decrypt one block of data. The encrypted data is taken from source[i..i+7] and plaintext is written to dest[j..j+7].
Specified by:
decrypt in interface DecryptionKey

matches

public boolean matches(Key key)
Returns true if this and key are the same symmetric key.

Symmetric keys simply call equals(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