-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwitterHacking.java
More file actions
80 lines (72 loc) · 2.73 KB
/
TwitterHacking.java
File metadata and controls
80 lines (72 loc) · 2.73 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
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
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TwitterHacking {
public static String decrypt(String encrypted_message) {
if (encrypted_message == null || encrypted_message.isEmpty()) {
return null;
}
int n = encrypted_message.length();
String endEncry = encrypted_message.substring(n - 18, n);
System.out.println(endEncry);
String endDecry = "Your friend, Alice";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < endEncry.length(); i++) {
char cDecry = endDecry.charAt(i);
char cEncry = endEncry.charAt(i);
if (Character.isAlphabetic(cDecry)) {
int k = Math.abs(cDecry - cEncry);
if (k > 10) {
k = 26 - k;
}
sb.append(k);
}
}
String key = findKey(sb.toString());
//String key = "8251220";
//System.out.println(key);
int keyLen = key.length();
int num = 0;
sb = new StringBuilder();
for (int i = n - 1; i >= 0; i--) {
char cEncry = encrypted_message.charAt(i);
if (Character.isAlphabetic(cEncry)) {
char cDecry = (char)(cEncry - (key.charAt(num % keyLen) - '0'));
if (Character.isLowerCase(cEncry) && cDecry < 'a'
|| Character.isUpperCase(cEncry) && cDecry < 'A') {
cDecry = (char)(cDecry + 26);
}
sb.append(cDecry);
num++;
} else {
sb.append(cEncry);
}
}
return sb.reverse().toString();
}
public static String findKey(String str) {
StringBuilder sb = new StringBuilder(str);
sb.reverse();
Pattern p = Pattern.compile("(.+?)\\1+");
Matcher m = p.matcher(sb.toString());
String repeated;
while (m.find()) {
repeated = m.group(1);
return repeated;
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String encrypted_message = "Otjfvknou kskgnl, K mbxg iurtsvcnb ksgq hoz atv. Vje xcxtyqrl vt ujg smewfv vrmcxvtg rwqr ju vhm ytsf elwepuqyez. -Atvt hrqgse, Cnikg";
//String encrypted_message = "Atvt hrqgse, Cnikg";
System.out.println(decrypt(encrypted_message));
// String str = "251220825122082";
// Pattern p = Pattern.compile("(.+?)\\1+");
// Matcher m = p.matcher(str);
// while (m.find()) {
// String repeated = m.group(1);
// System.out.println(repeated);
// }
}
}