1. Data Encryption

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()

new topic     » topic index » view message » categorize

2. Re: Data Encryption

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()

new topic     » goto parent     » topic index » view message » categorize

3. Re: Data Encryption

I think your method could be duplicated with a simple xor function. a crude
example follows.

note that in this example multiple bits are switched out rather than just one


------------------------encrypt.ex:
include machine.e
include get.e

sequence filein, fileout, buffer, ext
atom key,char,stroke,done

puts(1,"enter file to encrypt: ")
filein={"",0}
done=0
while 1 do
    stroke=wait_key()
        if stroke!=13 then
            puts(1,{stroke})
            filein[1]=filein[1]&stroke
        else
            exit
        end if
end while

filein[2]=open(filein[1],"r")
if filein[2]=-1 then
    puts(1,"invalid file name")
    stroke=wait_key()
    abort(0)
end if
fileout={"",0}
for p=1 to length(filein[1]) do
    if filein[1][p]='.' then
        fileout[1]=filein[1][1..p]&"hid"
        ext=filein[1][p+1..length(filein[1])]
        exit
    elsif p=length(filein[1]) then
        fileout[1]=filein[1][1..p]&".hid"
        ext={}
    end if
end for
fileout[2]=open(fileout[1],"w")

key=rand(255)

buffer={key}&length(ext)&ext
while 1 do
char=getc(filein[2])
    if char=-1 then
        exit
    elsif char='\n' then
        buffer=buffer&'\n'
    else
        buffer=buffer&xor_bits(key,char)
    end if
end while
puts(fileout[2],buffer)

close(filein[2])
close(fileout[2])



---------------------------------decrypt.ex:
include machine.e
include get.e

sequence filein, fileout, buffer, ext
atom key,char,stroke

puts(1,"enter file to decrypt: ")
filein={"",0}
while 1 do
    stroke=wait_key()
        if stroke !=13 then
            puts(1,{stroke})
            filein[1]=filein[1]&stroke
        else
            exit
        end if
end while

filein[2]=open(filein[1],"r")
if filein[2]=-1 then
    puts(1,"invalid file name")
    stroke=wait_key()
    abort(0)
end if
key=getc(filein[2])
char=getc(filein[2])
ext={}
if char>0 then
    ext="."
    for p=1 to char do
        ext=ext&getc(filein[2])
    end for
end if
fileout={filein[1][1..length(filein[1])-4]&ext,0}
fileout[2]=open(fileout[1],"w")

buffer={}
while 1 do
char=getc(filein[2])
    if char=-1 then
        exit
    elsif char='\n' then
        buffer=buffer&'\n'
    else
        buffer=buffer&xor_bits(key,char)
    end if
end while
puts(fileout[2],buffer)

close(filein[2])
close(fileout[2])

new topic     » goto parent     » topic index » view message » categorize

4. Re: Data Encryption

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

I'll take a look at it. I have made one that uses password encryption. I
figure that if you use several passwords, and decrypt with the WRONG
password at least once, the file would be impossible to crack, especially
if I make a couple of changes to the basic algorithm. It's fast too.

This is how mine works:

It gets and confirms the password
It performs calculations on the password (adding the ASCII values up and
such and a little bit more) to get a number.
Seeds the randomizer to the number (set_rand(number))
Takes each character in the file, and adds a random number between 1 and
255 to it and puts it into the output file. (or subtracts in decryption)

Since it seeds the randomizer, the rand() results will always be the same
for each different password, but you have to know the password (or at
least a password that would give you the same number) in order to decrypt
it. (And you will need the program if I change a little bit that happens
in the main loop, since you won't know exactly what the formula is)

>  (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!)

Cool! That's a lot of competition. I got 1st at the C++ at the Great
Computer Challenge at ODU (i'm a senior in high school :).. But there
were only 5 total teams. The coolest part was beating New Horizons
Government School, who had won the last 3 years. They had 5 people on
their team. We had two, and it was our first time. :) We finished all
four programs (we weren't supposed to have enough time to do that.... :),
and 3 of them had a few bugs (not glitches! :) and grammar errors...
Imagine how well we could have done if we could have used Euphoria! ;)
(We did have Windows NT, so that sorta provided at least some subscript
checking for us....)

I recreated each program in about an hour, I think, in Euphoria at home.
Although I did know what we did and all after then, but I know we could
have done even better with Euphoria.. :)

_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

new topic     » goto parent     » topic index » view message » categorize

5. Re: Data Encryption

