-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathceaser.java
More file actions
58 lines (55 loc) · 1.73 KB
/
ceaser.java
File metadata and controls
58 lines (55 loc) · 1.73 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
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class ceaser {
public static void main(String[] args) throws java.lang.Exception {
System.out.println("Enter a message for Encryption:");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine().toLowerCase();
System.out.println("Enter the key:");
int key = sc.nextInt() % 26;
sc.nextLine();
// String input = sc.nextLine().toLowerCase();
// if (input.charAt(0) == 'e') {
System.out.println("Encrypted text: "+encryption(s, key));
// } else {
// System.out.println("Decrypted text: "+decryption(s, key));
System.out.println("19DCE111:- Ravi Patel");
// }
}
public static String encryption(String s, int key) {
int n = s.length();
String ans = "";
for (int i = 0; i < n; i++) {
char a = s.charAt(i);
if ((int) a == 32) {
ans += a;
continue;
}
int last = ((((int) a + key) - 97) % 26) + 97;
a = (char) (last);
ans = ans + a;
}
return ans;
}
public static String decryption(String s, int key) {
int n = s.length();
String ans = "";
for (int i = 0; i < n; i++) {
char a = s.charAt(i);
if ((int) a == 32) {
ans += a;
continue;
}
int last = (((int) a - key) - 97);
if (last < 0) {
last += 26;
}
last += 97;
a = (char) (last);
ans = ans + a;
}
return ans;
}
}