Shane Chism

RSA Key Generation Tool (Command Line)


Description


Note: This tool will only generate usable RSA keys for you (and, currently, only from a command line). To actually implement RSA in a Java application, see the RSA Encryption/Decryption Class.

In order to implement an RSA encryption method you'll first need public and private keys for encryption and decryption, respectively. Due to the mathematics utilized by the algorithm, the public key will allow encryption of a message however can not be used to decrypt. This allows for a safe method of encryption and transit.

The keys must be generated on the following premises:

{lbrace}{exists(p,q) in P : p <> q}{rbrace}
{lbrace}{exists n in bbZ : n = pq}{rbrace}
{lbrace}{exists e in bbZ : e < n, gcd( e, phi(n) ) = 1}{rbrace}
{lbrace}{exists d in bbZ : d = e^-1 mod phi(n)}{rbrace}

Where, e = Encryption Exponent, d = Decryption Exponent, n = Modulus

Usage


Once keys are used, one can utilize them in their RSA encryption scheme.

The most efficient manner of transmitting a message protected by RSA is, surprisingly, not by encrypting it with RSA. Instead, encrypt a message using an efficient symmetric key encryption, such as AES-256, and then encrypt that key with RSA. Then, transmit both the key as well as the encrypted version of the message.

To run the program, compile the script using a Java compiler and then run the class file.

Code

RSAUtility.java - v1.0.2
View Plain   Download Code
/*************************************************
 * RSA Key Generation Utility
 * Version: 1.0.2
 * Coded by: Shane Chism <http://shanechism.com>
 * Updates: http://shanechism.com/code/static/9
 * Distributed under the GNU Public License
 *************************************************/

import java.util.*;
import java.lang.*;
import java.math.*;
import java.security.*;

class RSAUtility {

	public static int numBits = 1024;

	public static void main( String[] args ){
		
		Scanner sysInput = new Scanner( System.in );
		
		while( true ){
		
			int choice = 0;
			boolean validChoice = false;
			
			
			do {
			
				System.out.println( "---------- RSA Utility Menu Choices ---------" );
				System.out.println( "1. Generate Keys" );
				System.out.println( "2. Set Bit Key Strength (Currently " + numBits + ")" );
				System.out.println( "3. Exit (or type \"exit\")" );
				System.out.print( "\nPlease enter your choice: " );
			
				try {
						
					String temp = sysInput.nextLine();
					
					if( temp.toLowerCase().equals( "exit" ) )
						System.exit( 0 );
					
					choice = Integer.parseInt( temp );
					
					if( choice != 1 && choice != 2 && choice != 3 )
						throw new Exception();
					
					validChoice = true;
					
				}catch( Exception e ){
					System.out.println( "Error: That is not a valid choice. Please try again.\n" );
				}
				
			}while( !validChoice );
			
			switch( choice ){
				
				case 1	:	generateKeys( numBits ); break;
				case 2	:	setNumBits(); break;
				case 3	:	System.exit( 0 ); break;
				default	:	System.out.println( "Error: An unknown error has occured." );
							System.exit( 0 );
				
			}

		}
		
	}

	public static void generateKeys( int numBits ){
		
		System.out.println( "Generating keys, please stand by.\n" );
		
		SecureRandom sRand = new SecureRandom();
		
		
		BigInteger p = BigInteger.probablePrime( ( numBits / 2 ), sRand );
		BigInteger q = BigInteger.probablePrime( ( numBits / 2 ), sRand );
		
		BigInteger n = p.multiply( q );
		BigInteger phi = ( p.subtract( BigInteger.ONE ) ).multiply( q.subtract( BigInteger.ONE ) );
		
		BigInteger publicKey = new BigInteger( "65537" );
		
		if( publicKey.gcd( phi ).compareTo( BigInteger.ONE ) != 0 )
			System.out.println( "Scanning for eligible encryption key. This may take a while." );
		
		while( publicKey.gcd( phi ).compareTo( BigInteger.ONE ) != 0 ){
			
			publicKey = publicKey.add( BigInteger.ONE );
			
			if( publicKey.compareTo( phi ) == 1 ){
				System.out.println( "No eligible encryption key found. Please try again." );
				return;
			}
			
		}
		
		BigInteger privateKey = publicKey.modInverse( phi );
		
		System.out.println( "\n[KEYS]\n" );
		System.out.println( "P  = " + p );
		System.out.println( "Q  = " + q );
		System.out.println( "N  = " + n );
		System.out.println( "\nPU = " + publicKey );
		System.out.println( "PR = " + privateKey + "\n" );
		
	}

	public static void setNumBits(){
		
		Scanner sysIn = new Scanner( System.in );
		boolean validBitNum = false;
		int entry = numBits;
		
		do {
			
			try {
				
				System.out.print( "Enter desired number of bits (default 1024): " );
				entry = Integer.parseInt( sysIn.nextLine() );
				
				if( entry < 32 )
					throw new Exception();
					
				validBitNum = true;
				
			}catch( Exception e ){
				System.out.println( "Error: Invalid Entry. Please enter a valid integer bit amount >= 32.\n" );
			}
			
		}while( !validBitNum );
		
		numBits = entry;
		System.out.println( "\nSuccess! Bit strength adjusted.\n" );
		
	}

}


 
Java
DECEMBER 16 2010
Download
97 DOWNLOADS