Encrypting/Decrypting 
Author Message
 Encrypting/Decrypting

I have created a program that uses the ascii number of a character and adds
the ascii number of the corresponing character i the key.
Anyway the program does not tell the user if the key is correct. How do I do
that and should I do that?

And I can only use MS-DOS text files. How can I use windows and non-text
files? Please help.

--
Martin Isaksson



Wed, 18 Jun 1902 08:00:00 GMT  
 Encrypting/Decrypting

Quote:

> And I can only use MS-DOS text files. How can I use windows and non-text
> files? Please help.

There is no major difference between DOS and Windows text files. Some characters
will have to be converted but they are accessed the same way from Pascal. To
access binary files, you can use any file type except for text, i.e: file, file
of byte, file of integer, etc. Depending on how you want to interpret the
content of the file.


Wed, 18 Jun 1902 08:00:00 GMT  
 Encrypting/Decrypting

Quote:

>I have created a program that uses the ascii number of a character and adds
>the ascii number of the corresponing character i the key.
>Anyway the program does not tell the user if the key is correct. How do I
do
>that and should I do that?

>And I can only use MS-DOS text files. How can I use windows and non-text
>files? Please help.

>--
>Martin Isaksson


As has already been pointed out, Martin, any file is just a bunch of digital
ones and zeros that are grouped in standard specific ways. The cyphering
method you mention for textfiles is a substition cypher that is quite old.
With computers it is particularly easy to implement. It is so easy to break
for plain text files, it sounded like a fun exercise to implement with no
key given.

I assume you do not wish to code carriage returns and line feeds but do wish
to code the spaces. That is a give away also, since there are more spaces
than any other character so just look for the most freqently used char,
which must be a space. In a textfile there are normally more alphabetic keys
than any others so you could make an array of these values and run through a
sizable portion of the file and pick the array element with the most "hits"
as the space and the next most as "e", etc.

BLOCKREAD and BLOCKWRITE Turbo Pascal look particularly useful for this
application so I will give it a shot. If it works as expected, I'll include
the short program.  4:52 PM 12/6/99

It works! 6:07 PM 12/6/99  Kind of slow for such a simple program but the
computer did what I told it to do instead of what I wanted it to. Dinner
time.

Regards,

Later--To use this just enter your short textfile to be encoded. Every byte
will be loaded into an array. A random offset is generated, the message is
encoded and placed in JUNKXYZ.DAT.  Next you enter that file as a source and
the decoded version will be placed inJUNKXYZ.TXT and showed on screen as a
plain textfile.

Program SimpleCode;

CONST   max = 5000;   { 60000+ is ok, but procedures will use the global
array variable instead of passing it as a parameter, which I will do here.}

dest = 'TESTXYZ.DAT';   plain = 'TESTXYZ.TXT';   {change as needed}

TYPE s64 = String[64]; {for filename in procedure}

VAR
A:Array[1..max] of byte;
ct, offset, total:Word;
source:s64;
ftxt:TEXT;  {optional for debug}
f:FILE;  {untyped for BLOCK commands, cannot be passed as a parameter}
tmp:Integer;
txtLine:String[80];

Procedure GetSource(VAR src:s64; VAR actual:Word);
Begin
     Repeat
         Writeln; Write('Source name (? Quits): ');  Readln(src);
         If src = '?' then exit; {prodedure, then program}
         assign(f, src);
         {$I-}     (* disables exit on IO fault *)
         Reset(f, 1);
         {$I+}
      Until IOResult = 0;

      BLOCKREAD(f, A, max, actual);   {A is global byte array}
      Close(f);
End;

Procedure FindOffset(VAR offst:Word; actual:Word);
VAR B:Array[33..255] of Byte;
        ct:Word;
Begin
     For ct := 33 to 255 Do B[ct] := 0; {clear B, then count chars}
     For ct := 1 to actual do If A[ct] > 32 then Inc(B[ A[ct] ]);

     offst := 0; ct := 32;
     Repeat {the coded spaces count will be first in the array}
          Inc(ct);
          Inc(offst);
     Until B[ct] > 0;
End;

Begin
     Writeln; writeln;
     GetSource(source, total);  {puts bytes in A}
     If source = '?' then EXIT; {program}

     RANDOMIZE;
     offset := Random(100) + 15; {arbitrary, but Ord(chr) + offset < 256}
(*** coding ***)
     For ct := 1 to total Do If A[ct] > 31 then A[ct] := A[ct] + offset;

     Assign(f, dest);
     Rewrite(f, 1);
     BLOCKWRITE(f, A, total); Close(f);
     Writeln(source, ' is coded in ', dest);
     Writeln('Offset encoded ', offset);
     Writeln('<<< Press Enter to decode >>>':40); Readln;

(******* begin decode *******)

     offset := 0;
     GetSource(source, total);
     FindOffset(offset, total);
     Writeln('Offset detected ', offset);
     Writeln('<<< Press Enter to cont. >>>':40); Readln;

     For ct := 1 to total Do
     Begin
          tmp := A[ct] - offset;    {ignore CR and LF}
          If tmp > 0 then A[ct] := A[ct] - offset;
     End;

     Assign(f, plain);
     Rewrite(f, 1);
     BLOCKWRITE(f, A, total); Close(f);

     {although stored as bytes, can be read as text}
     Assign(ftxt, plain);   {For debugging, but use DOS editor if you wish}
     Reset(ftxt);
     While not eof(ftxt) Do
     Begin
          readln(ftxt, txtLine);
          writeln(txtline);
     End;
     Close(ftxt);
readln;
End.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Encrypt\Decrypt help...

2. Encrypt\Decrypt HELP...

3. Encrypt/Decrypt

4. Encrypt/Decrypt Procedure

5. Encrypting /Decrypting ...

6. Encrypting and decrypting data

7. wanted: enc/decrypt programme

8. Encrypting text stored in inidividual database records

9. Using ADO with Encrypted/Password Protected Access databases

10. Encrypting TClientDataSet field values

11. using encrypted access database

12. Encrypting Program with PDOX tables

 

 
Powered by phpBB® Forum Software