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