Skip to content

Instantly share code, notes, and snippets.

@obikag
Created August 30, 2013 01:31
Show Gist options
  • Save obikag/6385386 to your computer and use it in GitHub Desktop.
Save obikag/6385386 to your computer and use it in GitHub Desktop.
Public Key Generator using the RSA algorithm written in JAVA. See comments for more details.
package main;
import java.math.BigInteger;
/**
* Key class used to store the key's Component and Modulus
* @author obikag
* @since 2013-06-22
*/
public class Key {
private BigInteger component; //Component
private BigInteger modulus; //Modulus
/**
* Object Constructor
* @param component Component of Key
* @param modulus Modulus
*/
Key(BigInteger component, BigInteger modulus){
this.component = component;
this.modulus = modulus;
}
/**
* Method used to return the Component of the Key
* @return BigInteger value of key's Component
*/
public BigInteger getComponent(){
return component;
}
/**
* Method used to return the Modulus
* @return BigInteger value of Modulus
*/
public BigInteger getModulus(){
return modulus;
}
/**
* Prints to screen Key Information
*/
@Override
public String toString(){
return "Component: "+component+" / Modulus: "+modulus;
}
}
package main;
import java.math.*;
import java.security.SecureRandom;
/**
* RSA Generator class is used to calculate the private and public keys, based on a specified bit length.
* Encrypt and Decrypt Methods also defined below.
* @author obikag
* @since 2013-06-22
*/
public class RSAGenerator {
private Key publickey; //Public Key
private Key privatekey; //Private key
private static final BigInteger ONE = BigInteger.ONE; //Value of 1 expressed as a BigInteger for ease of calculation
/**
* Object Constructor
* @param numbits Bit Length used to generate the various components
*/
public RSAGenerator(int numbits){
//Generate p and q
BigInteger p = BigInteger.probablePrime(numbits, new SecureRandom());
BigInteger q = BigInteger.probablePrime(numbits, new SecureRandom());
//Compute n - modulus
BigInteger n = p.multiply(q);
//Compute Euler's totient function, phiN
BigInteger p_minus_one = p.subtract(ONE);
BigInteger q_minus_one = q.subtract(ONE);
BigInteger phiN = p_minus_one.multiply(q_minus_one);
//Calculate public exponent
BigInteger e, d;
do {
e = BigInteger.probablePrime(numbits, new SecureRandom());
} while ((e.compareTo(ONE) == 1) && (e.compareTo(phiN) == -1) && (e.gcd(phiN).compareTo(ONE) != 0));
//Calculate private exponent
d = e.modInverse(phiN);
//Set Keys
publickey = new Key(e,n);
privatekey = new Key(d,n);
}
/**
* Method used to encrypt a message string
* @param msg Message string to be encrypted
* @return BigInteger value of encrypted message
*/
public BigInteger encrypt(String msg){
return (new BigInteger(msg.getBytes())).modPow(publickey.getComponent(), publickey.getModulus());
}
/**
* Method used to decrypt a message
* @param encrypt_msg Encrypted message as a BigInteger
* @return BigInteger value of decrypted string
*/
public BigInteger decrypt(BigInteger encrypt_msg){
return encrypt_msg.modPow(privatekey.getComponent(), privatekey.getModulus());
}
/**
* Prints to screen Public and Private key components
*/
@Override
public String toString(){
return "Public Key -> "+publickey.toString()+
"\nPrivate Key -> "+privatekey.toString();
}
}
package test;
import java.math.BigInteger;
import main.*;
/**
* Test Section of RSA Generator
* @author obikag
* @since 2013-06-22
*/
public class RSATest {
public static void main(String[] args) throws Exception{
//Create a new RSA Generator Object of 64 bit length
RSAGenerator rsa_gen = new RSAGenerator(64);
//Create a message to encrypt
String msg = "This is a test!!";
//Encrypt message
String encrypted = rsa_gen.encrypt(msg).toString();
//Decrypt message
BigInteger decrypt = rsa_gen.decrypt(new BigInteger(encrypted));
String decrypted = "";
//BigInteger must be converted to a byte array in order to rebuild the original message
for(byte b: decrypt.toByteArray()){
decrypted += (char) b;
}
System.out.println("Original Message: "+msg);
System.out.println(rsa_gen.toString());
System.out.println("Encrypted: "+encrypted);
System.out.println("Decrypted: "+decrypted);
/*
* Example Output:
*
* Original Message: This is a test!!
* Public Key -> Component: 14493509104177907227 / Modulus: 112625708915384646043981538437387484617
* Private Key -> Component: 7249569340342942776596243374501560163 / Modulus: 112625708915384646043981538437387484617
* Encrypted: 79682373572381863577079732109682216626
* Decrypted: This is a test!!
*
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment