Skip to content

Instantly share code, notes, and snippets.

@gorlum0
Created August 31, 2011 09:27
Show Gist options
  • Save gorlum0/1183158 to your computer and use it in GitHub Desktop.
Save gorlum0/1183158 to your computer and use it in GitHub Desktop.
topcoder - 516-2 - 500 (java)
import java.io.*;
import java.util.*;
import java.math.*;
public class NetworkXOneTimePad
{
Long[] convert(String[] arr) {
int n = arr.length;
Long[] res = new Long[n];
for (int i = 0; i < n; i++)
res[i] = Long.parseLong(arr[i], 2);
return res;
}
public int crack(String[] plaintexts, String[] ciphertexts) {
Long[] plains = convert(plaintexts);
Long[] ciphers = convert(ciphertexts);
Vector<Long> keys = new Vector();
long c0 = ciphers[0];
for (long p : plains)
keys.add(c0 ^ p);
// Long (not long) for hashing
HashSet<Long> plainset = new HashSet(Arrays.asList(plains));
for (long c : ciphers) {
Vector<Long> keys2 = new Vector();
for (long k : keys)
if (plainset.contains(c ^ k))
keys2.add(k);
keys = keys2;
}
return keys.size();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment