Repository navigation
Expand file tree
/
Copy pathCiphEnc.java
More file actions
46 lines (43 loc) · 1.51 KB
/
Copy pathCiphEnc.java
File metadata and controls
46 lines (43 loc) · 1.51 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
import java.util.Scanner;
public class CiphEnc {
public static String encrypt(String str, int k){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
if(ch >= 'a' && ch <= 'z'){
char enc = (char)('a' + (ch - 'a' + 3) % 26);
sb.append(enc);
}else if(ch >= 'A' && ch <= 'Z'){
char enc = (char)('A' + (ch - 'A' + 3) % 26);
sb.append(enc);
}else{
sb.append(ch);
}
}
return sb.toString();
}
public static String decrypt(String str, int k){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
if(ch >= 'a' && ch <= 'z'){
char enc = (char)('a' + (ch - 'a' + 3) % 26);
sb.append(enc);
}else if(ch >= 'A' && ch <= 'Z'){
char enc = (char)('A' + (ch - 'A' + 3) % 26);
sb.append(enc);
}else{
sb.append(ch);
}
}
return sb.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String Encrypted = encrypt(str, 3);
String Decrypted = decrypt(str,-3);
System.out.println("Encrypted String: " + Encrypted);
System.out.println("Decrypted String: " + Decrypted);
}
}