Created
August 31, 2011 14:25
-
-
Save fb55/1183667 to your computer and use it in GitHub Desktop.
A simple class for Java generating RSA public/private-key pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RSAencrypt{ | |
static public double m; | |
static public double publicK; | |
static private double privateK; | |
static private double nums = 6; | |
static{ | |
double p = ranPrime.genRanPrime(nums); | |
double q = ranPrime.genRanPrime(nums); | |
RSAencrypt.m = p * q; | |
double phi = (p-1) * (q - 1); | |
RSAencrypt.publicK = ranPrime.genRanPrime(nums); | |
while(euklid.lil(RSAencrypt.publicK, phi) == 1){ | |
RSAencrypt.publicK = ranPrime.genRanPrime(nums); | |
} | |
RSAencrypt.privateK = Math.pow(RSAencrypt.publicK, -1) % phi; | |
} | |
public static void main (String[] args){ | |
System.out.println("Public Key: " + (int)RSAencrypt.publicK); | |
System.out.println("Class: " + (int)RSAencrypt.m); | |
} | |
} | |
class euklid{ | |
static public double lil(double a, double b){ | |
if(a*b == 0) return 0; | |
if(a > b) return euklid.lil(b,a); | |
if(b % a == 0) return a; | |
return lil(b % a, a); | |
} | |
} | |
class ranPrime{ | |
static double genRanPrime(double num){ | |
int i; | |
for(i = (int)(Math.random() * Math.pow(10,num)); !ranPrime.lilFermat(i); i++){ | |
} | |
return i; | |
} | |
static boolean lilFermat(double num){ | |
int a = (int)(Math.random() * 10)+1; | |
return (Math.pow(a, num)%num) == (a % num); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment