You are on page 1of 8

Tutorial 6 RSA in Java

Tutorial 6 RSA in Java

Objectives

RSA Implementation in Java

Exercises

Tutorial for CS 5285 Information Security for E-commerce 1


Tutorial 6 RSA in Java

1 RSA Implementation in Java

1.1 Generating a Pair of Keys

In this example we will generate a public-private key pair


for the algorithm named "RSA". We will generate keys with
a 1024-bit modulus, using a user-derived seed, called
userSeed. We don't care which provider supplies the
algorithm implementation.

 Creating the Key Pair Generator

The first step is to get a key pair generator object for


generating keys for the RSA algorithm:

KeyPairGenerator keyGen =
KeyPairGenerator.getInstance("RSA");

 Initializing the Key Pair Generator

The next step is to initialize the key pair generator. All key
pair generators share the concepts of a keysize and a source
of randomness. A KeyPairGenerator class
initialize method has these two types of arguments.
Thus, to generate keys with a keysize of 1024 and a new
SecureRandom object seeded by the userSeed value
(e.g. userSeed = 123456789), you can use the
following code:

Tutorial for CS 5285 Information Security for E-commerce 2


Tutorial 6 RSA in Java

long userSeed = 123456789;

SecureRandom random =
SecureRandom.getInstance("SHA1PRNG","SUN");
random.setSeed(userSeed);
keyGen.initialize(1024, random);

 Generating the Pair of Keys

The final step is generating the key pair. The following code
is used to generate the key pair:
KeyPair pair = keyGen.generateKeyPair();

1.2 Generating and Verifying a Signature

The following signature generation and verification


examples use the key pair generated in the key pair example
above.

 Generating a Signature

We first create a signature object:

Signature rsa =
Signature.getInstance("SHA1withRSA");

Tutorial for CS 5285 Information Security for E-commerce 3


Tutorial 6 RSA in Java

Next, using the key pair generated in the key pair example,
we initialize the object with the private key, and then sign a
byte array called data.

/* Initializing the object with a private key */


PrivateKey priv = pair.getPrivate();
rsa.initSign(priv);

/* Update and sign the data */


rsa.update(data);
byte[] sig = rsa.sign();

 Verifying a Signature

Verifying the signature is straightforward. (Note that here


we also use the key pair generated in the key pair example.)
/* Initializing the object with the public
key */
PublicKey pub = pair.getPublic();
rsa.initVerify(pub);

/* Update and verify the data */


rsa.update(data);
boolean verifies = rsa.verify(sig);
System.out.println("signature verifies: " +
verifies);

Tutorial for CS 5285 Information Security for E-commerce 4


Tutorial 6 RSA in Java

1.3 Implementing RSA by using BigInteger class

 BigInteger class

Immutable arbitrary-precision integers. All operations


behave as if BigIntegers were represented in two's-
complement notation (like Java's primitive integer types).
BigInteger provides analogues to all of Java's primitive
integer operators, and all relevant methods from
java.lang.Math. Additionally, BigInteger provides
operations for modular arithmetic, GCD calculation,
primality testing, prime generation, bit manipulation, and a
few other miscellaneous operations.

We can construct a randomly generated positive BigInteger


that is probably prime, with the specified bitLength as
follows:
BigInteger (int bitLength, int certainty,
Random rnd)

bitLength - bitLength of the returned BigInteger.


certainty - a measure of the uncertainty that the caller is
willing to tolerate. The probability that the new
BigInteger represents a prime number will exceed
(1 - 1/2certainty). The execution time of this
constructor is proportional to the value of this
parameter.
rnd - source of random bits used to select candidates to be
tested for primality.

Tutorial for CS 5285 Information Security for E-commerce 5


Tutorial 6 RSA in Java

 Generating the Pair of Keys

Generate two distinct large prime numbers p, q:


int SIZE = 512;
p = new BigInteger(SIZE, 10, new Random());
q = new BigInteger(SIZE, 10, new Random());

Calculate their product:


N = p.multiply(q);

Calculate φ(N) = (p-1)(q-1):


PhiN = p.subtract(BigInteger.valueOf(1));
PhiN =
PhiN.multiply( q.subtract( BigInteger.valu
eOf(1)));

Next choose e, coprime to and less than φ(N):


do{
e = new BigInteger(2*SIZE, new Random());
}
while ((e.compareTo(PhiN) != -1) ||
(e.gcd(PhiN).compareTo( BigInteger.valueOf
(1)) != 0));

Compute d, the inverse of e mod PhiN:


d = e.modInverse(PhiN);

 Encrypting and Decrypting messages

Encrypt a message using N and e:


ciphertext = message.modPow(e, N);

Decrypt the message using N and d:

Tutorial for CS 5285 Information Security for E-commerce 6


Tutorial 6 RSA in Java

plaintext = ciphertext.modPow(d, N);

Tutorial for CS 5285 Information Security for E-commerce 7


Tutorial 6 RSA in Java

2 Exercises

 Try the above programs (all Java program files can be


downloaded from course web)

 Write a RSA signing and verifying Java program by


using BigInteger class.

Tutorial for CS 5285 Information Security for E-commerce 8

You might also like