All pastes #739518 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-17 06:56 UTC
rendered paste body
import java.lang.Exception;
import java.io.IOException;
import java.io.PrintStream;

class RSA{

// Extended Euclid Alogorithm to compute Greatest Common Divisor
public static long GCD(long x, long y) 
{	long u  = x, v  = y,
		 a  = 1, b  = 0,
		 at = 0, bt = 1;
				
	while (v != 0)
	{	long q = (u / v);

		long uTemp = u, vTemp = v, aTemp = a, bTemp = b, atTemp = at, btTemp = bt;
		
		u  = vTemp;		v  = uTemp - q * vTemp;
		a  = atTemp;	b  = btTemp;
		at = aTemp - atTemp * q;
		bt = bTemp - btTemp * q;
	}

	//return a;  // if GCD(e, phi_n);
	return b;    // if GCD(phi_n, e);
}  // end GCD

public static void main (String args[])  throws java.lang.Exception
{	long p=0, q=0, e=0, d, n, phi_n;
	try
	{	
		p = Long.parseLong(args[0]);
		q = Long.parseLong(args[1]);
		e = Long.parseLong(args[2]);
	}
	catch(ArrayIndexOutOfBoundsException aE)
	{	System.out.println("Thrown to indicate that an array has been accessed with an illegal index." + aE.getMessage());
	}
	
	n = p * q;
	phi_n = (p - 1) * (q - 1);
	d = GCD(phi_n, e);    // if GCD returns a
	//d = GCD(e, phi_n);  // if GCD returns b
	
	// 0 < d < n
	while( d < 0)
	{	d += phi_n; }
	
	while( d > n)
	{	d -= phi_n; }
	
	/*
	System.out.println("p = " + p);
	System.out.println("q = " + q);
	System.out.println("e = " + e);
	System.out.println("d = " + d);
	System.out.println("n = " + n);
	System.out.println("phi(n) = " + phi_n);
	*/
	
	System.out.println(n);
	System.out.println(e);
	System.out.println(d);
	
} // end main

}// end class RSA