org.logi.crypto.keys
Class RSAPrivateKey

java.lang.Object
  |
  +--org.logi.crypto.Crypto
        |
        +--org.logi.crypto.keys.K
              |
              +--org.logi.crypto.keys.RSAPrivateKey
All Implemented Interfaces:
BlindSigningKey, DecryptionKey, Key, java.security.Key, PrettyPrintable, java.security.PrivateKey, java.security.interfaces.RSAKey, java.security.interfaces.RSAPrivateKey, java.io.Serializable, SigningKey

public class RSAPrivateKey
extends org.logi.crypto.keys.K
implements DecryptionKey, SigningKey, BlindSigningKey, java.security.interfaces.RSAPrivateKey

An instance of this class handles a single RSA private key.

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
 
Fields inherited from interface java.security.PrivateKey
serialVersionUID
 
Constructor Summary
RSAPrivateKey(java.math.BigInteger d, java.math.BigInteger n)
          Create a new RSA key (r,n).
RSAPrivateKey(java.math.BigInteger d, java.math.BigInteger p, java.math.BigInteger q)
          Create a new RSA key (r,n).
 
Method Summary
 int cipherBlockSize()
          Returns the size of the blocks that can be decrypted in one call to decrypt().
static KeyPair createKeys(java.math.BigInteger e, java.math.BigInteger d, java.math.BigInteger n)
          Create a KeyPair object holding objects for the public RSA key (e,n) and the private RSA key (d,n).
static KeyPair createKeys(int bitLength)
          Create a pair of public/private keys.
static KeyPair createKeys(java.lang.String username, java.lang.String password, java.lang.String hashFunction, int bitLength)
          Create a pair of public/private keys from a username/password pair.
 void decrypt(byte[] source, int i, byte[] dest, int j)
          Decrypt 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 "RSA".
 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 getModulus()
          Return public modulus.
 java.math.BigInteger getPrivateExponent()
          Return private exponent.
 int getSize()
          Return the size of the key modulo in bits.
 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 RSAPrivateKey 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.
 BlindSignature sign(BlindFingerprint fp)
          Create a signature for a blinded fingerprint with a private key.
 Signature sign(Fingerprint fp)
          Create a signature for a Fingerprint with a private key.
 int signatureSize()
          Returns the length of the signature in bytes.
 int signBlockSize()
          Returns the maximum size in bytes of the fingerprint that can be signed.
 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, 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

RSAPrivateKey

public RSAPrivateKey(java.math.BigInteger d,
                     java.math.BigInteger n)
Create a new RSA key (r,n).

RSAPrivateKey

public RSAPrivateKey(java.math.BigInteger d,
                     java.math.BigInteger p,
                     java.math.BigInteger q)
Create a new RSA key (r,n). It is a private key if pri is true.
Method Detail

parseCDS

public static RSAPrivateKey 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)

createKeys

public static KeyPair createKeys(int bitLength)
Create a pair of public/private keys. The key modulo will be bitLength or bitLength-1 bits.

createKeys

public static KeyPair createKeys(java.lang.String username,
                                 java.lang.String password,
                                 java.lang.String hashFunction,
                                 int bitLength)
                          throws InvalidCDSException
Create a pair of public/private keys from a username/password pair. The public exponent will be 65536 and the private exponent will have bitLength or bitLength-1 bits.

The keys are created by hashing the password, appending with 0's until it is bitLength bits long and searching for a prime pby counting down from there. Another prime q is found in the same way, but the username is prepended to the password before hashing. Key-generation proceeds as normally from there.

The hashFunction parameters directs which hash function to use. It must be the name of a supported hash function, such as MD5 or SHA1.

The username does not need to be secret and can in fact be a fixed string. It plays a similar role as SALT in unix password systems in protecting against dictionary attacks.

Throws:
InvalidCDSException - if the specified hash function is not available.

createKeys

public static KeyPair createKeys(java.math.BigInteger e,
                                 java.math.BigInteger d,
                                 java.math.BigInteger n)
                          throws KeyException
Create a KeyPair object holding objects for the public RSA key (e,n) and the private RSA key (d,n).
Throws:
KeyException - if (e,n) and (d,n) does not describe a valid pair of RSA keys.

getSize

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

getAlgorithm

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

getModulus

public java.math.BigInteger getModulus()
Return public modulus.
Specified by:
getModulus in interface java.security.interfaces.RSAKey

getPrivateExponent

public java.math.BigInteger getPrivateExponent()
Return private exponent.
Specified by:
getPrivateExponent in interface java.security.interfaces.RSAPrivateKey

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)

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

equals

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

matches

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

plainBlockSize

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

cipherBlockSize

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

decrypt

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

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

Specified by:
decrypt in interface DecryptionKey

signBlockSize

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

signatureSize

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

sign

public Signature sign(Fingerprint fp)
               throws CryptoException
Create a signature for a Fingerprint with a private key. If fp is a BlindFingerprint, then a BlindSignature will be returned, so in this case the return value can be safely typecast to BlindSignature.
Specified by:
sign in interface SigningKey
Throws:
KeyException - if the key modulus is shorter than the signature.
KeyException - if this is not a private key

sign

public BlindSignature sign(BlindFingerprint fp)
                    throws CryptoException
Create a signature for a blinded fingerprint with a private key.
Specified by:
sign in interface BlindSigningKey
Throws:
KeyException - if there are problems, depending on the implementing class.

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