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