-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCipherIT.java
149 lines (130 loc) · 4.92 KB
/
CipherIT.java
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
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.*;
import Ciphers.DES_P.HexEntries;
import Ciphers.Substitution;
import Ciphers.Caesar;
public class CipherIT {
static int Algorithm = -7;
// DES = 1; Caesar = 2; Substution = 3
static int Cryption = -7;
// Encryption = 1; Decryption = 2
static int Input = -7;
// File = 1; Manual = 2
static String key = "default";
static String inputText;
static String filepath;
static String CryptedOutput;
static int keyValid = -7;
static String OutputFile = "./test/output.txt";
static void getAlgorithm(int a) {
Algorithm = a;
}
static void getCryption(int a) {
Cryption = a;
}
static void getInput(int a) {
Input = a;
}
static void getKey(String a) {
key = a;
}
static void getFile(String a) {
if(Input == 1) {
inputText = FileInput(a);
filepath = inputText;
} else if (Input == 2) {
inputText = a;
filepath = a;
}
}
public static void Execute() {
if (Cryption == 1) {
switch (Algorithm) {
case 1:
String text = HexEntries.asciiToHex(inputText);
String input16[] = split(text);
CryptedOutput = DES.Cryption(input16, 0);
System.out.println("The encrypted string (hexadecimal): " + CryptedOutput);
writeToFile(CryptedOutput);
break;
case 2:
// code for encrypting using Caesar cipher
int shift = Integer.parseInt(key);
CryptedOutput = Caesar.encrypted(inputText, shift);
System.out.println("The encrypted string : " + CryptedOutput);
writeToFile(CryptedOutput);
break;
case 3:
// code for encrypting using Substitution cipher
key = "No key required for substitution";
CryptedOutput = Substitution.encrypted(inputText);
System.out.println("The encrypted string : " + CryptedOutput);
writeToFile(CryptedOutput);
break;
}
} else {
switch (Algorithm) {
case 1:
String text = inputText;
String input16[] = split(text);
text = DES.Cryption(input16, 1, key);
CryptedOutput = HexEntries.hexToAscii(text);
System.out.println("\nThe decrypted string is : " + CryptedOutput);
writeToFile(CryptedOutput);
break;
case 2:
// code for decrypting using Substitution cipher
int shift = Integer.parseInt(key);
CryptedOutput = Caesar.decrypted(inputText, shift);
System.out.println("The decrypted string: " + CryptedOutput);
writeToFile(CryptedOutput);
break;
case 3:
// code for decrypting using Substitution cipher
key = "No key required for substitution";
CryptedOutput = Substitution.decrypted(inputText);
System.out.println("The decrypted string: " + CryptedOutput);
writeToFile(CryptedOutput);
break;
}
}
}
static void writeToFile(String str) {
try {
FileWriter myWriter = new FileWriter(OutputFile);
myWriter.write(str);
myWriter.close();
} catch (IOException e) {
System.out.println("An error occurred while writing output to file.");
e.printStackTrace();
}
}
static String[] split(String input) {
// input is hexadecimal
// splitting input into length of 16
String input16[] = input.split("(?<=\\G.{16})");
int n = input16.length;
if (input16[n - 1].length() < 16) {
input16[n - 1] = String.format("%-" + 16 + "s", input16[n - 1]).replace(' ', '0');
}
// System.out.println(Arrays.toString(input16));
return input16;
}
static String FileInput(String fileName) {
String str = "";
try {
FileReader input = new FileReader(fileName);
int i;
while ((i = input.read()) != -1)
str += (char) i;
input.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("\nThe input string is :" + str);
return str.replaceAll("\n", " ");
}
}