Repository navigation
Expand file tree
/
Copy pathDecrypt.java
More file actions
47 lines (39 loc) · 1.74 KB
/
Copy pathDecrypt.java
File metadata and controls
47 lines (39 loc) · 1.74 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
public class Decrypt implements Constants{
private int decryptKey(String encryptedLine){
StringBuilder binary = new StringBuilder();
int limit = Math.min(7, encryptedLine.length());
for(int i = 0; i < limit; i++){
if(encryptedLine.charAt(i) == Constants.PLACEHOLDER_CHAR){
for (int j = i + 1; j < encryptedLine.length(); j++){
String currentCharBinary = Integer.toBinaryString(encryptedLine.charAt(j));
binary.append(currentCharBinary.length() > 8 ? 1 : 0);
}
break;
}
String currentCharBinary = Integer.toBinaryString(encryptedLine.charAt(i));
binary.append(currentCharBinary.length() > 8 ? 1 : 0);
}
if(binary.length() > 7){
binary.delete(7, binary.length());
}
return Integer.parseInt(binary.toString(), 2);
}
public String decryptData(String encryptedLine){
int key = decryptKey(encryptedLine);
int placeHolderCharIndex = encryptedLine.indexOf(Constants.PLACEHOLDER_CHAR);
if(placeHolderCharIndex != -1){
encryptedLine = encryptedLine.substring(0, placeHolderCharIndex);
}
StringBuilder decryptedData = new StringBuilder();
for(int i = 0; i < encryptedLine.length(); i++){
String binary = Integer.toBinaryString(encryptedLine.charAt(i) - key);
if (binary.length() == 15) {
int character = Integer.parseInt(binary.substring(7), 2);
decryptedData.append((char) character);
}else{
decryptedData.append((char) (encryptedLine.charAt(i) - key));
}
}
return decryptedData.toString();
}
}