|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.logi.crypto.Crypto
org.logi.crypto.keys.K
org.logi.crypto.keys.RSAKey
An instance of this class handles a single RSA key.
The RSA algorithm is probably the best known and most widely used public key algorithm. Breaking one RSA key is believed to be as difficult as factoring the modulus (n) of the group in which calculations are done. When speaking of the size of an RSA key, it is understood to be the size of this modulus.
The first 512 bit number is expected to be factored by the end of 1999. 1024 bits should be more than enough in most cases, but the clinically paranoid may want to use up to 4096 bit keys.
Each RSA key is a pair (r,n) of integers and matches another key (s,n). If P is a block of plain data represented as an integer smaller than n, then it can be encrypted with the transformation:
E = (P^r) mod n
which has the inverse transformation:
P = (E^s) mod n
The key's owner will keep one key secret and publish the other as widely as possible. This allows anyone who gets hold of the public key to encrypt data which can only be decrypted with the corresponding private key. The public key in the pair will always use the exponent 65537.
Data that is encrypted with a private key can similarly only be decrypted with the corresponding public key. This is useful for digital signatures.
When P is created from an array of bytes, it will correspond to as many bytes of plain data as the bytes needed to store n, less one. When encrypting less than a full block of data, the data should be put in the most significant bytes of the plaintext-block and appended with random data. This is done by all relevant classes in the logi.crypto library. The plaintext block is encrypted to form a ciphertext block with as many bytes as are needed to store the modulus.
This implementation was originally done from a description given in Gallian's Contemporary Abstract Algebra, but various changes from various sources have been incorporated.
When a key-pair is created, the private key will actually be an instance of the RSAKeyChin class, which uses the Chinese Remainder Theorem to speed up exponentiation.
The CDS for the RSAKey class is RSAKey(r,n,pub)
for a public key,
RSAKey(r,n,pri)
for a private key or RSAKey(r,n,p)
for a private key where we know one factor of n
. In all cases
r
, n
and p
are hexadecimal numbers.
Crypto.fromString(String)
Field Summary | |
protected Fingerprint |
matchPrint
The fingerprint for the other key in the pair, or null. |
protected java.math.BigInteger |
n
The RSA key modulus |
protected boolean |
pri
Is it a private key? |
protected java.math.BigInteger |
r
The RSA key exponent |
protected static java.math.BigInteger |
R
R is the exponent used in all created public keys. |
Fields inherited from class org.logi.crypto.keys.K |
fingerprint, otherFingerprint |
Fields inherited from class org.logi.crypto.Crypto |
BIT, cdsPath, EMPTY_ARRAY, FOUR, keySource, NIBBLE, ONE, primeCertainty, random, TWO, ZERO |
Constructor Summary | |
RSAKey(java.math.BigInteger r,
java.math.BigInteger n,
boolean pri)
Create a new RSA key (r,n) . |
Method Summary | |
protected Fingerprint |
calcFingerprint(boolean other,
java.lang.String algorithm)
Calculate the fingerprint for this key or the other in the pair. |
int |
cipherBlockSize()
Returns the size of the blocks that can be decrypted in one call to decrypt(). |
static KeyPair |
createKeys(java.math.BigInteger r,
java.math.BigInteger s,
java.math.BigInteger n)
Create a KeyPair object holding objects for the public RSA key (r,n) and the private RSA key (s,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. |
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. |
static java.math.BigInteger |
findPrime(java.math.BigInteger start)
Returns the largest prime p <= start |
java.lang.String |
getAlgorithm()
The name of the algorithm is "RSA". |
java.math.BigInteger |
getExponent()
Return the RSA exponent. |
java.math.BigInteger |
getModulus()
Return the RSA modulus. |
int |
getSize()
Return the size of the key modulo in bits. |
boolean |
isPrivate()
Return true iff this is a private key. |
boolean |
matches(Key key)
Check if a key mathces this. |
static RSAKey |
parseCDS(java.lang.String key)
If "RSAKey( key )" is a valid CDS for an RSAKey, then RSAKey.parseCDS(key) will return the described RSAKey object. |
int |
plainBlockSize()
Returns the size of the blocks that can be encrypted in one call to encrypt(). |
Signature |
sign(Fingerprint fp)
Create a signature for a Fingerprint fith 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. |
boolean |
verify(Signature s,
Fingerprint fp)
Verify a Signature on a Fingerprint with a public key. |
Methods inherited from class org.logi.crypto.keys.K |
getFingerprint, getFingerprint, hashCode, matchFingerprint, matchFingerprint |
Methods inherited from class org.logi.crypto.Crypto |
binString, binString, equal, equalRelaxed, equalSub, fromHexNibble, fromHexString, fromString, fromString, hexString, hexString, hexString, hexString, hexString, initRandom, initRandom, makeClass, makeInt, makeLong, makeSessionKey, pastSpace, pickBits, pickBits, readBlock, readInt, writeBytes, writeBytes, writeInt |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.logi.crypto.keys.Key |
getFingerprint, getFingerprint, hashCode, matchFingerprint, matchFingerprint |
Field Detail |
protected static final java.math.BigInteger R
R
is the exponent used in all created public keys.
protected java.math.BigInteger r
protected java.math.BigInteger n
protected boolean pri
protected Fingerprint matchPrint
Constructor Detail |
public RSAKey(java.math.BigInteger r, java.math.BigInteger n, boolean pri)
(r,n)
.
It is a private key if pri
is true.
Method Detail |
public static RSAKey parseCDS(java.lang.String key) throws InvalidCDSException
A valid CDS can be created by calling the RSAKey.toString() method.
InvalidCDSException
- if the CDS is malformed.Crypto.fromString(String)
public static java.math.BigInteger findPrime(java.math.BigInteger start)
p <= start
public static KeyPair createKeys(int bitLength)
bitLength
or bitLength-1
bits.
public static KeyPair createKeys(java.lang.String username, java.lang.String password, java.lang.String hashFunction, int bitLength) throws InvalidCDSException
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 p
by 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.
InvalidCDSException
- if the specified hash function is not
available.public static KeyPair createKeys(java.math.BigInteger r, java.math.BigInteger s, java.math.BigInteger n) throws KeyException
(r,n)
and the private RSA key (s,n).
KeyException
- if (r,n) and (s,n) does not describe a valid
pair of RSA keys.public int getSize()
getSize
in interface Key
public java.lang.String getAlgorithm()
getAlgorithm
in interface Key
public java.math.BigInteger getExponent()
public java.math.BigInteger getModulus()
protected Fingerprint calcFingerprint(boolean other, java.lang.String algorithm) throws InvalidCDSException
calcFingerprint
in class K
InvalidCDSException
org.logi.crypto.K#getFingerprint
,
org.logi.crypto.K#matchFingerprint
public boolean isPrivate()
isPrivate
in interface Key
public java.lang.String toString()
Crypto.fromString(java.io.Reader)
public boolean equals(java.lang.Object o)
public final boolean matches(Key key)
matches
in interface Key
public int plainBlockSize()
plainBlockSize
in interface CipherKey
public int cipherBlockSize()
cipherBlockSize
in interface CipherKey
public void encrypt(byte[] source, int i, byte[] dest, int j)
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()
.
encrypt
in interface CipherKey
public void decrypt(byte[] source, int i, byte[] dest, int j)
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()
.
decrypt
in interface CipherKey
public int signBlockSize()
signBlockSize
in interface SignatureKey
public int signatureSize()
signatureSize
in interface SignatureKey
public Signature sign(Fingerprint fp) throws KeyException
sign
in interface SignatureKey
KeyException
- if the key modulus is shorter than the signature.
KeyException
- if this is not a private keypublic boolean verify(Signature s, Fingerprint fp) throws KeyException
The method returns true iff s
is a signature for
fp
created with the mathcin private key.
verify
in interface SignatureKey
KeyException
- if this is not a public key
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |