-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesarCipher.java
220 lines (170 loc) · 4.56 KB
/
caesarCipher.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//package programmingTools;
import java.util.Scanner;
import java.io.*;
public class caesarCipher
{
/* Function writes the excrypted message to an output file. */
public static void writeEncryptionToFile(String inputFileName, String outputFileName, int shift)
{
String contents = getFileContents(inputFileName);
String encryptedText = encryptString(contents, shift);
BufferedWriter writeFile;
try
{
writeFile = new BufferedWriter(new FileWriter(outputFileName));
writeFile.write(encryptedText);
writeFile.close();
}
catch (Exception ex)
{
System.out.println("\nAn error occured writing to the file!");
}
}
/* Function writes the decrypted message to an output file. */
public static void writeDecryptionToFile(String inputFileName, String outputFileName, int shift)
{
String contents = getFileContents(inputFileName);
String decryptedText = decryptString(contents, shift);
BufferedWriter writeFile;
try
{
writeFile = new BufferedWriter(new FileWriter(outputFileName));
writeFile.write(decryptedText);
writeFile.close();
}
catch (Exception ex)
{
System.out.println("\nAn error occured writing to the file!");
}
}
/* Function to open and read the file and storing the contents
in a String, and returing the string */
public static String getFileContents(String fileName)
{
Scanner readFile;
String contents = "";
try
{
readFile = new Scanner(new File(fileName));
contents = readFile.useDelimiter("\0").next(); // store contents in String.
readFile.close();
}
catch (FileNotFoundException ex)
{
System.out.println("\nCannot open file!");
}
return contents;
}
/* Function to encrypt and return a passed String */
public static String encryptString(String text, int shift)
{
char[] encryptedText = new char[text.length()];
int i = 0;
int j = 0;
// apply the character shift to each character
for (i = 0; i < text.length(); i++)
{
if (text.charAt(i) != ' ')
{
encryptedText[j] = determineNextCharInEncryption(text.charAt(i), shift);
}
else
encryptedText[j] = ' ';
j++;
}
// return the new encrypted string
return new String(encryptedText);
}
/* Function to decrypt and return a passed String */
public static String decryptString(String text, int shift)
{
char[] decryptedText = new char[text.length()];
int i = 0;
int j = 0;
// apply the character shift to character
for (i = 0; i < text.length(); i++)
{
if (text.charAt(i) != ' ')
{
decryptedText[j] = determineNextCharInDecryption(text.charAt(i), shift);
}
else
decryptedText[j] = ' ';
j++;
}
// return the new decrypted string
return new String(decryptedText);
}
/* Finds the next character, applying the shift,
* and calling the determineCase function. */
static char determineNextCharInEncryption(char ch, int shift)
{
int findCase = determineCase(ch);
int asciiVal = -1;
// lowercase
if (findCase == 1)
{
if ((int)ch + shift > 122)
{
asciiVal = 97 + (((int)ch + shift) - 122 - 1);
ch = (char)asciiVal;
}
else
ch = (char)(ch + shift);
}
// uppercase
else if (findCase == 0)
{
if ((int)ch + shift > 90)
{
asciiVal = 65 + (((int)ch + shift) - 90 - 1);
ch = (char)asciiVal;
}
else
ch = (char)(ch + shift);
}
// if character is undefined returns character.
return ch;
}
/* Finds the next character, applying the shift,
and calling the determineCase function. */
public static char determineNextCharInDecryption(char ch, int shift) {
int findCase = determineCase(ch);
int asciiVal = -1;
// lowercase
if (findCase == 1)
{
if ((int)ch - shift < 97)
{
asciiVal = 122 + (((int)ch - shift) - 97 + 1);
ch = (char)asciiVal;
}
else
ch = (char)(ch - shift);
}
// uppercase
else if (findCase == 0)
{
if ((int)ch - shift < 65)
{
asciiVal = 90 + (((int)ch - shift) - 65 + 1);
ch = (char)asciiVal;
}
else
ch = (char)(ch - shift);
}
// if character is undefined returns character.
return ch;
}
/* Function to determine the case of character
using ascii values. */
public static int determineCase(char ch)
{
if ((int)ch >= 97 && (int)ch <= 122)
return 1; // lowercase return.
else if ((int)ch >= 65 && (int)ch <= 90)
return 0; // uppercase return.
// default invalid case
return -1;
}
}