-
Notifications
You must be signed in to change notification settings - Fork 9
/
Cypher.java
82 lines (74 loc) · 2.7 KB
/
Cypher.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.Map;
import java.util.HashMap;
/**
*
* @author joeolaoye
* @author uk
*/
@SuppressWarnings("unchecked")
public class Cypher {
public static Map<String, String> lookupTable = new HashMap(26);
static {
lookupTable.put("a", "Q");
lookupTable.put("b", "W");
lookupTable.put("c", "E");
lookupTable.put("d", "R");
lookupTable.put("e", "T");
lookupTable.put("f", "Y");
lookupTable.put("g", "U");
lookupTable.put("h", "I");
lookupTable.put("i", "O");
lookupTable.put("j", "P");
lookupTable.put("k", "A");
lookupTable.put("l", "S");
lookupTable.put("m", "D");
lookupTable.put("n", "F");
lookupTable.put("o", "G");
lookupTable.put("p", "H");
lookupTable.put("q", "J");
lookupTable.put("r", "K");
lookupTable.put("s", "L");
lookupTable.put("t", "Z");
lookupTable.put("u", "X");
lookupTable.put("v", "C");
lookupTable.put("w", "V");
lookupTable.put("x", "B");
lookupTable.put("y", "N");
lookupTable.put("z", "M");
}
public static String doEncryption(String s) {
//try to remove some special characters from the string as this cypher is not equipped for symbols
s = s.replaceAll("[-+.^@_:,]", "");
return Arrays.asList(s.split(""))
.stream()
// Won't return * so long as you speak the English alphabet
.map(c -> lookupTable.getOrDefault(c, "*").toString())
.collect(Collectors.joining(""));
}
public static String doDecryption(String s) {
return Arrays.asList(s.split(""))
.stream()
.map(c -> {
Map.Entry<String, String> tableEntry = lookupTable.entrySet()
.stream()
.filter((Map.Entry entry) -> c.equals(entry.getValue()))
.findFirst()
// Quite unlikely, but we shouldn't let the code go up in flames if a character is not in our character set
.orElse(null);
return tableEntry == null? "*" : tableEntry.getKey();
})
.collect(Collectors.joining(""));
}
public static void main(String... rags) {
String str = "abc",
enc = doEncryption(str),
dec = doDecryption(enc);
System.out.println(String.format("str: %s, enc: %s, dec: %s", str, enc, dec));
str = "test";
enc = doEncryption(str);
dec = doDecryption(enc);
System.out.println(String.format("str: %s, enc: %s, dec: %s", str, enc, dec));
}
}