Board index » delphi » Help with enycription??

Help with enycription??

How do i do it?

im using T.Pascal 7 (Dos ver)

i need to store the password in a record in a file and make it so that it
can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

I don't know where to start :-(

Thanx in advance
Andy

 

Re:Help with enycription??


Quote
Ardener wrote in message <8civ2m$qt...@plutonium.btinternet.com>...
>How do i do it?

>im using T.Pascal 7 (Dos ver)

>i need to store the password in a record in a file and make it so that it
>can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

>I don't know where to start :-(

>Thanx in advance
>Andy

Andy,

You might use the trusty old "exclusive or" trick:

 for i := 1 to length(password) do password[i] := chr(ord(password[i]) xor
key)

where key is a number between 1 and 255.

It has two advantages: It's very simple, and the decoding process is
identical
to the encoding process. It's drawback is that it's easily cracked.

KlausL

Re:Help with enycription??


Quote
Ardener wrote:
> i need to store the password in a record in a file and make it so that it
> can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

> I don't know where to start :-(

You can use simple xor-encryption. Simple, because it isn't very secure and
because no complicated decryption algorithm is required to transform an
encrypted string back to its unencrypted form.

First, you must choose a key. That will be typically be a series of
bytes/characters, I suggest that you start with one character for the sake of
simplicity. The formula for encrypting will be:

encrypted := decrypted xor key

where "encrypted", "decrypted" and "key" are of type char. You will have to
do a type cast from char to byte, e.g.:

encrypted := char(byte(decrypted) xor byte(key))

It's not elegant, I know. As mentioned above, a handy feature of
xor-encryption is the easy decryption algorithm:

decrypted := encrypted xor key.

To encrypt or decrypt a string, xor each character of the string with the key
using a for-loop.

Re:Help with enycription??


Hi,

on Thu, 06 Apr 2000 at 21:20:35 o'clock, Ardener wrote:

Quote
> i need to store the password in a record in a file and make it so that it
> can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

> I don't know where to start :-(

Ok, let's assume (you mentioned earlier today that you are working on
a college project) that security is *not* critical; in other words, it
doesn't matter if someone manages to break the code. In this case a
simple algorithm will suffice.

One of the most simple algorithms is to shift every letter by a fixed
number. For example, A -> D, B -> E, and so on. At the end of the
alphabet (or of the ASCII range), the shift rolls over so that Z -> C.
An example of this is the Rot13 algorithm that is sometimes used to
hide informations in Usenet postings, like for example movie spoilers:

   Ng gur raq bs "Gvgnavp", gur fuvc fvaxf.

Most newsreaders have a "Rot13" button that will make the text legible.

To implement such an algorithm in Pascal, you have to get a numerical
representation of characters, which is possible by using the Ord and
Chr functions. An example:

   Write('> ');
   c := ReadKey;
   Write(c, ' -> ');
   c := Chr(Ord(c) + 3);
   Writeln(c);

A Rot13 implementation can be found in Timo Salmi's faqpas6.txt.
To improve security, the algorithm should not shift every letter by
the same number. Instead, you could use a longer key, such as:

   19 5 13 6 1 ...

In this case, the first letter is shifted by 19, the second by 5, and
so on. (If the key is too short for the plain text, repeat it).
Another common algorithm uses binary XOR on the plain text and the
key. The advantage of this is that the same algorithm can be used to
decrypt the text.

Please note that these algorithms do not offer security against
persons more experienced than the often-cited "little sister".

 - Sebastian

Re:Help with enycription??


Quote
Ardener wrote in message <8civ2m$qt...@plutonium.btinternet.com>...
>How do i do it?

>im using T.Pascal 7 (Dos ver)

>i need to store the password in a record in a file and make it so that it
>can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

>I don't know where to start :-(

>Thanx in advance
>Andy

Well the store in file part you should know.
But here is a veiw ideas on the incryption:
 Firstly you should know that the string is an array so
 to get charachter n of the password you would use:  password[n]

Ok :
 To change the order of the array wouldn't be to difficult.
 For example continue to take the last and then the first charachter until
and put it in a new string.
To find the original password just reverse the process.

