-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenere.java
More file actions
77 lines (73 loc) · 2.64 KB
/
Vigenere.java
File metadata and controls
77 lines (73 loc) · 2.64 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
import java.util.Scanner;
public class Vigenere {
public static char alphabets[][] = new char[26][26];
//To create the vigenere matrix
public static void create_matrix(){
for(int i=0; i<26; i++){
for(int j=0; j<26; j++){
int val = j+i+97;
if(val>122)
val = 96 + val - 122;
alphabets[i][j] = (char)(val);
System.out.print(alphabets[i][j] + " ");
}
System.out.println();
}
}
public static String encrypt(String message, String key){
String encryption = "";
for(int i=0; i<message.length(); i++){
if(message.charAt(i) != ' '){
int val2 = (int)message.charAt(i) - 97;
int val1 = (int)key.charAt(i) - 97;
encryption += String.valueOf(alphabets[val1][val2]);
}
else{
encryption += " ";
}
}
return encryption;
}
public static String decrypt(String message, String key) {
String decryption = "";
int j;
for(int i=0; i<message.length(); i++){
if(message.charAt(i) != ' '){
int val1 = (int)key.charAt(i) - 97;
for(j=0; j<26; j++){
if(alphabets[val1][j] == message.charAt(i))
break;
}
decryption += String.valueOf((char)(j+97));
}
else
decryption += " ";
}
return decryption;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
create_matrix();
System.out.println("Enter the message: ");
String message = sc.nextLine();
System.out.println("Enter the key phrase: ");
String key = sc.nextLine();
int counter = 0;
int key_length = key.length();
int message_length = message.length();
//Making key length equal to message length
if(key_length<message_length){
while(key.length() != message.length()){
key += key.charAt(counter);
counter++;
if(counter == key_length)
counter = 0;
}
}
System.out.println("Changed key: "+key);
String encrypted_message = encrypt(message,key);
System.out.println("The encrypted message is: "+encrypted_message);
String decrypted_message = decrypt(encrypted_message,key);
System.out.println("The decrypted message is: "+decrypted_message);
}
}