-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher2calculateKey.java
198 lines (139 loc) · 3.74 KB
/
cipher2calculateKey.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
public class cipher2calculateKey {
public static void main(String[] args) {
String cipherText = "ZVM]AZCH^kPPMWs\\sCsCM@AU[_LRM\\[UXUOYG\\"
+ "[YDBNUMkXZPEDY\\UXCM\\GBB_DWsTZUXBNUHYOPs\\Ous";
int[] arr=new int[cipherText.length()];
int n=0;
String str="";
//Converting ASCII values to decimal values
for(int i=0; i<cipherText.length();i++){
arr[i]=(int) cipherText.charAt(i);
}
String[] binary=new String[arr.length];
//Converting decimal values to binary strings
for(int i=0;i<arr.length;i++){
binary[i]=Integer.toBinaryString(arr[i]);
}
//Adjusting length of the binary strings
for(int i=0;i<binary.length;i++){
if(binary[i].length()<8){
n=8-binary[i].length();
str=binary[i];
if(n==1){
binary[i]="0"+str;
}
else if(n==2){
binary[i]="00"+str;
}
else if(n==3){
binary[i]="000"+str;
}
else if(n==4){
binary[i]="0000"+str;
}
str=new String();
}
}
StringBuilder sb = new StringBuilder();
String[] plaintext=new String[binary.length];
//Key value for 'P' in the first position in binary
// String key1=Integer.toBinaryString(10);
//Key value for 'S' in the first position in binary
String key1=Integer.toBinaryString(9);
n=0;
str=new String();
//Adjusting length of the binary strings
if(key1.length()<8){
n=8-key1.length();
str=key1;
if(n==1){
key1="0"+str;
}
else if(n==2){
key1="00"+str;
}
else if(n==3){
key1="000"+str;
}
else if(n==4){
key1="0000"+str;
}
str=new String();
}
n=0;
str=new String();
//Key value for 'G' in the second position in binary
String key2=Integer.toBinaryString(17);
//Adjusting length of the binary strings
if(key2.length()<8){
n=8-key2.length();
str=key2;
if(n==1){
key2="0"+str;
}
else if(n==2){
key2="00"+str;
}
else if(n==3){
key2="000"+str;
}
else if(n==4){
key2="0000"+str;
}
str=new String();
}
for(int i=0;i<binary.length;i++){
//Performs XOR with key1 for characters in the first position
//and key2 in the second position
if(i%2==0){
for (int j = 0; j < binary[i].length(); j++) {
sb.append(calculateChar(calculateBit(binary[i].charAt(j))
^ calculateBit(key1.charAt(j))));
}
plaintext[i] = sb.toString();
sb = new StringBuilder();
}
else{
for (int j = 0; j < binary[i].length(); j++) {
sb.append(calculateChar(calculateBit(binary[i].charAt(j))
^ calculateBit(key2.charAt(j))));
}
plaintext[i] = sb.toString();
sb = new StringBuilder();
}
}
//Converting binary strings to characters to get the Plaintext
for(int i=0;i<plaintext.length;i++){
arr[i] = Integer.parseInt(plaintext[i], 2);
}
char[] result=new char[arr.length];
for(int i=0;i<arr.length;i++){
result[i]=(char) arr[i];
}
//Prints the partially decrypted text
System.out.println("Partially decrypted text:");
for(int i=0;i<result.length;i++){
System.out.print(result[i]);
}
}
//Calculating the bit-by-bit value of the XOR operation
//Gives the boolean result of a^b
private static boolean calculateBit(char input) {
boolean result=false;
if(input=='1'){
result=true;
}
return result;
}
//Converts boolean to character value
private static char calculateChar(boolean input) {
char result='\0';
if(input){
result='1';
}
else{
result='0';
}
return result;
}
}