I too am a member of the encryption club (it is a relatively simple thing to
code). There are a couple of things I would like to add, the first is the
power of matrix encryption. This makes the messed up file completely
indistinguishable from the original, as the bytes no longer correspond. The
second is rot (rotate) type encryption. The usefullness of this is that you
can encode a text file and it will still be text. This allows you to paste it
straight into an email (rather than using something like BinHex or Base64). Of
course you could BinHex a Matrix encrypted file, but I find that a bit cheesy.
I have not perfected the rot part of my alg. It works but would still be no
good for email (ASCII values > 128). I have got the necessary Matrix code if
anybody wants it. (Multiply, Calculate Inverse, Determinate).

"School was brought in to impart on our children a new understanding. An
understanding so new that nobody has even discovered yet." - Les Mailles

Daniel

new topic     » goto parent     » topic index » view message » categorize

6. Re: Data Encryption

Salutations,

>I have recently gotten into data encryption algorithms, and just to
>see, I have developed a *very* basic one,

Not as "basic" as the one I did.  Rather than manipulate the characters
on a binary level, I just added a certain integer to each ascii code and
if it ened up more than 256 then I subtracted 256 from it or something
like that.

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

Like some one else said, watch out for the "\n" character.
I didn't have to worry about that in my prog.


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

Yes it would, keep working on it.  You'll figure it out.
If you want to check my proggie out it is on the recent user
contributions page in the "graphics bundle" by Lewis Townsend :)
It works for both typing and text files as long as the characters are
within 256 chars :)


>  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!)

Cool, I wish my high school would have participated in cool contests
like that.  I guess I was lucky to have been in art contests they were
fun and sometimes we got to go outside and draw :)

Sincerely,
Lewis Townsend
|\      ____ _     _     _ _    __
| \    | __/ ||   / |   // || / __ \
|  \   ||_   ||  //||  //  || ||__\|
|   \  | _|  || // || //   || \___ \
| |\ \ ||__  ||//  ||//    || |\__||
| | \ \|___\ |_/   |_/     || \____/
| |  \ \      _____    ________
| |   \ \     | __ \  | __  __ |
| |    \ \    ||__||  |/  ||  \|
| |     \ \   | __ /      ||
| |______\ \  ||  \\      ||
|___________\ ||  ||      ||
Keroltarr at hotmail.com

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

7. Re: Data Encryption

Thank you to all who helped me out.  I'm still getting that
same bug, though!!  Ralf, I tried your suggestion of "rb"
instead of "r", but the error keeps occurring.  And, I know about
the "int_to_bits" and "bits_to_int" and "not_bits" functions,
but I originally started this program in Pascal for school,
which does not have those functions.  Therefore, I figured
them out myself, and implemented them in this code to make
sure they worked, which they do.  I just don't understand it...
Isn't technology wonderful? smile  Well, I'll continue to work on it,
and if anyone has given this a test run, maybe fill me in?
I'd appreciate any help!!  Thanks for your time!

Regards,
  Bryan Watts

new topic     » goto parent     » topic index » view message » categorize

8. Re: Data Encryption

I did an encrypter a while back.

It does the following.

        1) Expands the code into a 256 "ramdom" character string
        2) Loads and encrypts data from files in blocks
        3) The exact length of the blocks is determined by a
           checksum derived from the code string
        4) Starts encrypting at a derived point in each block
           XORing each byte with a character from the code
           string and a number from rand() and the value of the
           next byte *before* it is in turn encrypted (except
           for the 'last' byte, which could be any byte in the
           block) It continues encrypting forward until the end
           of the block then starts from the beginning of the
           block and works up to the 'first' byte

        Reasonable security at 15MB per minute, re-writing the
        output to a seperate file (P-120)

Graeme


----------------------------------------
Got to do one of those signiture thingys
----------------------------------------

new topic     » goto parent     » topic index » view message » categorize

9. Re: Data Encryption

I did one of these ages ago. I ported it from Qbasic to a language called
"MoonRock", and then to Euphoria, but I can't think why the source isn't
on my web page.

The .COM file produced by MoonRock is lurking on my DOS webpage under the
heading ENCODE v1.1. It even comes with documentation (which happens to
be longer than the .COM file). It's a piece of my coding history, 'cause
it's still got my old "company" name (CresSoft) in it!
Mental note: Upgrade it before the real CresSoft get a hold of me!

I'll post to the list when I put the Euphoria source (which is probably
very similar to one I've already seen in this thread) in my web Euphoria
directory.

--
Carl R White
E-mail...: cyrek- at -bigfoot.com              / Remove the hyphens before
Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering...
Url......: http://www.bigfoot.com/~cyrek/

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu