Data Encryption
- Posted by Bryan Watts <mr_bungle at COMPUSERVE.COM> May 14, 1998
- 767 views
To all you Euphorians out there, I have recently gotten into data encryption algorithms, and just to see, I have developed a *very* basic one, implemented into the program at the end of this letter. I can get a sentence encrypted and decrypted fine, and I can also encrypt a file as well. My problem is that when I try to decrypt the file, sometimes the marker that shows which bit to switch in the character ("ch" in the "decrypt_file" procedure) sometimes reads in a very strange value, and causes a subscript error. It happens randomly, sometimes not at all, and it has me completely baffled. I think this would be a very cool program, and useful too, if I could get it to run!! Any help as to what may be causing this weird error would be greatly appreciated!! Thanks a lot. Regards, Bryan Watts (Oh yeah, I'm a junior in high school, and while it may not seem like much to you guys, my team came in 4th place out of 40 teams in the SouthWest US in a computer programming contest at UCF. Yay me!) ----------- Encrypt.ex starts here ---------------- include get.e with trace function ch_to_bits(integer ch) --changes a character to bits integer num2 sequence bits bits =3D repeat(0,8) num2 =3D 128 for i =3D 1 to 8 do if ch - num2 < 0 then bits[i] =3D '0' else bits[i] =3D '1' end if if bits[i] =3D '1' then ch =3D ch - num2 end if if i !=3D 8 then num2 =3D num2 / 2 end if end for return bits end function function bits_to_ch(sequence bits) --changes bits to a character integer num2,final num2 =3D 128 final =3D 0 for i =3D 1 to 8 do if bits[i] =3D '1' then final =3D final + num2 end if if i !=3D 8 then num2 =3D num2 / 2 end if end for return final end function function encrypt(integer ch) = integer x sequence bits x =3D rand(8) --select a random number... bits =3D ch_to_bits(ch) bits[x] =3D (1 - (bits[x] - '0')) + '0' --...and switch that element's= bit ch =3D 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 =3D ch_to_bits(encrypted[2]) bits[encrypted[1]] =3D (1-(bits[encrypted[1]] - '0')) + '0' --switch original bit back ch =3D bits_to_ch(bits) return ch end function procedure encrypt_file() --self-explanatory integer ch,filenum,done,filenum2 sequence file,dummy done =3D 0 puts(1,"Enter the file name now: ") file =3D gets(0) position(17,30) file =3D file[1..length(file)-1] filenum =3D open(file,"r") if filenum =3D -1 then printf(1,"Cannot open \"%s\"",{file}) ch =3D wait_key() else filenum2 =3D open(file,"w") while not done do ch =3D getc(filenum) if ch =3D -1 then done =3D 1 elsif ch =3D '\n' then puts(filenum2,ch) else dummy =3D 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 =3D wait_key() end if end procedure = procedure decrypt_file() sequence file,dummy2 integer done,filenum,filenum2,dummy,ch2,ch done =3D 0 puts(1,"Enter the filename now: ") file =3D gets(0) position(17,30) dummy2 =3D "" file =3D file[1..length(file)-1] filenum =3D open(file,"r") if filenum =3D -1 then printf(1,"Cannot open \"%s\"",{file}) else filenum2 =3D open(file,"w") --trace(1) while not done do ch =3D getc(filenum) --here is where the error occurs. It should always be in [1..8] if ch =3D -1 then done =3D 1 elsif ch =3D '\n' then puts(filenum2,ch) else ch2 =3D getc(filenum) dummy =3D decrypt({ch,ch2}) dummy2 =3D dummy2 & dummy --debug help, ignore puts(filenum2,dummy) end if end while close(filenum) close(filenum2) puts(1,"Your file has been decrypted.") done =3D wait_key() end if end procedure procedure main() sequence choices integer ch,done done =3D 0 choices =3D {"Encrypt a file","Decrypt a file","Quit"} while not done do clear_screen() for i =3D 1 to 3 do position(10+i,30) printf(1,"%d: %s",{i,choices[i]}) end for ch =3D wait_key() position(15,30) if ch =3D '1' then encrypt_file() elsif ch =3D '2' then decrypt_file() elsif ch =3D '3' then done =3D 1 end if end while = end procedure main()