-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecryptMessage.java
More file actions
179 lines (119 loc) · 5.83 KB
/
DecryptMessage.java
File metadata and controls
179 lines (119 loc) · 5.83 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DecryptMessage {
public static void main(String[] args) {
// Welcome Print
System.out.println("----------------------------------------");
System.out.println(" Message Decrypter");
System.out.println("----------------------------------------");
// Wait again for 2 Seconds
try {
Thread.sleep(2000); // wait for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
// Create Scanner
Scanner Scanner1 = new Scanner(System.in);
// Choose Subfolder inside the Project to look for existing files
File ordner = new File(System.getProperty("user.dir") + "/Files");
// List the files inside that folder
File[] listFiles = ordner.listFiles();
// Print Available Files, add position of each file in front for visibility
int i = 1;
System.out.println("Your existing files are: ");
for (File file : listFiles) {
System.out.print(i + ") ");
System.out.println(file.getName());
i++;
}
// Choose Private Key File
System.out.print("Enter the name of the private key you want to use for decryption: ");
String privateKeyFile = Scanner1.nextLine();
// Choose File to be decrypted
System.out.print("Enter the name of the file you want to decrypt: ");
String encryptedMessage = Scanner1.nextLine();
// Choose new File Name for decrypted message
System.out.print("What name should the decrypted file receive: ");
String decryptedFilename = Scanner1.nextLine();
// Choose Private Key File in File Folder based on user input
Path filePathPrivKey = Paths.get(ordner.getPath(), privateKeyFile);
// Need to declare this outside the try/catch so I could use it afterwards
String contentPrivKey = null;
// Read Content of Private Key File // Throw Error if File doesnt exist
try {
contentPrivKey = Files.readString(filePathPrivKey);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
// Extract numbers from the string
String[] lines = contentPrivKey.split("\n");
//Define Lines of File as Variables
String nLine = lines[0];
String dLine = lines[1];
// Get the numbers between { and } for both Lines
String nValue = nLine.substring(nLine.indexOf("{") + 1, nLine.indexOf("}"));
String dValue = dLine.substring(dLine.indexOf("{") + 1, dLine.indexOf("}"));
// Convert Numbers (String) to BigIntegers
BigInteger bigN = new BigInteger(nValue);
BigInteger bigD = new BigInteger(dValue);
// Choose Encrypted Message File in File Folder based on user input
Path filePathMessage = Paths.get(ordner.getPath(), encryptedMessage);
// Read Content of Encrypted Message File
try {
encryptedMessage = Files.readString(filePathMessage);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
// Convert String into BigInteger
BigInteger ciphertextBig = new BigInteger(encryptedMessage);
//Decrypt Ciphertext into Decimal via RSA Decryption Formula
BigInteger decryptedMessageDecimal = ciphertextBig.modPow(bigD, bigN);
//Convert Decimal into Binary
String decryptedMessageBinary = decryptedMessageDecimal.toString(2);
// Pad length to multiple of 8 by adding zeros to the left if needed
// This is important so the split in the next step has the right order
int remainder = decryptedMessageBinary.length() % 8;
if (remainder != 0) {
int padding = 8 - remainder;
decryptedMessageBinary = "0".repeat(padding) + decryptedMessageBinary;
}
// Split into 8-bit groups (left to right)
List<String> groups = new ArrayList<>();
for (int l = 0; l < decryptedMessageBinary.length(); l += 8) {
groups.add(decryptedMessageBinary.substring(l, l + 8));
}
// Convert each 8-bit group to its ASCII character
StringBuilder asciiString = new StringBuilder();
for (String group : groups) {
int decimalValue = Integer.parseInt(group, 2); // binary -> decimal
asciiString.append((char) decimalValue); // decimal -> ASCII char
}
// Create String containing decrypted Message
String decryptedMessage = asciiString.toString();
// Print decrypted message
System.out.println("Your decrypted message says: " + decryptedMessage);
// Create File containing decrypted Message using Filename based on user input
try (FileWriter writerMessage = new FileWriter(new File(ordner, decryptedFilename))) {
writerMessage.write(decryptedMessage);
}
catch (IOException ex) {
System.out.println("Error writing public key: " + ex.getMessage());
}
// Info Print
System.out.println("Yay! You decrypted your message! Message has been saved at " + ordner + "\\" + decryptedFilename);
// Wait again for 2 Seconds
try {
Thread.sleep(2000); // wait for 1 second
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}