Posted 31 August 2012 - 05:32 AM
So today i was coding a Lua caesar cypher based off of my Java one i created a while back, problem is im a terrable Lua programer. I made what i have so far by looking at other programs to learn how Lua booleans and stuff work. What i have so far works… ish.
Basicaly it converts every letter to its numerical value, adds the key, then converts it back to its letter value. The issue is that lower case, upper case, and symbols are all in the same numerical system. So after Z you don't get A you get some symbol. Right now my caesar works if it doesn't have to convert anything past Z. A + 6 = G, but Z + 6 = ?.
What i need to do is have a loop that says "If enc + key > 'Z' then enc = enc - 26" but im not sure how to do that in Lua. Any ideas?
Spoiler
- print("Welcome to the monkey cypher")
print("Please enter encryption key")
key = read()
function encrypt(data)
i = 1
enc = ""
repeat
enc = enc .. string.char(data:byte(i) + key)
i = i+1
until data:byte(i) == nil
return enc
end
print("Enter text to encrypt: ")
str = read()
str = string.upper(str)
print("Original message: " .. str)
str = encrypt(str)
print("Encrypted message: " .. str)
print("")
print("To decrypt use the inverse key you just used.")
print("Ex. If i used 6 to encrypt the message i would use -6 to decrypt it")
What i need to do is have a loop that says "If enc + key > 'Z' then enc = enc - 26" but im not sure how to do that in Lua. Any ideas?