Last active
December 30, 2015 03:49
-
-
Save dmadisetti/7772132 to your computer and use it in GitHub Desktop.
Bit of Crypto. Nothing elegant, just a quick hack to decrypt a newspaper puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I normally use python for quick hacks like these- but javascript is so familiar at this point | |
var cipher = "ni xiph btffbo ytf iw liin azoho xip vho tf r fziro btffbo ytfr iw liin spf filofzoh fzvf imohazobg fzo aihbn norgijn fpfp".split('') | |
, i = cipher.length - 1 | |
, dictionary = {} | |
, rank = ["E","T","A","O","I","N","S","H","R","D","L","C","U","M","W","F","G","Y","P","B","V","K","X","Q","J","Z"]; | |
// based off of http://www.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html | |
// Probably find a way to sort as populating, but quicksort is so easy.. and I'm pretty lazy.. | |
do{ | |
dictionary[cipher[i]] = dictionary[cipher[i]] == undefined ? 1 : dictionary[cipher[i]] + 1; | |
}while(i--) | |
sorted = []; | |
// Sort frequency | |
for (var key in dictionary) sorted.push({'f':dictionary[key],'value':key}); | |
quicksort(0,sorted.length - 1); | |
// Kill the space | |
sorted.shift(); | |
i = cipher.length - 1; | |
// Apply frequency index | |
// Hopefully look more readable | |
do{ | |
val = cipher[i]; | |
ii = sorted.length - 1; | |
do{ | |
if(val == sorted[ii].value){ | |
cipher[i] = rank[ii]; | |
break; | |
} | |
}while(ii--) | |
}while(i--) | |
// Replace for manual swapping | |
function redo(a,b){ | |
i = cipher.length - 1; | |
do{ | |
if(cipher[i] == a) cipher[i] = b; | |
else if(cipher[i] == b) cipher[i] = a; | |
}while(i--) | |
} | |
// Just playing around | |
redo('D','I'); | |
redo('O','T'); | |
redo('U','Y'); | |
redo('N','L'); | |
redo('E','T'); | |
redo('E','A'); | |
redo('H','I'); | |
redo('R','U'); | |
redo('W','A'); | |
redo('N','G'); | |
// More or less got it at this point | |
redo('W','H'); | |
redo('S','R'); | |
redo('A','B'); | |
redo('F','A'); | |
redo('W','S'); | |
redo('P','N'); | |
redo('M','P'); | |
redo('C','W'); | |
redo('C','P'); | |
redo('C','F'); | |
redo('C','V'); | |
// Boom | |
// "DO YOUR LITTLE BIT OF GOOD WHERE YOU ARE IT S THOSE LITTLE BITS OF GOOD PUT TOGETHER THAT OVERWHELM THE WORLD DESMOND TUTU" | |
cipher = cipher.join(''); | |
// QuickSort stuff | |
function quicksort(left, right){ | |
// Needs at least 2 | |
if(left < right){ | |
pivotIndex = Math.floor((left+right)/2); | |
pivotNewIndex = partition(left, right, pivotIndex); | |
quicksort(left, pivotNewIndex - 1); | |
quicksort(pivotNewIndex + 1, right); | |
} | |
} | |
function partition(left, right, pivotIndex){ | |
var pivotValue = sorted[pivotIndex].f; | |
swap(right,pivotIndex); | |
storeIndex = left; | |
for (var i = left; i < right; i++) { | |
if(sorted[i].f >= pivotValue) { | |
swap(i,storeIndex); | |
storeIndex++; | |
} | |
} | |
swap(storeIndex,right); // Move pivot to its final place | |
return storeIndex; | |
} | |
function swap(a,b){ | |
var holder = sorted[a]; | |
sorted[a] = sorted[b]; | |
sorted[b] = holder; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Different puzzle
var cipher = "g ysm x rmsv vfp ctgxgwf dkxstw dtl nlxxgmn cgn zdtxw gm diltgkdm xj wfsvw idpcl gx w clkdhwl vl tl kfldz ysigmgk vlwx".split('')
redo('T','I');
redo('A','T');
redo('S','O');
redo('N','V');
redo('R','W');
redo('N','O');
redo('G','C');
redo('D','A');
redo('B','O');
redo('U','G');
redo('L','B');
redo('R','V');
redo('C','W');
redo('A','O');
redo('D','A');
redo('U','H');
redo('Y','F');
redo('C','W');
redo('C','D');
redo('P','K');
redo('P','V');
redo('C','U');
redo('V','U');
redo('F','U');
// "I DON T KNOW WHY BRITISH ACTORS ARE GETTING BIG PARTS IN AMERICAN TV SHOWS MAYBE IT S BECAUSE WE RE CHEAP DOMINIC WEST"