-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateKey.java
114 lines (78 loc) · 2.1 KB
/
calculateKey.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
public class calculateKey {
static StringBuilder sb = new StringBuilder();
// Hint given: first letter is 'A'
// Hence performing the XOR operation between the first character and 'A'
// gives the first element of the key
public static String getKey(String binary1) {
sb = new StringBuilder();
String key = "";
String str = "";
int n = 0;
int intValue = (int) 'A';
String binary2 = Integer.toBinaryString(intValue);
if (binary2.length() < 8) {
n = 8 - binary2.length();
str = binary2;
if (n == 1) {
binary2 = "0" + str;
} else if (n == 2) {
binary2 = "00" + str;
} else if (n == 3) {
binary2 = "000" + str;
} else if (n == 4) {
binary2 = "0000" + str;
}
}
for (int j = 0; j < binary1.length(); j++) {
sb.append(calculateChar(calculateBit(binary1.charAt(j)) ^
calculateBit(binary2.charAt(j))));
}
key = sb.toString();
return key;
}
// Method to calculate key for other characters
public static String getKey(String binary1, char c) {
sb = new StringBuilder();
String key = "";
String str = "";
int n = 0;
int intValue = (int) c;
String binary2 = Integer.toBinaryString(intValue);
if (binary2.length() < 8) {
n = 8 - binary2.length();
str = binary2;
if (n == 1) {
binary2 = "0" + str;
} else if (n == 2) {
binary2 = "00" + str;
} else if (n == 3) {
binary2 = "000" + str;
} else if (n == 4) {
binary2 = "0000" + str;
}
}
for (int j = 0; j < binary1.length(); j++) {
sb.append(calculateChar(calculateBit(binary1.charAt(j)) ^
calculateBit(binary2.charAt(j))));
}
key = sb.toString();
return key;
}
// Calculating the bit-by-bit value of the XOR operation
private static boolean calculateBit(char input) {
boolean result = false;
if (input == '1') {
result = true;
}
return result;
}
private static char calculateChar(boolean input) {
char result = '\0';
if (input) {
result = '1';
} else {
result = '0';
}
return result;
}
}