RSA Implementation in Java(I found this paper in journal hope that it will help you)
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:
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();
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");
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);
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.
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.valueOf(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:
plaintext = ciphertext.modPow(d, N);
I am thankful to you for elaborating each point in such a cool way. This is the first time I have understood about this algorithm because I read many articles so far but they all confused me.
ReplyDeleteeSignature