Jump to content




[Lua] Slight problem with my Decrypted output


2 replies to this topic

#1 Rwkeith

  • Members
  • 56 posts

Posted 06 January 2013 - 12:36 PM

My code syntax is fine. The problem is the output. This program does a simple xor encryption/Decryption with a text string and a key provided. It prints out the decrypted/encrypted text. The output however is messed up.

Just the first few characters of my decrypted string is not right. The rest of my string is intact. The longer my key is, the longer the incorrect characters.


--Let's see here
--This might work..
textTable = {}
keyTable = {}
readFile = fs.open("log","r")
text = readFile.readAll()
readFile.close()
print("Unencrypted:")
write(text.."\n")
--Inialize tables
size = string.len(text)
for o=1,size do
  textTable[o] = string.sub(text,o,o)
end
key = "string"
kSize = string.len(key)
for n = 1,kSize do
  keyTable[n] = string.sub(key,n,n)
end
--Encrypt
for i=1,size do
  if i > kSize then
	t = (i % kSize) + 1
  else
	t = i
  end
  textTable[i] = string.byte(textTable[i])
  keyTable[t] = string.byte(keyTable[t])
  textTable[i] = bit.bxor(textTable[i],keyTable[t])
  textTable[i] = string.format("%c",textTable[i])
end
print("Encrypted:")
for z=1,size do
  write(textTable[z])
end
print()
local s = table.getn(textTable)
print("Size of Encrypted:"..s)
print("Size of Original:"..size)
--Decrypt
for i=1,size do
if i > kSize then
  t = (i % kSize) + 1
else
  t = i
end
textTable[i] = string.byte(textTable[i])
textTable[i] = bit.bxor(textTable[i],keyTable[t])
-- textTable[i] = string.format("%c",textTable[i])
textTable[i] = string.char(textTable[i])
end
print("Decrypted:")
for z=1,size do
  write(textTable[z])
end
print()

Quote

pastebin get hQctdisk encrypt


#2 imef

  • Members
  • 18 posts
  • LocationFrance

Posted 06 January 2013 - 03:51 PM

Here's a corrected part of the code :
Spoiler


The Problem came from the statement : "keyTable[t] = string.byte(keyTable[t])" in the encryption loop.
This instruction is executed "size" times in the non corrected version whereas it should be done only "ksize" times.

Why only the beginning of the text was affected remains a mystery to me.

#3 Rwkeith

  • Members
  • 56 posts

Posted 07 January 2013 - 05:06 AM

View Postimef, on 06 January 2013 - 03:51 PM, said:

Here's a corrected part of the code :
Spoiler


The Problem came from the statement : "keyTable[t] = string.byte(keyTable[t])" in the encryption loop.
This instruction is executed "size" times in the non corrected version whereas it should be done only "ksize" times.

Why only the beginning of the text was affected remains a mystery to me.

Thanks a ton for catching that :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users