Skip to content

Instantly share code, notes, and snippets.

@dlaxar
Created October 23, 2012 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlaxar/3941131 to your computer and use it in GitHub Desktop.
Save dlaxar/3941131 to your computer and use it in GitHub Desktop.
MPCA #2
package u2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class CipherManager {
private byte[] bytes;
static final int ASCII_MIN = 32;
static final int ASCII_MAX = 127;
private static final int THREADS = 64;
private int runningThreads;
private boolean enableExiting;
public static final String[] WORDS = {"the", "be", "to", "of", "and"};
private long startTime;
public CipherManager() {
this.startTime = System.currentTimeMillis();
runningThreads = 0;
BufferedReader br = null;
String s = "";
String c = "";
try {
br = new BufferedReader(new FileReader(new File("./input.txt")));
while((s = br.readLine()) != null) {
c += s;
}
String[] b = c.split(",");
bytes = new byte[b.length];
for(int i = 0; i < b.length; i++) {
bytes[i] = Byte.parseByte(b[i]);
}
CipherRunner.input = bytes;
CipherRunner.toComplete = " the ".getBytes();
} catch(Exception e) {
e.printStackTrace();
} finally {
if(br != null) try { br.close(); } catch(Exception e) {}
}
for(int i = 0; i < THREADS; i++) {
dispatch(i);
}
enableExiting = true;
}
public synchronized void dispatch(int threadNumber) {
runningThreads++;
//each thread gets a range of chars to work with
new CipherRunner(this,
initKey(3, (int) ((threadNumber) * Math.pow(ASCII_MAX-ASCII_MIN, 3)/THREADS)),
(long) ((Math.pow(ASCII_MAX-ASCII_MIN, 3)/THREADS))).start();
}
public synchronized void finished() {
runningThreads--;
if(enableExiting && runningThreads == 0) {
System.out.println("This program ran " + (System.currentTimeMillis() - this.startTime) + "ms");
System.exit(0);
}
}
public synchronized void reportKey(byte[] key) {
StringBuilder sb = new StringBuilder();
int keyIndex = 0;
for(byte rb : CipherRunner.input) {
sb.append((char)(rb ^ key[keyIndex++]));
if(keyIndex == key.length) {
keyIndex = 0;
}
}
//check if all criteria are met
String out = sb.toString();
String lower = out.toLowerCase();
int all = WORDS.length;
for(String s : WORDS) {
if(lower.contains(s.toLowerCase())) all--;
}
if(all != 0) return;
//output
System.out.println("key: >>" + new String(key) + "<<");
System.out.println("key: " + (short)key[0] + " " + (short)key[1] + " " + (short)key[2]);
System.out.println(out);
}
private byte[] initKey(int digits, int number) {
byte[] nextKey = new byte[digits];
for(int i = 0; i < nextKey.length; i++) {
nextKey[i] = (byte) (number/Math.pow(ASCII_MAX-ASCII_MIN, nextKey.length-i-1)+32);
number %= Math.pow(ASCII_MAX-ASCII_MIN,nextKey.length-i-1);
}
return nextKey;
}
public static void main(String[] args) {
new CipherManager();
}
}
package u2;
public class CipherRunner extends Thread {
public static byte[] input;
public static byte[] toComplete;
private byte[] key;
private CipherManager ref;
private long number;
public CipherRunner(CipherManager ref, byte[] key, long number) {
this.ref = ref;
this.key = key;
this.number = number;
}
@Override
public void run() {
//for each key in the range
while(this.number-- >= 0) {
int keyIndex = 0;
int completedIndex = 0;
//for each input byte
for(byte b : input) {
//decode
if((b ^ key[keyIndex++]) == toComplete[completedIndex]) {
completedIndex++;
if(completedIndex == toComplete.length) {
// the the master thread that we've found a valid key
ref.reportKey(key);
break;
}
} else {
completedIndex = 0;
}
if(keyIndex == key.length) {
keyIndex = 0;
}
}
for(int i = key.length-1; i >= 0; i--) {
if(key[i] != (byte)CipherManager.ASCII_MAX) {
key[i] = (byte) (key[i]+1);
break;
}
key[i] = CipherManager.ASCII_MIN;
}
}
//exit program cleanly
ref.finished();
}
}
Here all the solutions for MPCA #2 will go
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'e', 'f', 'g', 'a'];
var map = {};
for(var i = 0; i < array.length; i++) {
if(map[array[i]]) {
map[array[i]].push(i);
}
else {
map[array[i]] = [i];
}
}
console.log(map);
var n = 547362514;
var sum = 0;
while(n > 0) {
sum += n%10;
n = parseInt(n/10);
}
console.log(sum);
http://new.shenteq.com/htl/mysite_week2/index.php?id=12%20or%201=1
Array.prototype.contains = function(el) {
for(var i = 0; i < this.length; i++) {
if(el == this[i]) return true;
}
return false;
};
var a = [4,5,7,8,2];
var b = [7,5,1,8,4];
var c = [];
for(var i = 0; i < a.length; i++) {
if(!b.contains(a[i])) {
c.push(a[i]);
}
}
console.log(c);
from array import array
a = (4,5,7,8,2)
b = (7,5,1,8,4)
c = []
for i in a:
if not (i in b):
c.append(i);
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment