Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
57 lines (57 loc) · 2.31 KB
/
Copy pathMain.java
File metadata and controls
57 lines (57 loc) · 2.31 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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter input filename: ");
StringBuilder f_addr=new StringBuilder();
f_addr.append("./files/");
String f_name=br.readLine();
f_addr.append(f_name);
String path=f_addr.toString();
File f;
f=new File(path);
boolean f_exists=f.exists();
if (!f_exists) {
System.out.println("This file does not exist. FILE CREATION begins....");
randomHexInput(path);
f=new File(path);
}
else System.out.println("File found!");
br=new BufferedReader(new FileReader(path));
int numInputs=0;
while (true) {
String plaintext=br.readLine();
if (plaintext==null) break;
numInputs++;
System.out.println("----INPUT "+numInputs+" processing begins----");
AES obj = new AES(128);
if (plaintext.length()>32) plaintext=plaintext.substring(0,32);
else if (plaintext.length()==0) break;
String ciphertext=obj.encrypt(plaintext);
String retVal=obj.decrypt(ciphertext);
obj.displayRounds();
System.out.println("----INPUT "+numInputs+" processing ends----");
System.out.println("");
}
System.out.println("############## END ##############");
}
public static void randomHexInput(String s) throws FileNotFoundException, UnsupportedEncodingException {
PrintWriter writer = new PrintWriter(s, "UTF-8");
Random r=new Random();
int num1=r.nextInt(10)+3;
System.out.println("Generating new input file with random text...");
for (int i=0;i<num1;i++) {
StringBuilder strb=new StringBuilder();
for (int j=0;j<16;j++) {
int num2= r.nextInt(256);
String str=Integer.toHexString(num2);
if (str.length()==1) strb.append("0");
strb.append(str);
}
String req=strb.toString().toUpperCase();
writer.println(req);
}
writer.close();
}
}