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

[Lua] Slight problem with my Decrypted output

Started by Rwkeith, 06 January 2013 - 11:36 AM
Rwkeith #1
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()

pastebin get hQctdisk encrypt
imef #2
Posted 06 January 2013 - 03:51 PM
Here's a corrected part of the code :
Spoiler

for n = 1,kSize do
  keyTable[n] = string.byte(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])
  textTable[i] = bit.bxor(textTable[i],keyTable[t])
  textTable[i] = string.format("%c",textTable[i])
end


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.
Rwkeith #3
Posted 07 January 2013 - 05:06 AM
Here's a corrected part of the code :
Spoiler

for n = 1,kSize do
  keyTable[n] = string.byte(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])
  textTable[i] = bit.bxor(textTable[i],keyTable[t])
  textTable[i] = string.format("%c",textTable[i])
end


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/>