-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
36 lines (29 loc) · 1.16 KB
/
CaesarCipher.java
File metadata and controls
36 lines (29 loc) · 1.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
import java.util.Scanner;
public class CaesarCipher {
public static StringBuffer cipher(String text, int s, boolean encrypt) {
StringBuffer result = new StringBuffer();
s = encrypt ? s : 26 - s;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (Character.isUpperCase(ch)) {
ch = (char) ((ch + s - 65) % 26 + 65);
} else if (Character.isLowerCase(ch)) {
ch = (char) ((ch + s - 97) % 26 + 97);
}
result.append(ch);
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String: ");
String string = sc.nextLine();
System.out.println("Enter key (Shift): ");
int shift = sc.nextInt();
System.out.println("Do you want to (E)ncrypt or (D)ecrypt? ");
char choice = sc.next().toUpperCase().charAt(0);
sc.close();
boolean encrypt = (choice == 'E');
System.out.println((encrypt ? "Encrypted" : "Decrypted") + " message: " + cipher(string, shift, encrypt));
}
}