Lua - 包含string.gsub的密码逻辑错误,未输入加密输出

时间:2014-07-13 10:07:42

标签: string encryption lua gsub

我的程序似乎遇到了逻辑错误。我已多次查看它,甚至编写了另一个与此类似的程序(似乎也有同样的错误)。我无法弄清楚出了什么问题,虽然我认为它可能涉及到我对string.gsub的使用......

repeat
local file = io.open("out.txt", "w")
print("would you like to translate Cipher to English or English to Cipher?")
print("enter 1 for translation to English. enter 2 for translation to Cipher")
tchoice=io.read()
if tchoice=="2" then
print(" enter any text to translate it: ")
rawtextin=io.read()
text=string.lower(rawtextin)
text1=string.gsub(text,"a","q")
text2=string.gsub(text1,"b","e")
text3=string.gsub(text2,"c","h")
text4=string.gsub(text3,"d","c")
text5=string.gsub(text4,"e","j")
text6=string.gsub(text5,"f","m")
text7=string.gsub(text6,"g","r")
text8=string.gsub(text7,"h","g")
text9=string.gsub(text8,"i","b")
text10=string.gsub(text9,"j","a")
text11=string.gsub(text10,"k","d")
text12=string.gsub(text11,"l","y")
text13=string.gsub(text12,"m","v")
text14=string.gsub(text13,"n","z")
text15=string.gsub(text14,"o","x")
text16=string.gsub(text15,"p","k")
text17=string.gsub(text16,"q","i")
text18=string.gsub(text17,"r","l")
text19=string.gsub(text18,"s","f")
text20=string.gsub(text19,"t","s")
text21=string.gsub(text20,"u","w")
text22=string.gsub(text21,"v","t")
text23=string.gsub(text22,"w","p")
text24=string.gsub(text23,"x","u")
text25=string.gsub(text24,"y","n")
text26=string.gsub(text25,"z","o")
text27=string.gsub(text26," ","@")
print(text27)
elseif tchoice=="1" then
print("enter text!")
rawtextin=io.read()
text=string.lower(rawtextin)
text1=string.gsub(text,"q","a")
text2=string.gsub(text1,"e","b")
text3=string.gsub(text2,"h","c")
text4=string.gsub(text3,"c","d")
text5=string.gsub(text4,"j","e")
text6=string.gsub(text5,"m","f")
text7=string.gsub(text6,"r","g")
text8=string.gsub(text7,"g","h")
text9=string.gsub(text8,"b","i")
text10=string.gsub(text9,"a","j")
text11=string.gsub(text10,"d","k")
text12=string.gsub(text11,"y","l")
text13=string.gsub(text12,"v","m")
text14=string.gsub(text13,"z","n")
text15=string.gsub(text14,"x","o")
text16=string.gsub(text15,"k","p")
text17=string.gsub(text16,"i","q")
text18=string.gsub(text17,"l","r")
text19=string.gsub(text18,"f","s")
text20=string.gsub(text19,"s","t")
text21=string.gsub(text20,"w","u")
text22=string.gsub(text21,"t","v")
text23=string.gsub(text22,"p","w")
text24=string.gsub(text23,"u","x")
text25=string.gsub(text24,"n","y")
text26=string.gsub(text25,"o","z")
text27=string.gsub(text26,"@"," ")
print(text27)
end
print("writing to out.txt...")
file:write(text27)
file:close()
print("done!")
print("again? type y for yes or anything else for no.")
again=io.read()
until again~="y"
x=io.read()

代码中没有错误 - 我缺少什么?我知道这不是最有效的方法,但我需要在使用循环和表格编写更高效的程序之前弄清楚出了什么问题。

样本运行(仅包含重要数据):

in:2
in:hi test
out:gb@safs
in:y
in:1
in:gb@safs
out:hq vjvv

1 个答案:

答案 0 :(得分:3)

local decoded = 'abcdefghijklmnopqrstuvwxyz @'
local encoded = 'qehcjmrgbadyvzxkilfswtpuno@ '

local enc, dec = {}, {}
for i = 1, #decoded do
   local e, d = encoded:sub(i,i), decoded:sub(i,i)
   enc[d] = e
   dec[e] = d
end

repeat
   local file = io.open("out.txt", "w")
   local text27, rawtextin
   print("would you like to translate Cipher to English or English to Cipher?")
   print("enter 1 for translation to English. enter 2 for translation to Cipher")
   local tchoice = io.read()
   if tchoice == "2" then
      print(" enter any text to translate it: ")
      rawtextin = io.read()
      text27 = rawtextin:lower():gsub('.',enc)
      print(text27)
   elseif tchoice == "1" then
      print("enter text!")
      rawtextin = io.read()
      text27 = rawtextin:lower():gsub('.',dec)
      print(text27)
   end
   print("writing to out.txt...")
   file:write(text27)
   file:close()
   print("done!")
   print("again? type y for yes or anything else for no.")
   local again = io.read()
until again ~= "y"