-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenereCipher.java
176 lines (133 loc) · 4.02 KB
/
vigenereCipher.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
// package programmingTools;
import java.util.Scanner;
import java.io.*;
public class vigenereCipher
{
static char [][] CharMatrix=new char[26][26];
static char characters[]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z'};
/* Function writes the excrypted message to an output file. */
public static void writeEncryptionToFile(String inputFileName, String outputFileName, String key)
{
String contents = getFileContents(inputFileName);
String encryptedText = encryptString(contents, key);
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, String key)
{
String contents = getFileContents(inputFileName);
String decryptedText = decryptString(contents, key);
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, String key)
{
String cyphir =null;
int sub1=0,sub2=0,sub3=0;
int sub=0;
int start=0;
for(int i=0; i<text.length(); i++)
{
sub1=text.substring(i,i+1).codePointAt(0);
sub2=key.substring(start,start+1).codePointAt(0);
start++;
sub3=sub1+sub2;
sub3=sub3%97;
sub=sub3%26;
if(cyphir==null)
cyphir="" + characters[sub];
else
cyphir+=characters[sub];
if(start==key.length())
{
start=0;
}
}
// return the new encrypted string
return new String(cyphir);
}
/* Function to decrypt and return a passed String */
public static String decryptString(String text, String key)
{
String dcyphir = null;
int sub1=0,sub2=0,sub3=0;
int keyword_len;
int sub=0;
int cyphir_len;
char char_num;
int start=0;
for(int i=0; i<text.length(); i++) {
sub1=text.substring(i,i+1).codePointAt(0);
sub2=key.substring(start,start+1).codePointAt(0);
start++;
sub1=(sub1%97)%26;
sub2=(sub2%97)%26;
for(int k=0; k<26; k++) {
sub3=(Integer.parseInt(""+(short)CharMatrix[sub2][k])%97)%26;
if(sub1==sub3){
if(dcyphir==null)
dcyphir=Character.toString(CharMatrix[0][k]);
else
dcyphir+=Character.toString(CharMatrix[0][k]);
}
}
if(start==key.length()){
start=0;
}
}
System.out.println(dcyphir);
return new String(dcyphir);
}
public void FillCharacMatrix()
{
for(int i=0; i<=25; i++)
{
for(int j=0; j<=25; j++)
{
CharMatrix[i][j]=characters[(j+i)%26];
}
}
for(int i=0;i<=25;i++)
{
System.out.print(" | ");
for(int j=0;j<=25;j++)
{
System.out.print(" "+CharMatrix[i][j]+" ");
}
System.out.println(" | ");
}
}
}