-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleByteXorCipher.java
More file actions
139 lines (114 loc) · 4.94 KB
/
SingleByteXorCipher.java
File metadata and controls
139 lines (114 loc) · 4.94 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
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
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import javax.xml.bind.DatatypeConverter;
/* Class illustrates two methods to break a repeated hex. Although both
* utilize the same brute force method, the final check to determine which
* string is correct uses keywords or character frequency. Also includes
* method for encoding a message into hexademical.
*/
public class SingleByteXorCipher {
public SingleByteXorCipher(){
}
/**
* Uses keyword to determine which string is correct
*
* @param hex the encrypted hexadecimal value
* @return the decrypted message
*/
public String[] BreakerKeyWord(String hex){
HexConverter converter = new HexConverter();
int key=0;
String decrypt="";
byte[] byteArray = converter.HexToByteArray(hex);
//keywords used to check for correct string
String keyWords[]= new String[]{"the", "be", "to", "of", "and", "a",
"in", "is", "that", "have", "I", "it", "for", "not", "on", "with",
"he", "as", "my", "you", "do", "at", "this", "but", "his", "by",
"from", "they", "we", "say", "her", "she", "or", "an", "will",
"my", "one", "all", "would", "there", "their", "what", "so",
"up", "out", "if", "about", "who", "get", "which", "go", "me",
"when", "make", "can", "like", "time", "no", "just",
"him", "know", "take", "people", "into", "year", "your",
"good", "some", "could", "them", "see", "other", "than",
"then", "now", "look", "only", "come", "its", "over",
"think", "also", "back", "after", "use", "two", "how",
"our", "work", "first", "well", "way", "even", "new",
"want", "because", "any", "these", "give", "day", "most",
"us"};
outerloop:
//iterates through the keys in byte value
for (key=0X20;key<0X7A;key++){
//checks if current decoded string contains keywords
for (String word : keyWords){
if(decrypt.indexOf(" "+word)>-1 || decrypt.indexOf(word +
" ")>-1){
key--;
break outerloop;
}
}
//
byte[] testByteArray= byteArray;
for(int i=0; i<byteArray.length;i++){
testByteArray[i]=(byte) (byteArray[i]^(byte)(key));
}
decrypt=converter.hexToString(
DatatypeConverter.printHexBinary(testByteArray));
//decodes byteArray using certain key
}
String answer[]=new String[] {decrypt, Integer.toString(key)};
return answer;
}
/**
* Uses character frequency to determine which string is correct.
*
* @param hex the hex value to be decrypted
* @return the decrypted string
*/
public String[] BreakerFrequency(String hex){
HexConverter converter = new HexConverter();
IsEnglish scorer = new IsEnglish();
Charset ascii = Charset.forName("US-ASCII");
int key=0; // store answer
String decrypt=""; // store decrypted message
byte[] byteArray = converter.HexToByteArray(hex);
String regx = "^[\\p{L} .'-]+$";
outerloop:
//iterates through the keys in byte value
for (key=0X20;key<0x7A;key++){
//checks if current decoded string scores with common letters
if(decrypt.matches(regx) && scorer.isValid(decrypt) &&
scorer.scoreThatString(decrypt) > 1){
key--;
break outerloop;
}
byte[] testByteArray= byteArray;
for(int i=0; i<byteArray.length;i++){
testByteArray[i]=(byte) (byteArray[i]^(byte)(key));
}
decrypt=converter.hexToString(
DatatypeConverter.printHexBinary(testByteArray));
//decodes byteArray using certain key
}
String answer[]=new String[] {decrypt, Integer.toString(key)};
return answer;
}
/**
* Converts an inputted message into a encrypted message
* Using single byte xor encryption
*
* @param message the message to be encrypted
* @param key the hex byte to xor by
* @return the encrypted string
*/
public String SingleByteXorMaker(int key,String message){
HexConverter converter = new HexConverter();
String hexedMessage=converter.StringToHex(message);
byte[] byteArray = converter.HexToByteArray(hexedMessage);
byte[] encryptedByteArray=byteArray;
for (int i=0;i<byteArray.length;i++){
encryptedByteArray[i]=(byte) (byteArray[i]^(byte)(key));
}
String answer=DatatypeConverter.printHexBinary(encryptedByteArray);
return answer;
}
}