2

I have an assignment on RSA implementation. Just now started working on it. I need some ideas on random key generation. I'm clear with the theories, but not clear on how to generate those random numbers for public key and private key. Could I get some help regarding this?

1 Answer 1

3

A Java implementation of RSA could be something similar to this:

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.*;

class Rsa_Implementation
{
 private BigInteger n, d, e;

 public Rsa_Implementation(int bitlen)
{
 SecureRandom r = new SecureRandom();
 BigInteger p = new BigInteger(bitlen / 2, 100, r);
 BigInteger q = new BigInteger(bitlen / 2, 100, r);
 n = p.multiply(q);
 BigInteger m = (p.subtract(BigInteger.ONE))
                .multiply(q.subtract(BigInteger.ONE));
 e = new BigInteger("3");
 while(m.gcd(e).intValue() > 1) e = e.add(new BigInteger("2"));
 d = e.modInverse(m);
 }
public BigInteger encrypt(BigInteger message)
{
 return message.modPow(e, n);
}
public BigInteger decrypt(BigInteger message)
 {
return message.modPow(d, n);
}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Mithun. Now I could do my encryption and decryption. But how to transfer the encrypted message to the destination.
Stack overflow is for helping each other clear programming doubts and queries and not for doing each other's work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.