-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCryptoDemo.java
146 lines (135 loc) · 4.64 KB
/
CryptoDemo.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
abstract class Cryptography {
public abstract String encode(String plainText);
public abstract String decode(String cipherText);
}
class CeaserCipher extends Cryptography {
private int key;
CeaserCipher(int key){
this.key = key;
}
@Override
public String encode(String plainText){
String cipherText = "";
for(int i = 0; i < plainText.length(); i++){
char c = plainText.charAt(i);
if(!Character.isLetter(c)){
cipherText += c;
continue;
}
if(Character.isUpperCase(c)){
c += key;
if(c > 'Z'){
c -= 26;
}
} else {
c += key;
if(c > 'z'){
c -= 26;
}
}
cipherText += c;
}
return cipherText;
}
@Override
public String decode(String cipherText){
String plainText = "";
for(int i = 0; i < cipherText.length(); i++){
char c = cipherText.charAt(i);
if(!Character.isLetter(c)){
plainText += c;
continue;
}
if(Character.isUpperCase(c)){
c -= key;
if(c < 'A'){
c += 26;
}
} else {
c -= key;
if(c < 'a'){
c += 26;
}
}
plainText += c;
}
return plainText;
}
}
class TranspositionCipher extends Cryptography {
private int key;
TranspositionCipher(int key){
this.key = key;
}
@Override
public String encode(String plainText){
String cipherText = "";
String keyString = Integer.toString(key);
if(plainText.length() % keyString.length() != 0){
int padding = keyString.length() - plainText.length() % keyString.length();
for(int i = 0; i < padding; i++){
plainText += " ";
}
}
for(int i = 0;i < plainText.length() / keyString.length();i++) {
for(int j =0;j<keyString.length();j++) {
char ch = (String.valueOf(key)).charAt(j);
int index = Integer.parseInt(String.valueOf(ch)) - 1;
char c = plainText.charAt(i * keyString.length() + index);
cipherText += c;
}
}
return cipherText;
}
@Override
public String decode(String cipherText){
String plainText = "";
String keyString = Integer.toString(key);
char[] keyArray = keyString.toCharArray();
char[] decodeKeyArray = new char[keyArray.length];
for(int i = 0; i < keyArray.length; i++) {
decodeKeyArray[i] = (char)(i + '1');
}
for(int i = 0; i < keyArray.length - 1; i++) {
for(int j = 0; j < keyArray.length - i - 1; j++) {
if(keyArray[j] > keyArray[j + 1]) {
char temp = keyArray[j];
keyArray[j] = keyArray[j + 1];
keyArray[j + 1] = temp;
temp = decodeKeyArray[j];
decodeKeyArray[j] = decodeKeyArray[j + 1];
decodeKeyArray[j + 1] = temp;
}
}
}
String decodeKeyString = new String(decodeKeyArray);
if(cipherText.length() % decodeKeyString.length() != 0){
int padding = decodeKeyString.length() - cipherText.length() % decodeKeyString.length();
for(int i = 0; i < padding; i++){
cipherText += " ";
}
}
for(int i = 0;i < cipherText.length() / decodeKeyString.length();i++) {
for(int j =0;j<decodeKeyString.length();j++) {
char ch = (String.valueOf(decodeKeyString)).charAt(j);
int index = Integer.parseInt(String.valueOf(ch)) - 1;
char c = cipherText.charAt(i * decodeKeyString.length() + index);
plainText += c;
}
}
return plainText;
}
}
public class CryptoDemo {
public static void main(String[] args) {
Cryptography ceaserCipher = new CeaserCipher(3);
Cryptography transpositionCipher = new TranspositionCipher(312);
String plainText = "Hello World";
String cipherText = ceaserCipher.encode(plainText);
System.out.println(cipherText);
System.out.println(ceaserCipher.decode(cipherText));
cipherText = transpositionCipher.encode(plainText);
System.out.println(cipherText);
System.out.println(transpositionCipher.decode(cipherText));
}
}