Quick Start on RSA Encryption and Decryption using Java


You are probably not running a Java enabled browser. Please use a Java enabled browser (or enable your browser for Java) to view this applet...

Click here to download all the source files.


Steps:

  1. Generate two distinct large prime numbers p and q
  2. primeSize represents the size (number of bits) of each generated prime number.

    public static void generatePrimeNumbers()
    {
        p = new BigInteger( primeSize, 10, new Random() ) ;


        do
        {
            q = new BigInteger( primeSize, 10, new Random() ) ;
        }
        while( q.compareTo( p ) == 0 ) ;
    }

  3. Generate Public and Private Exponents / Keys
  4. The product of p and q gives the modulus, N.

    Choose a number, E, less than N and relatively prime to ( p - 1 ) * ( q - 1 ), which means E and ( p - 1 ) * ( q - 1 ) have no common factors except 1. In other words, E is coprime to and less than ( p - 1 ) * ( q - 1 ).

    Find another number D such that ( E * D - 1 ) is divisible by ( p - 1 ) * ( q - 1 ). The value of D can be obtained by computing the inverse of E mod ( p - 1 ) * ( q - 1 ). This gives a value for D such that E * D = 1 mod ( p - 1 ) * ( q - 1 ).

    The values E and D are called the public and private exponents, respectively.

    The public key is the pair (N, E) which will be published. The private key is the pair (N, D) which will be kept private.

    The factors p and q may be destroyed or kept with the private key.

    public static void generatePublicPrivateKeys()
    {
        // N = p * q
        N = p.multiply( q ) ;


        // r = ( p - 1 ) * ( q - 1 )
        r = p.subtract( BigInteger.valueOf( 1 ) ) ;
        r = r.multiply( q.subtract( BigInteger.valueOf( 1 ) ) ) ;


        // Choose E, coprime to and less than r
        do
        {
            E = new BigInteger( 2 * primeSize, new Random() ) ;
        }
        while( ( E.compareTo( r ) != -1 ) || ( E.gcd( r ).compareTo( BigInteger.valueOf( 1 ) ) != 0 ) ) ;


        // Compute D, the inverse of E mod r
        D = E.modInverse( r ) ;
    }

  5. Encrypting the Plaintext (Using Public Key)
  6. Suppose Alice wants to send a message M (Plaintext) to Bob. Alice creates the ciphertext C by exponentiating: C = ME mod N, where E and N are Bob's public key. She sends C (Ciphertext) to Bob.

    public static BigInteger[] encrypt( String message )
    {
        int i ;
        byte[] temp = new byte[1] ;


        byte[] digits = message.getBytes() ;

        BigInteger[] bigdigits = new BigInteger[digits.length] ;

        for( i = 0 ; i < bigdigits.length ; i++ )
        {
            temp[0] = digits[i] ;
            bigdigits[i] = new BigInteger( temp ) ;
        }

        BigInteger[] encrypted = new BigInteger[bigdigits.length] ;

        for( i = 0 ; i < bigdigits.length ; i++ )
            encrypted[i] = bigdigits[i].modPow( E, N ) ;


        return( encrypted ) ;
    }

  7. Decrypting the Ciphertext (Using Private Key)
  8. To decrypt, Bob also exponentiates: M = CD mod N; the relationship between E and D ensures that Bob correctly recovers M (Recovered Plaintext). Since only Bob knows D, only Bob can decrypt this message.

    public static String decrypt( BigInteger[] encrypted )
    {
        int i ;


        BigInteger[] decrypted = new BigInteger[encrypted.length] ;

        for( i = 0 ; i < decrypted.length ; i++ )
            decrypted[i] = encrypted[i].modPow( D, N ) ;

        char[] charArray = new char[decrypted.length] ;

        for( i = 0 ; i < charArray.length ; i++ )
            charArray[i] = (char) ( decrypted[i].intValue() ) ;


        return( new String( charArray ) ) ;
    }


References:

[1] RSA Laboratories, Cryptography FAQ - What is the RSA cryptosystem?, http://www.rsasecurity.com/rsalabs/faq/3-1-1.html


Copyright� 2002 by Chue Wai Lian

Last updated on Sunday, 06 January 2002

Please send all mails to [email protected] or [email protected]

Hosted by www.Geocities.ws

1