Created
October 5, 2012 05:36
-
-
Save ex/3838276 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// The text to decipher. It was created using a substitution cipher | |
var text = "Fxr nb, ekb dmrdxjb xf upfb pj, ie ubije direuo, ex kizb txo.\n" | |
+ "Drxlrinnbrj xfeby fbbu txo akby ekbo qiy qxyqbyerieb xy ekb qrbiepzb\n" | |
+ "jpwb xf drxlrinnpyl, Jx Rmho pj wbjplybw ex nigb drxlrinnbrj kiddo.\n" | |
+ "Rmho pykbrpebw ekb Dbru dkpuxjxdko xf kizpyl nxrb ekiy xyb aio\n" | |
+ "ex wx ekb jinb ekpyl. P pykbrpebw ekie dkpuxjxdko frxn Uirro Aiuu,\n" | |
+ "akx pj no kbrx iqemiuuo. P aiye ex nigb Rmho mjbrj frbb. P aiye ex\n" | |
+ "lpzb ekbn ekb frbbwxn ex qkxxjb. Dbxdub irb wpffbrbye. Dbxdub qkxxjb\n" | |
+ "wpffbrbye qrpebrpi. Hme pf ekbrb pj i hbeebr aio inxyl niyo\n" | |
+ "iuebryiepzbj, P aiye ex byqxmrilb ekie aio ho nigpyl pe qxnfxreihub.\n" | |
+ "Jx ekie'j akie P'zb erpbw ex wx.\n" | |
+ "P aiye ex jxuzb drxhubnj P nbbe py ekb wipuo upfb ho mjpyl qxndmebrj,\n" | |
+ "jx P ybbw ex arpeb drxlrinj. Ho mjpyl Rmho, P aiye ex qxyqbyerieb ekb\n" | |
+ "ekpylj P wx, yxe ekb nilpqiu rmubj xf ekb uiylmilb, upgb jeirepyl apek\n" | |
+ "dmhupq zxpw jxnbekpyl jxnbekpyl jxnbekpyl ex jio, \"drpye kbuux axruw.\"\n" | |
+ "P tmje aiye ex jio, \"drpye ekpj!\" P wxy'e aiye iuu ekb jmrrxmywpyl\n" | |
+ "nilpq gboaxrwj. P tmje aiye ex qxyqbyerieb xy ekb eijg. Ekie'j ekb hijpq\n" | |
+ "pwbi. Jx P kizb erpbw ex nigb Rmho qxwb qxyqpjb iyw jmqqpyqe.\n" | |
+ "Omgpkprx Niejmnxex. (lxllub keed://aaa.irepni.qxn/pyez/rmhoD.kenu)\n"; | |
// This is the frequency table of the language of the original text | |
// (from more frequent to less frequent). | |
var freqLang = "TEOIARNSHMLYGCPUDWFBVKJXQZ"; | |
// Create frequency array for the encrypted text. | |
var len = text.length; | |
var frequency = []; | |
var regexUp = new RegExp("[A-Z]"); | |
for (var k = 0; k < len; k += 1) { | |
var c = text.charAt(k).toUpperCase(); | |
// Check if character is an uppercase letter. | |
if (regexUp.test(c)) { | |
if (frequency[c] == null) { | |
frequency[c] = 1; | |
} | |
else { | |
frequency[c] += 1; | |
} | |
} | |
} | |
// Create array for sorting the text frequency. | |
var sortFreq = []; | |
for (var k in frequency) { | |
sortFreq.push({key: k, value: frequency[k]}); | |
} | |
// Sort in descending order. | |
sortFreq.sort(function(a, b) { return (b.value - a.value) }); | |
// Create dictionary for deciphering. | |
var dic = []; | |
var freqText = ''; | |
var index = 0; | |
for (var k in sortFreq) { | |
freqText += sortFreq[k].key; | |
dic[sortFreq[k].key] = freqLang.charAt(index); | |
index += 1; | |
} | |
// Deciphering text by replacing characters. | |
var decrypted = ''; | |
for (var k = 0; k < len; k += 1) { | |
var uppercase = false; | |
var c = text.charAt(k); | |
if (regexUp.test(c)) { | |
uppercase = true; | |
} | |
else { | |
c = c.toUpperCase(); | |
} | |
if (dic[c] != null) { | |
if (uppercase) { | |
decrypted += dic[c]; | |
} | |
else { | |
decrypted += dic[c].toLowerCase(); | |
} | |
} | |
else { | |
decrypted += c; | |
} | |
} | |
console.log(decrypted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment