-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathVigenereCipher.java
More file actions
162 lines (137 loc) · 5.23 KB
/
VigenereCipher.java
File metadata and controls
162 lines (137 loc) · 5.23 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
/*********
-*- Made by VoxelPixel
-*- For YouTube Tutorial
-*- https://github.com/VoxelPixel
-*- Support me on Patreon: https://www.patreon.com/voxelpixel
*********/
import java.util.Scanner;
public class VigenereCipher {
private static Scanner in;
private static String message;
private static String mappedKey;
public static void main(String[] args){
in = new Scanner(System.in);
System.out.print("1. Encryption\n2. Decryption\nChoose(1,2): ");
int choice = in.nextInt();
in.nextLine();
if(choice == 1){
System.out.println("---Encryption---");
msgAndKey();
cipherEncryption(message, mappedKey);
} else if(choice == 2){
System.out.println("---Decryption---");
msgAndKey();
cipherDecryption(message, mappedKey);
} else {
System.out.println("Incorrect Choice");
}
}
private static void cipherDecryption(String message, String mappedKey) {
int[][] table = createVigenereTable();
String decryptedText = "";
for (int i = 0; i < message.length(); i++) {
if(message.charAt(i) == (char)32 && mappedKey.charAt(i) == (char)32){
decryptedText += " ";
} else {
decryptedText += (char)(65 + itrCount((int)mappedKey.charAt(i), (int)message.charAt(i)));
}
}
System.out.println("Decrypted Text: " + decryptedText);
}
private static int itrCount(int key, int msg) {
// this function will return the count which it takes from key's letter to reach cipher letter
// and then this count will be used to calculate decryption of encrypted letter in message.
int counter = 0;
String result = "";
for (int i = 0; i < 26; i++) {
if(key+i > 90){
//90 being ASCII of Z
result += (char)(key+(i-26));
} else {
result += (char)(key+i);
}
}
//counting from key's letter to cipher letter in vigenere table
for (int i = 0; i < result.length(); i++) {
if(result.charAt(i) == msg){
break; // letter found
} else {
counter++;
}
}
return counter;
}
private static void cipherEncryption(String message, String mappedKey) {
int[][] table = createVigenereTable();
String encryptedText = "";
for (int i = 0; i < message.length(); i++) {
if(message.charAt(i) == (char)32 && mappedKey.charAt(i) == (char)32){
encryptedText += " ";
} else {
//accessing element at table[i][j] position to replace it with letter in message
encryptedText += (char)table[(int)message.charAt(i)-65][(int)mappedKey.charAt(i)-65];
}
}
System.out.println("Encrypted Text: " + encryptedText);
}
private static int[][] createVigenereTable() {
// creating 26x26 table containing alphabets
int[][] tableArr = new int[26][26];
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
int temp;
if((i+65)+j > 90){
temp = ((i+65)+j) -26;
tableArr[i][j] = temp;
} else {
temp = (i+65)+j;
tableArr[i][j] = temp;
}
}
}
//printing table to check if its correct
// for (int i = 0; i < 26; i++) {
// for (int j = 0; j < 26; j++) {
// System.out.print((char)tableArr[i][j] + " ");
// }
// System.out.println();
// }
return tableArr;
}
private static void msgAndKey() {
System.out.println("***Message and key should be alphabetic***");
//message input
System.out.print("Enter Message: ");
String msg = in.nextLine();
msg = msg.toUpperCase();
//key input
System.out.print("Enter Key: ");
String key = in.next();
in.nextLine();
key = key.toUpperCase();
//mapping key to message
String keyMap = "";
for (int i = 0, j = 0; i < msg.length(); i++) {
if(msg.charAt(i) == (char)32){
//ignoring space
keyMap += (char)32;
} else {
//mapping letters of key with message
if(j < key.length()){
keyMap += key.charAt(j);
j++;
} else {
//restarting the key from beginning once its length is complete
// and its still not mapped to message
j = 0;
keyMap += key.charAt(j);
j++; //without incrementing here, key's first letter will be mapped twice
}
} //if-else
} //for
message = msg;
mappedKey = keyMap;
// System.out.println("Message: " + message);
// System.out.println("key: " + mappedKey);
}
}