-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvigenereCipher.java
More file actions
69 lines (54 loc) · 2.16 KB
/
vigenereCipher.java
File metadata and controls
69 lines (54 loc) · 2.16 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
import java.util.*;
public class vigenereCipher {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("1. Encrypt");
System.out.println("2. Decrypt");
System.out.println("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine();
if ( choice == 1){
System.out.println("Enter the string to be encrypted: ");
char[] p = sc.next().toCharArray();
for ( int i = 0 ; i < p.length ; i++){
p[i] = Character.toUpperCase(p[i]);
}
System.out.println("Enter the key : ");
char[] key = sc.next().toCharArray();
for( int i = 0 ; i < key.length ; i++){
key[i] = Character.toUpperCase(key[i]);
}
char[] c = new char[p.length];
for (int i = 0 ; i < c.length ; i++){
c[i] = (char)(((p[i]-'A') + (key[i%key.length] -'A'))%26 + 'A');
}
System.out.println("The encrypted string is: ");
for (int i = 0 ; i < c.length ; i++){
System.out.print(c[i]);
}
System.out.println();
}else{
System.out.println("Enter the string to be decrypted: ");
char[] c = sc.next().toCharArray();
for ( int i = 0 ; i < c.length ; i++){
c[i] = Character.toUpperCase(c[i]);
}
System.out.println("Enter the key : ");
char[] key = sc.next().toCharArray();
for( int i = 0 ; i < key.length ; i++){
key[i] = Character.toUpperCase(key[i]);
}
char[] p = new char[c.length];
for (int i = 0 ; i < c.length ; i++){
p[i] = (char)(((c[i]-'A') - (key[i%key.length] -'A') + 26)%26 + 'A');
// System.out.println(i);
}
System.out.println("The decrypted string is: ");
for (int i = 0 ; i < p.length ; i++){
System.out.print(p[i]);
}
System.out.println();
}
sc.close();
}
}