Re: Data Encryption
- Posted by Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL> May 15, 1998
- 752 views
I haven't tested it, but I already found a logical adjustment tha prolly made the bug occur. When you open a file normally, after every '\n' a value of 10 (ascII 10) gets placed, so your file obbeys to the ASCII text format. When you read in however, only gets () removes such a character again. And even if getc () also did that (I dunno.. I don't think so) there would still be the case where you yourself put a value of 10 to the value (due to encrypting or whatever) but when you read the file back, it gets eliminated. To avoid this.. open such files as binairy. Thus with the option flags: "wb" to write and "rb" to read. Furthermore I've shorten some of your code.. and the '!' means, the line has to replaced with the next line. You're welcome. Ralf Nieuwenhuijen nieuwen at xs4all.nl >----------- Encrypt.ex starts here ---------------- > >include get.e include machine.e -- int_to_bits () and bits_to_int () >function ch_to_bits(integer ch) --changes a character to bits -- Be warned it returns its values in reversed order!! return int_to_bits (ch,8) >end function >function bits_to_ch(sequence bits) --changes bits to a character return bits_to_int (bits) >end function >function encrypt(integer ch) > integer x > sequence bits > x = rand(8) --select a random number... > bits = ch_to_bits(ch) X bits[x] = (1 - (bits[x] - '0')) + '0' --...and switch that element's bit bits[x] = not bits[x] -- we no longer use '0' but real FALSE value and real TRUE value > ch = bits_to_ch(bits) > return {x,ch} --return {element changed,new random character} >end function >function decrypt(sequence encrypted) --a sequence of {bit switched,new >character} > integer ch > sequence bits > bits = ch_to_bits(encrypted[2]) ! bits[encrypted[1]] = (1-(bits[encrypted[1]] - '0')) + '0' --switch bits[encrypted[1]] = not bits[encrypted[1]] > ch = bits_to_ch(bits) > return ch >end function > >procedure encrypt_file() --self-explanatory > integer ch,filenum,done,filenum2 > sequence file,dummy > done = 0 > puts(1,"Enter the file name now: ") > file = gets(0) > position(17,30) > file = file[1..length(file)-1] > filenum = open(file,"r") > if filenum = -1 then > printf(1,"Cannot open \"%s\"",{file}) > ch = wait_key() > else ! filenum2 = open(file,"w") filenum2 = open(file, "wb") > while not done do > ch = getc(filenum) > if ch = -1 then > done = 1 > elsif ch = '\n' then > puts(filenum2,ch) > else > dummy = encrypt(ch) > printf(filenum2,"%s%s",{dummy[1],dummy[2]}) > end if > end while > close(filenum) > close(filenum2) > puts(1,"Your file has been encrypted.") > ch = wait_key() > end if >end procedure > >procedure decrypt_file() > sequence file,dummy2 > integer done,filenum,filenum2,dummy,ch2,ch > done = 0 > puts(1,"Enter the filename now: ") > file = gets(0) > position(17,30) > dummy2 = "" > file = file[1..length(file)-1] ! filenum = open(file,"r") filenum = open(file, "rb") > if filenum = -1 then > printf(1,"Cannot open \"%s\"",{file}) > else > filenum2 = open(file,"w") > --trace(1) > while not done do > ch = getc(filenum) --here is where the error occurs. It should >always be in [1..8] > if ch = -1 then > done = 1 > elsif ch = '\n' then > puts(filenum2,ch) > else > ch2 = getc(filenum) > dummy = decrypt({ch,ch2}) > dummy2 = dummy2 & dummy --debug help, ignore > puts(filenum2,dummy) > end if > end while > close(filenum) > close(filenum2) > puts(1,"Your file has been decrypted.") > done = wait_key() > end if >end procedure > >procedure main() > sequence choices > integer ch,done > done = 0 > choices = {"Encrypt a file","Decrypt a file","Quit"} > while not done do > clear_screen() > for i = 1 to 3 do > position(10+i,30) > printf(1,"%d: %s",{i,choices[i]}) > end for > ch = wait_key() > position(15,30) > if ch = '1' then > encrypt_file() > elsif ch = '2' then > decrypt_file() > elsif ch = '3' then > done = 1 > end if > end while >end procedure > >main()