-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt.java
More file actions
67 lines (45 loc) · 1.22 KB
/
Copy pathEncrypt.java
File metadata and controls
67 lines (45 loc) · 1.22 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
59
60
61
62
63
64
65
66
67
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.LinkedList;
public class Encrypt {
private String message ;
private int n;
//constructor
public Encrypt(String message, int n) {
this.message = message;
this.n = n;
}
public String encryptText() {
String lower = "abcedefghijklmnopqrstuvwxyz";
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if ( n > lower.length()) {
n = n % lower.length();
}
else if (n == lower.length()) {
n = lower.length() - n;
}
String src = lower + upper;
String dst = lower.substring(n) + lower.substring(0, n) + upper.substring(n) + upper.substring(0, n);
String[] line1 = src.split("");
String[] line2 = dst.split("");
Map<String, String> cipher = new HashMap<>() ;
for (int i = 0; i < line2.length && i < line1.length; i++) {
cipher.put(line1[i], line2[i]);
}
List<String> output = new LinkedList<>();
for (int i = 0; i < message.length(); i++) {
if (cipher.containsKey(message.substring(i,i+1))) {
output.add(cipher.get(message.substring(i,i+1)));
}
else {
output.add(message.substring(i,i+1));
}
}
String s = "";
for (String c : output) {
s += c;
}
return s;
}
}