-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathcipher.java
58 lines (45 loc) · 1.4 KB
/
cipher.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
import java.util.Scanner;
public class cipher {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("1 - Encryption");
System.out.println("2 - Decryption");
System.out.println("Please select an option : ");
String option = myObj.nextLine();
System.out.println("Please enter the key : ");
int key = myObj.nextInt();
myObj.nextLine();
System.out.println("Please enter the word to encrypt : ");
String text = myObj.nextLine();
switch (option.toLowerCase()){
case "1" : case "encrypt" :
encrypt(text,key);
break;
case "2" : case "decrypt" :
decrypt(text,key);
break;
}
}
public static void encrypt(String plainText,int shift){
char [] arr = plainText.toCharArray();
int length = plainText.length();
for (char c: arr ){
if(c=='z'){
c-=26;
}
c+=shift;
System.out.print(c);
}
}
public static void decrypt(String plainText,int shift){
char [] arr = plainText.toCharArray();
int length = plainText.length();
for (char c: arr ){
if(c=='z'){
c+=26;
}
c-=shift;
System.out.print(c);
}
}
}