lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


So this worked for the 1A but it didn't fix the problem with 0A. Help?
On Sat, Dec 24, 2016 at 12:12 AM Ulrich Schmidt <u.sch.zw@gmx.de> wrote:
You need to open the file in "binary mode" eg:

   k,g,c=io.read(),1,io.open(a,"rb")



Ulrich.



Am 24.12.2016 um 08:57 schrieb Creation Elemental:

> Using the following code I have noticed that it stops reading a file if

> it reaches the character 1A (decimal: 26). Then, if it is trying to

> write the character 0A to a file it will write it as 0D0A. I looked at

> the file with a hexadecimal viewer. I also noticed that after it had

> been written as 0D0A, if I ran the file again, it would no longer add

> the 0D as the 0D was already present. If anyone can tell me why this

> happens (whether it is a problem with my code of with Lua or whatever

> that would be much appreciated

>

> lua code is as follows

>

> print("Enter File Path:")

> a=io.read()

> print("Enter Key:")

> k,g,c=io.read(),1,io.open(a,"r")

> x,n,d=string.len(k),1,c:read("*all")

> c:close()

> print("Encrypting (0) or Decrypting (1)?")

> c,s,f=io.read(),string.len(d),io.open(a,"w")

> while g<=s do

> b,e,n,g=string.byte(d,g,g),string.byte(k,n,n),n+1,g+1

> if n>x then n=n-x end

> if c=="0" then y=256-e else y=e end

> z=y+b

> if z>255 then z=z-256 end

> z=string.char(z)

> f:write(z)

> end

> f:close()



--
-Ken Peterson A.K.A. Elemental Creation