Onother and maybe better way is to get the ascii code "ord" of the
charachters.
And apply some or other mathematical operation to them.
And then convert them back to charachters. Or just store the numbers.

Chaou
Heinrich

Check my web-site:
http://homes.arealcity.com/Heinrichshomepage/
Includes a complete TP tutoriul and more!

Re:Help with enycription??


Ta for help

Re:Help with enycription??


On Thu, 6 Apr 2000 22:20:35 +0100, "Ardener" <arde...@btinternet.com>
wrote:

Quote
>How do i do it?

>im using T.Pascal 7 (Dos ver)

>i need to store the password in a record in a file and make it so that it
>can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

>I don't know where to start :-(

>Thanx in advance
>Andy

Here is one way without an XOR. XORing to a textfile can send
undesired control chars. Works fine for a file of bytes or untyped
file. Here there is no char < Chr(32).

Program Encryption;
{Not knowing your intended application, here is a demo program you can
modify for MANY variants. You assign an array that is as long as any
intended pass word/phrase. This array is initialized as A[1] := 1,
A[2] := 2....etc. The built in random number generator is initialized
with a key seed so that the sequence of random numbers will be the
same each time. The array is "shuffled" in a deterministic manner. The
word(s) to be cyphered use this array to both transpose the chars as
well as to substitute other chars. No char < Chr(32) (control chars).
Turbo v6.0   <clifp...@airmail.net>  April 9, 2000 }

CONST
seed = 12345;   {arbitrary, different values change code}
max = 80;       {max length of pass word/phrase}
dest = 'TESTWXYZ.TXT'; {to insure this encryption works with a
textfile.}

TYPE  arry = Array[1..max] of Byte;
      s80 = String[80];
VAR
f:Text;    {dest is created and erased}
target, source, mid:s80;
ct, len:Byte;
A:arry;

Procedure SwapByte(VAR a, b:Byte);
VAR c:Byte;
Begin
     c := a;   a := b;   b := c;
End;

Procedure ShuffleArray(len:Byte; VAR aa:arry);
VAR ct, j, k:Byte;
Begin
     RandSeed := seed;
     For ct := 1 to len Do  aa[ct] := ct;  {initialization}

     For ct := 1 to 3 * len Do  {shuffle, 3* is arbitrary, but works
well}
     Begin
          j := Random(len) + 1;
          Repeat
                k := Random(len) + 1
          Until j <> k;
          SwapByte(aa[j], aa[k]);
     End;
End;

Procedure Cypher(x:s80; VAR z:s80; aa:arry; op:Integer);
{x--chars to be ciphered,  z--chars after ciphering, op is +/-1
Unlike an XOR method, this insures there are no chars < Chr(32). Such
control chars may not store properly as text.}

VAR j, k:Byte;
Begin
     z := x;
     For j := 1 to Length(x) Do   {+1 for coding, -1 for decoding}
          z[j] := Char(Ord(x[j]) + (op * aa[j]));
End;

BEGIN
     Writeln; Writeln; Writeln('QUIT BY JUST PRESSING <Enter>':50);
Repeat
     Writeln; Write('Input: ');  Readln(source);
     If source = '' then EXIT;
     Writeln;  Writeln('ORIGINAL TO CODED':40);
     len := Length(source);

     ShuffleArray(len, A);
     Cypher(source, mid, A, 1);
     target := mid;
     For ct := 1 to len Do target[ct] := mid[ A[ct] ];  {transpose}
     Writeln(source);   Writeln(mid);   Writeln(target);   Writeln;
               {store coded target in file}
     Assign(f, dest);  Rewrite(f);   Writeln(f, target);   Close(f);

(************ END CODING BEGIN DECODING ******************)

     For ct := 1 to max Do A[ct] := 0;   {reset variables}
     source := '';  mid := '';

     Writeln; Writeln('CODED TO ORIGINAL':40);
     Assign(f, dest);  Reset(f);  Readln(f, target);  Close(f);
Erase(f);
     Writeln(target);
     len := Length(target);

     ShuffleArray(len,A);
     mid := target;
     For ct := 1 to len Do mid[ A[ct] ] := target[ct] ;
{un-transpose}
     Writeln(mid);
     Cypher(mid, source, A, -1);
     Writeln(source);
Until source = '';
END.

Quote

Re:Help with enycription??


On Thu, 6 Apr 2000 22:20:35 +0100, "Ardener" <arde...@btinternet.com>
wrote:

Quote
>How do i do it?

>im using T.Pascal 7 (Dos ver)

>i need to store the password in a record in a file and make it so that it
>can't be viewed as it is eg. HELLO stored as  $%" or OLELH.

>I don't know where to start :-(

>Thanx in advance
>Andy

Here is one way without an XOR. XORing to a textfile can send
undesired control chars. Works fine for a file of bytes or untyped
file. Here there is no char < Chr(32).

Program Encryption;
{Not knowing your intended application, here is a demo program you can
modify for MANY variants. You assign an array that is as long as any
intended pass word/phrase. This array is initialized as A[1] := 1,
A[2] := 2....etc. The built in random number generator is initialized
with a key seed so that the sequence of random numbers will be the
same each time. The array is "shuffled" in a deterministic manner. The
word(s) to be cyphered use this array to both transpose the chars as
well as to substitute other chars. No char < Chr(32) (control chars).
Turbo v6.0   <clifp...@airmail.net>  April 9, 2000 }

CONST
seed = 12345;   {arbitrary, different values change code}
max = 80;       {max length of pass word/phrase}
dest = 'TESTWXYZ.TXT'; {to insure this encryption works with a
textfile.}

TYPE  arry = Array[1..max] of Byte;
      s80 = String[80];
VAR
f:Text;    {dest is created and erased}
target, source, mid:s80;
ct, len:Byte;
A:arry;

Procedure SwapByte(VAR a, b:Byte);
VAR c:Byte;
Begin
     c := a;   a := b;   b := c;
End;

Procedure ShuffleArray(len:Byte; VAR aa:arry);
VAR ct, j, k:Byte;
Begin
     RandSeed := seed;
     For ct := 1 to len Do  aa[ct] := ct;  {initialization}

     For ct := 1 to 3 * len Do  {shuffle, 3* is arbitrary, but works
well}
     Begin
          j := Random(len) + 1;
          Repeat
                k := Random(len) + 1
          Until j <> k;
          SwapByte(aa[j], aa[k]);
     End;
End;

Procedure Cypher(x:s80; VAR z:s80; aa:arry; op:Integer);
{x--chars to be ciphered,  z--chars after ciphering, op is +/-1
Unlike an XOR method, this insures there are no chars < Chr(32). Such
control chars may not store properly as text.}

VAR j, k:Byte;
Begin
     z := x;
     For j := 1 to Length(x) Do   {+1 for coding, -1 for decoding}
          z[j] := Char(Ord(x[j]) + (op * aa[j]));
End;

BEGIN
     Writeln; Writeln; Writeln('QUIT BY JUST PRESSING <Enter>':50);
Repeat
     Writeln; Write('Input: ');  Readln(source);
     If source = '' then EXIT;
     Writeln;  Writeln('ORIGINAL TO CODED':40);
     len := Length(source);

     ShuffleArray(len, A);
     Cypher(source, mid, A, 1);
     target := mid;
     For ct := 1 to len Do target[ct] := mid[ A[ct] ];  {transpose}
     Writeln(source);   Writeln(mid);   Writeln(target);   Writeln;
               {store coded target in file}
     Assign(f, dest);  Rewrite(f);   Writeln(f, target);   Close(f);

(************ END CODING BEGIN DECODING ******************)

     For ct := 1 to max Do A[ct] := 0;   {reset variables}
     source := '';  mid := '';

     Writeln; Writeln('CODED TO ORIGINAL':40);
     Assign(f, dest);  Reset(f);  Readln(f, target);  Close(f);
Erase(f);
     Writeln(target);
     len := Length(target);

     ShuffleArray(len,A);
     mid := target;
     For ct := 1 to len Do mid[ A[ct] ] := target[ct] ;
{un-transpose}
     Writeln(mid);
     Cypher(mid, source, A, -1);
     Writeln(source);
Until source = '';
END.

Other Threads