I have a project at school in which I have decieded to make an encryption system which I am struggling with quite a bit at the moment - just finding resources to use and even a guide to follow.

The only half decent thing i can find on the web is the code that I will put at the bottom of the post.

Can someone point me in the right direction to a guide or just something to help me get started? If that is not possible, I would be so grateful if someone could possibly write a short but detailed description of what it going on in the below code.

Thanks so much if you can help!

{ Compiler TP/BP7 }

{$A+,B-,D-,E-,F-,G+,I-,L-,N+,O-,P-,Q-,R-,S-,T-,V-,X+}
{$M 16384,0,655360}

var f_in,f_out:file;                { Files as untyped                }
    f_in_name,f_out_name,pwd:string;{ File names                      }
    pwd_value,bytes_read:word;      { Password checksum, read counter }
    i:word;                         { Counter                         }
    buffer:array[0..16383] of byte; { 16 kByte buffer for data        }

begin
 pwd_value:=0;                      { Init password checksum with 0   }
 write('Enter filename to encrypt/decrypt: ');readln(f_in_name);
 write('Enter filename to decrypt/encrypt: ');readln(f_out_name);
 write('Enter password: ');readln(pwd);
 for i:=1 to length(pwd) do begin { Calculate BSD checksum of the password  }
  pwd_value:=(pwd_value shr 1) + ((pwd_value and 1) shl 15); { Parity bit   }
  inc(pwd_value,ord(pwd[i]));     { Increment password checksum             }
  pwd_value:=pwd_value and $ffff; { Keep it whitin bounds                   }
 end;
 randseed:=pwd_value;             { Init random seed with password checksum }
 assign(f_in,f_in_name);reset(f_in,1); { Open input file for read           }
 if ioresult<>0 then begin        { Check if input file exists              }
  writeln(f_in_name,' not found, exiting.');
  halt;
 end;
 assign(f_out,f_out_name);rewrite(f_out,1);{ Create output file             }
 while not(eof(f_in)) do begin
  blockread(f_in,buffer,16384,bytes_read); { Read a chunk for input file    }
  for i:=1 to bytes_read do               { Encrypt or Decrypt (Same thing) }
   buffer[i]:=buffer[i] xor byte(random(256));{ Simple XOR encryption       }
  blockwrite(f_out,buffer,bytes_read);   { Output encrypted/decrypted data  }
 end;
 close(f_in);close(f_out);               { Close both files                 }
end.

Recommended Answers

All 4 Replies

What exactly do you need explained? It uses a very basic XOR mechanism.

Ok, I have spent some time trying to understand it all and I am left with a number of points that I really need explaining in order to fully understand what is actually happening and why; if you or anyone can help me here, I would be very grateful, even if you can only answer one of the questions it really does help, I am struggling to find anything on this anywhere.

1.  pwd_value:=(pwd_value shr 1) + ((pwd_value and 1) shl 15); { Parity bit   }

So it is shuffing the bits to the right in the first section, why is it doing this? The whole next bit baffles me though, I did not know you could use 'and' in this context, what is it doing? Finally, surely most things shl 15 is going to be 1? What is this achieving? Help!!

2. inc(pwd_value,ord(pwd[i])); { Increment password checksum }

So it is incrementing what it has just calculated by the ascii value of the letter it is on in the for loop, why is it doing this? What does it achieve?

3. pwd_value:=pwd_value and $ffff; { Keep it whitin bounds } 

I really need to be told what and does is this context because this just does not make sense at the moment.

4.  randseed:=pwd_value;             { Init random seed with password checksum }

So I can assume the whole code before this has been calculating one big interger value that has just been assigned to this randseed? Am I right?

  1. I really need lines 30 to 34 explaining
    So it is writing a new file, whilst placing something in a buffer and using XOR on them, what an earth is happening here?

So I know this is alot to be asking, but if anyone can help me at all I would be forever grateful, this is something I really want and need to understand. Even if you don't answer above, just a piece of pseudocode or anything that can help me is really appreciated.

It is just creating a reproducible encryption method using xor and shifts. It is programmed this way because that's what the author wanted to use. You explain the code just fine, I have no true answer as to why.

Can anyone else give some insight to some of the questions I have? It would really help me out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.