-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenere.java
More file actions
57 lines (46 loc) · 1.57 KB
/
Copy pathVigenere.java
File metadata and controls
57 lines (46 loc) · 1.57 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
import java.util.Scanner;
public class Vigenere {
public static String encode (String input, String keyword) {
char temp;
String encryptedText = "";
for (int i = 0, j =0; i<input.length(); i++) {
temp = (char) ('A' + (input.charAt(i)+keyword.charAt(j))%26);
System.out.println(input.charAt(i) + " + " + keyword.charAt(j) + " = " + temp);
j = ++j % keyword.length();
encryptedText += temp;
}
return encryptedText;
}
public static String decode (String input, String keyword) {
char temp;
String encryptedText = "";
for (int i = 0, j =0; i<input.length(); i++) {
temp = (char) ('A' + (input.charAt(i)- keyword.charAt(j) + 26)%26);
System.out.println(input.charAt(i) + " + " + keyword.charAt(j) + " = " + temp);
j = ++j % keyword.length();
encryptedText += temp;
}
return encryptedText;
}
public static void main (String[] args) {
System.out.println("Enter your sequence");
String input = "";
String keyword = "";
String option ="";
Scanner scanner = new Scanner (System.in);
input = scanner.nextLine();
input = input.trim();
System.out.println("Enter your keyword");
keyword =scanner.nextLine();
keyword = keyword.trim();
System.out.println("Enter Option");
option = scanner.nextLine();
if (option.trim().toLowerCase().equals("encode")) {
System.out.println(encode(input, keyword));
} else if (option.trim().toLowerCase().equals("decode")) {
System.out.println(decode(input, keyword));
} else {
System.out.println("Unknown Option");
}
}
}