Skip to content

Instantly share code, notes, and snippets.

@fredlahde
Last active April 3, 2018 18:33
Show Gist options
  • Save fredlahde/6bdf90cff314b5ff18c282cf0cc4540f to your computer and use it in GitHub Desktop.
Save fredlahde/6bdf90cff314b5ff18c282cf0cc4540f to your computer and use it in GitHub Desktop.
Keystream parser
Hinweis IV:050607 bedeutet IV[0]=05,IV[1]=06,IV[2]=07
IV: 02 01 C4 Schlüsselstrom: 02
IV: 03 01 C4 Schlüsselstrom: F8
IV: 03 FF 15 Schlüsselstrom: E0
IV: 03 FF 78 Schlüsselstrom: 54
IV: 02 01 A3 Schlüsselstrom: FC
IV: 02 01 F3 Schlüsselstrom: 73
IV: 03 FF 99 Schlüsselstrom: AA
IV: 02 01 0C Schlüsselstrom: CD
IV: 02 01 43 Schlüsselstrom: A1
IV: 02 FF 27 Schlüsselstrom: 2A
IV: 03 01 FA Schlüsselstrom: 2E
IV: 03 FF 08 Schlüsselstrom: 0A
IV: 03 FF 84 Schlüsselstrom: 4F
IV: 02 FF 9F Schlüsselstrom: C9
IV: 03 01 CC Schlüsselstrom: 27
IV: 03 01 63 Schlüsselstrom: 95
IV: 02 FF 5F Schlüsselstrom: 38
IV: 03 01 8D Schlüsselstrom: DA
IV: 03 FF FC Schlüsselstrom: 6F
IV: 03 FF 8E Schlüsselstrom: EC
IV: 03 01 AA Schlüsselstrom: D8
IV: 02 FF CF Schlüsselstrom: E0
IV: 02 FF E2 Schlüsselstrom: 91
IV: 02 01 34 Schlüsselstrom: 4E
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SuppressWarnings("WeakerAccess")
class IVKeystreamEntry {
int first, second, third;
int key;
IVKeystreamEntry() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IVKeystreamEntry that = (IVKeystreamEntry) o;
return first == that.first &&
second == that.second &&
third == that.third &&
key == that.key;
}
@Override
public int hashCode() {
return Objects.hash(first, second, third, key);
}
@Override
public String toString() {
return "IVKeystreamEntry{" +
"first=" + first +
", second=" + second +
", third=" + third +
", key=" + key +
'}';
}
}
public class Main {
private static final Pattern linePattern = Pattern.compile("IV: (?<first>[0-9A-Z]+)\\s(?<second>[0-9A-Z]+)\\s(?<third>[0-9A-Z]+)\\s+Schlüsselstrom:\\s(?<key>[0-9A-Z]+)");
public static void main(String[] args) throws IOException {
@SuppressWarnings("SpellCheckingInspection") File file = new File("IVkeystream.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
reader.readLine();
String line;
List<IVKeystreamEntry> entryList = new ArrayList<>();
try (reader) {
line = reader.readLine();
while (line != null) {
line = reader.readLine();
entryList.add(mapToEntry(line));
}
} catch (IOException e) {
e.printStackTrace();
}
entryList.stream()
.filter(Objects::nonNull)
.filter(entry -> entry.first == 3 && entry.second == 255)
.map(IVKeystreamEntry::toString)
.reduce((sum, item) -> sum + '\n' + item)
.ifPresent(System.out::println);
}
private static IVKeystreamEntry mapToEntry(String line) {
if (line == null) {
return null;
}
Matcher matcher = linePattern.matcher(line);
if (!matcher.find()) {
return null;
}
final IVKeystreamEntry entry = new IVKeystreamEntry();
Arrays.asList(entry.getClass().getDeclaredFields())
.forEach(field -> {
try {
field.setInt(entry, Integer.valueOf(matcher.group(field.getName()), 16));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
});
return entry;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment