-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathChaocipher.java
83 lines (71 loc) · 1.97 KB
/
Chaocipher.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
83
package cryptography.ciphers.chaocipher;
import java.util.LinkedList;
import cryptography.Mode;
public class Chaocipher {
public static void main(String[] args) {
}
public static String chaocipher(String input, Mode mode, String alpha1, String alpha2) {
String output = "";
input = input.replaceAll(" ", "");
LinkedList<Character> cipher, plain;
cipher = new LinkedList<>();
plain = new LinkedList<>();
for (int i = 0; i < alpha1.length(); i++) {
cipher.addLast(alpha1.charAt(i));
plain.addLast(alpha2.charAt(i));
}
if (mode == Mode.ENCRYPT) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char plainChar = input.charAt(i);
int a = plain.indexOf(plainChar);
char cipherChar = cipher.get(a);
sb.append(cipherChar);
int ind = cipher.indexOf(cipherChar);
for (int e = 0; e < ind; e++) {
char ch = cipher.remove(0);
cipher.addLast(ch);
}
char ch = cipher.remove(1);
cipher.add(13, ch);
ind = plain.indexOf(plainChar);
for (int e = 0; e < ind; e++) {
ch = plain.remove(0);
plain.addLast(ch);
}
ch = plain.remove(0);
plain.addLast(ch);
ch = plain.remove(2);
plain.add(13, ch);
}
output = sb.toString();
}
if (mode == Mode.DECRYPT) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char cipherChar = input.charAt(i);
int a = cipher.indexOf(cipherChar);
char plainChar = plain.get(a);
sb.append(plainChar);
int ind = cipher.indexOf(cipherChar);
for (int d = 0; d < ind; d++) {
char ch = cipher.remove(0);
cipher.addLast(ch);
}
char ch = cipher.remove(1);
cipher.add(13, ch);
ind = plain.indexOf(plainChar);
for (int d = 0; d < ind; d++) {
ch = plain.remove(0);
plain.addLast(ch);
}
ch = plain.remove(0);
plain.addLast(ch);
ch = plain.remove(2);
plain.add(13, ch);
}
output = sb.toString();
}
return output;
}
}