Skip to content

Instantly share code, notes, and snippets.

@obikag
Created October 18, 2013 02:39
Show Gist options
  • Save obikag/7035680 to your computer and use it in GitHub Desktop.
Save obikag/7035680 to your computer and use it in GitHub Desktop.
ROT13 substitution cipher implemented in Lua. ROT13 function encrypts and decrypts a given string. Special characters and numbers are not encrypted with this cipher.
--Create a table to store the cipher
cipher = {
A="N",B="O",C="P",D="Q",E="R",F="S",G="T",H="U",I="V",J="W",K="X",L="Y",M="Z",
N="A",O="B",P="C",Q="D",R="E",S="F",T="G",U="H",V="I",W="J",X="K",Y="L",Z="M",
a="n",b="o",c="p",d="q",e="r",f="s",g="t",h="u",i="v",j="w",k="x",l="y",m="z",
n="a",o="b",p="c",q="d",r="e",s="f",t="g",u="h",v="i",w="j",x="k",y="l",z="m"
}
--Function encrypts and decrypts using ROT13
function ROT13(str)
local estr = ""
for c in str:gmatch(".") do --for each character in the string
if (c:match("%a")) then --true if character is a letter
estr = estr..cipher[c]
else
estr = estr..c
end
end
return estr
end
--Test Section
print(ROT13("Hello World")) -- Uryyb Jbeyq
print(ROT13("The quick brown fox jumps over the lazy moon")) --Gur dhvpx oebja sbk whzcf bire gur ynml zbba
print(ROT13("Are you hungry??")) --Ner lbh uhatel??
print(ROT13("Hey!!")) -- Url!!
print(ROT13("[email protected]")) [email protected]
print(ROT13("Qrpelcgrq")) --Decrypted
@ukn-ubi
Copy link

ukn-ubi commented Jan 10, 2015

Hey cool code man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment