0

I have the public and private key generation working. My next step is to create 2 more methods - encrypt and decrypt. I'm just not sure about how to implement the encrypt and decrypt. I have a few ideas, but nothing that seems to be a good solution. Any insights?

public class RSA
{
    private final static BigInteger one = new BigInteger("1");
    private final static SecureRandom random = new SecureRandom();

    // prime numbers
    private BigInteger p;
    private BigInteger q;

    // modulus
    private BigInteger n;

    // totient
    private BigInteger t;

    // public key
    private BigInteger e;

    // private key
    private BigInteger d;

    /**
     * Constructor for objects of class RSA
     */
    public RSA(int N)
    {
        p = BigInteger.probablePrime(N/2, random);
        q = BigInteger.probablePrime(N/2, random);

        // initialising modulus
        n = p.multiply(q);

        // initialising t by euclid's totient function (p-1)(q-1)
        t = (p.subtract(one)).multiply(q.subtract(one));

        // initialising public key ~ 65537 is common public key
        e = new BigInteger("65537");
    }

    public int generatePrivateKey()
    {
         d = e.modInverse(t);
         return d.intValue();
    }
}
1
  • Your private key generation function may fail. As an exercise, determine why. HINT: It will fail approximately with prob = 2/65537. Also, d.intValue() is a meaningless quantity. Commented Jan 1, 2011 at 2:51

2 Answers 2

4

Since you haven't really asked a specific question, I'll offer you a somewhat tangential answer.

DIY Crypto is famously a bit like DIY nuclear power.

I recommend reading bouncy castle if you want to learn about crypto coding, and using it rather than rolling your own.

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what you're asking, but for RSA crypto, if your modulus is n bits wide, your public key will also be n bits wide. A simple int won't work.

See also

My own humble attempt at RSA encryption in Java:
http://david.tribble.com/src/java/tribble/crypto/RSACipher.java

Comments

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.