A RSA System in Java
import java.util.Arrays;
int e;
int P; //factorial P
int Q; // factorial Q
int PQ; //multiplication of PQ
int PQ1; //multiplication of (P-1)(Q-1)
int numberMessage;
int theD;
int z;
int[] possibleE = new int[50];
void setE() {
for(int a = 0; a < 50; a++){
possibleE[a] = 2 + a;
}
}
public void draw() {
noLoop();
P = round(random(50))+ 508; //random number
Q = round(random(50))+ 518; //random number
setE();
//println(e);
PQ = P*Q;
PQ1 = (P-1)*(Q-1);
int[] factors = new int[1000];
int numberOfFactors = 0;
for(int zz = 2; zz < PQ1; zz++){
if(PQ1%zz == 0){
factors[0] = zz;
}
}
int currentEPosition = 0;
e = possibleE[currentEPosition];
for(int p = 0; p < factors.length; p++){
if(e == factors[p] && currentEPosition<50){
currentEPosition++;
e = possibleE[currentEPosition];
p = 0;
}
}
//share E and PQ
//Alice calculations
String message = "abc";
String charUp = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String charDown = " abcdefghijklmnopqrstuvwxyz";
char[] charactersUpper = charUp.toCharArray();
char[] charactersLower = charDown.toCharArray();
String[] numberTemp = split("00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26", ' ');
int[] numberCode = new int[27];
for (int i = 0; i < numberTemp.length; i++) {
numberCode[i] = Integer.parseInt(numberTemp[i]);
}
char[] charMessage = message.toCharArray();//split the message into individual characters
int[] messageCode = new int[message.length()];
for (int i = 0; i < charMessage.length; i++) { //convert text to a number
for (int j = 0; j < charactersUpper.length; j++) {
println(charactersUpper[j]);
if (charMessage[i] == charactersUpper[j] || charMessage[i] == charactersLower[j]) {
messageCode[i] = numberCode[j];
println(numberCode[j]);
}
}
}
String StringNumberMessage = Arrays.toString(messageCode);
StringNumberMessage = StringNumberMessage.replace(",", "");
StringNumberMessage = StringNumberMessage.replace(" ", "");
StringNumberMessage = StringNumberMessage.replace("[", "");
StringNumberMessage = StringNumberMessage.replace("]", "");
numberMessage = Integer.parseInt(StringNumberMessage);
println(numberMessage);
int codedMessage = encrypt();
println(codedMessage + " !!!!!");
//bob decrypt message
while ( (e*z)%PQ1 != 1) {
println((e*z)%PQ1);
z++;
}
theD = z;
println(theD + "BJHBJEB");
int uncodedMessage = decrypt(codedMessage);
println("d = " + theD + " PQ: " + PQ + "the message:" + codedMessage);
println("e = " + e + " PQ: " + PQ + "the message:" + numberMessage);
}
int encrypt() {
int b = round(numberMessage/2);
println(b);
int c = numberMessage%2;
println(c);
int d = round(pow(e, 2))%PQ;
println(d);
int v = round(pow(d, b));
println(v);
int f = (c*e);
println(f);
int a;
if (f != 0) {
a = f*v;
println("wee");
}
else {
a = v;
println("wherp");
}
println(e);
println(a);
println(PQ);
int g = a%PQ;
println(g);
return g;
}
int decrypt(int codedMessage) {
int b = round(theD/2);
println(b);
int c = theD%2;
int d = round(pow(codedMessage, 2))%PQ;
println(d);
int v = round(pow(d, b));
println(v);
int f = (c*codedMessage);
int a;
if (f != 0) {
a = f*v;
println("wee");
}
else {
a = v;
println("wherp");
}
int g = a%PQ;
println(g);
return g;
}