This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ChasedMonkey's profile picture

Caesar Cypher issue

Started by ChasedMonkey, 31 August 2012 - 03:32 AM
ChasedMonkey #1
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.
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")
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?
Jan #2
Posted 31 August 2012 - 05:09 PM
You could do % as modulus?
For example the time is 23h . And you want to know the time 4 hours later:
(23+4) % 24 = 3

Also I would not recommend using the ANSI- alphabet, because some characters are unprintable and result in ?
ChasedMonkey #3
Posted 05 September 2012 - 03:13 PM
Its not that they result in a ? because they are unprintible. Its that symbols are numericaly after capitol letters.
Sourceless #4
Posted 08 September 2012 - 10:28 PM
If the value of Z + 6 is more than Z, take away Z. Likewise from the other direction, but instead add Z.