Class RSA

java.lang.Object

com.wjholden.crypto.rsa.RSA

public class RSA
extends java.lang.Object

This program implements a subset of the
RSA Cryptography Standard
(specifically RSA/ECB/PKCS1Padding), also known as PKCS #1.
Much of the code in this program is copied or strongly influenced by
Robert Sedgewick and Kevin Wayne.
I also found a paper by
Evgeny Milanov
useful in writing this program and understanding RSA.
The RSA algorithm is surprisingly simple!

Generate two large prime numbers p and q. Together, pq = n.

Next, choose a prime encryption exponent, e, greater than 2.
This value should always be set to 65537.

To use this class, simply create an instance, generate keys, then encrypt and decrypt messages
with the public methods.
RSA myRSA = new RSA();
myRSA.init(1024);
System.out.println(myRSA.decrypt(myRSA.encrypt("Hello world!")));

You can also import and export the keys to a Java KeyPair object that should be
compatible to other Java applications.

Method Summary

In PKCS #1v1.5
(RFC 2313), the encryption block (EB) contains a block type (BT)
and padding string (PS), surrounded by zeros and followed by the data (D).EB = 00 || BT || PS || 00 || D
This implementation is only intended to encrypt data using the public key,
so the block type is set to 02.
The padding string
is at least eight octets long, with an exact length of
k - 3 - mLen (mLen is the length in octets of the data).

PKCS #1v1.5
requires 11 bytes of padding before the message, so the maximum block size allowed is
k - 11, where k is the length of the RSA modulus, n, in bytes.
This limit must be enforced to prevent the message to be encrypted from being
"larger
than the RSA modulus.

RSA

Creates an instance of RSA using specified RSA encryption exponent, prime numbers
p and q, decryption exponent, and modulus with no error checking.

Parameters:

e - RSA encryption exponent.

p - Large prime number.

q - Another large prime number.

d - RSA decryption exponent.

n - RSA modulus (n = p*q).

RSA

public RSA(int keyLength)

Creates an instance of RSA and generates keys of the given key length.

Parameters:

keyLength - Length of the key to be generated from 1024 to 4096.

RSA

public RSA(java.security.KeyPair kp)

Creates an instance of RSA and imports keys from the given key pair.

Parameters:

kp - KeyPair to import and replace values of d, e, and n.

Method Detail

addPadding

private byte[] addPadding(byte[] input)

In PKCS #1v1.5
(RFC 2313), the encryption block (EB) contains a block type (BT)
and padding string (PS), surrounded by zeros and followed by the data (D).EB = 00 || BT || PS || 00 || D
This implementation is only intended to encrypt data using the public key,
so the block type is set to 02.
The padding string
is at least eight octets long, with an exact length of
k - 3 - mLen (mLen is the length in octets of the data).
These bytes must contain "pseudo-randomly generated nonzero octets."

Parameters:

input - The byte array to pad.

Returns:

The same byte array, padded according to PKCS #1v1.5 with nonzero random octets.

exportKeys

Exports generated keying material in a familiar KeyPair format
that should be usable in other programs.

Returns:

An RSA KeyPair that can be serialized or reused in other applications.

Throws:

java.security.NoSuchAlgorithmException

java.security.spec.InvalidKeySpecException

getD

protected java.math.BigInteger getD()

Gives package members direct access to RSA decryption exponent.

Returns:

The RSA decryption exponent d.

getE

protected java.math.BigInteger getE()

Gives package members direct access to RSA encryption exponent.

Returns:

The RSA encryption exponent e.

getMaximumBlockSize

public int getMaximumBlockSize()

PKCS #1v1.5
requires 11 bytes of padding before the message, so the maximum block size allowed is
k - 11, where k is the length of the RSA modulus, n, in bytes.
This limit must be enforced to prevent the message to be encrypted from being
"larger
than the RSA modulus.

Returns:

The maximum allowable message size, in bytes, and 0 if keys are initialized.

getModulusLength

public int getModulusLength()

Indicates the length of the RSA modulus n in bits.
This should equal the value passed to generateKeys.

Returns:

The length of the RSA modulus, 0 if n is not defined.

getN

protected java.math.BigInteger getN()

Gives package members direct access to RSA modulus.

Returns:

The RSA modulus n.

getP

protected java.math.BigInteger getP()

Gives package members direct access to prime number p used to generate n.

Returns:

Prime number p.

getQ

protected java.math.BigInteger getQ()

Gives package members direct access to prime number q used to generate n.