-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAdecrypt.java
More file actions
executable file
·51 lines (47 loc) · 1.69 KB
/
RSAdecrypt.java
File metadata and controls
executable file
·51 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
import java.math.BigInteger;
public class RSAdecrypt
{
public static String decryptMessage(Message messageIn){
BigInteger power = messageIn.getB();
BigInteger modulo = new BigInteger(messageIn.getN());
BigInteger a;
BigInteger b;
//message is the final decrypted message
String message = "";
//subIntMessage is the sub message in integer form
String subIntMessage = "";
//fullIntMessage is all subIntMessages combined
String fullIntMessage = "";
//goes through all submessages in nums
ArrayList<String> nums = messageIn.getListOfNums();
for(int i=0; i<nums.size(); i++){
//gets first sub message block
a = new BigInteger(nums.get(i));
b = a;
//Puts the sub message block to the power power
for(BigInteger j= BigInteger.ONE; j.compareTo(power) < 0; j = j.add(BigInteger.ONE)){
a = (a.multiply(b)).mod(modulo);
}
//Puts the BitInteger into a string
subIntMessage = a.toString();
//Adds back any zeroes that were left on converting from string
for(int k=0; k<nums.get(i).length() - String.valueOf(a).length(); k++){
subIntMessage = "0" + subIntMessage;
}
//Adds on the submessage to the full message of integers
fullIntMessage = fullIntMessage + subIntMessage;
}
//Changes the integer message into the alphabet
for(int l=0; l<fullIntMessage.length()/2; l++){
//for normal characters
if(!fullIntMessage.substring(l*2,l*2+2).equals("26")){
message=message + Character.toString((char) (Integer.parseInt(fullIntMessage.substring(l*2,l*2+2))+65));
//for spaces
}else{
message=message + " ";
}
}
return message;
}
}