Board index » delphi » How do i save textfiles crypted?

How do i save textfiles crypted?

Hi..
I want to save some texts in crypted form in a file. It doesnt even have
to be really crypted, but just in a non-text form , so it cant be read by
text editors.. Ive tried to save it with array of chars, records, array of
integers with the chars in ascii numbers. but it wont work, the letters appear
no matter how i save it.. I now wonder , if someone could help me.
Im not familiar with encoding/crypting, maybe you could give me a quick
lesson.A pascal program would be great to show what i should do..

My other question: How do i save both integers and chars and booleans etc..
in ONE file. If i do it with records they will be written in a prechosen
order, what if I dont want that, and I want to save space?

Im not so familiar with turbo pascal, and its the only thing i have right now,
i should get a book..I will, but what should i do when i want to use pointers
(i.e. ^) to see the next record ?

Thank you for your time! bye!

 

Re:How do i save textfiles crypted?


Quote
a...@aa.aa.aa (aaa) wrote:

Stay tuned to my tutorial.  I just posted part 2 in
comp.lang.pascal.borland.  Eventually, we will cover all of these
issues and more.

Re:How do i save textfiles crypted?


You could try to use PGP which can be found on most ftp sites

Jeroen Verhoeven

=>mb...@hi.ft.hse.nl<=
=>hy...@il.ft.hse.nl<=
=>ej...@dds.nl<=

On 12 Dec 1995, aaa wrote:

Quote
> Hi..
> I want to save some texts in crypted form in a file. It doesnt even have
> to be really crypted, but just in a non-text form , so it cant be read by
> text editors.. Ive tried to save it with array of chars, records, array of
> integers with the chars in ascii numbers. but it wont work, the letters appear
> no matter how i save it.. I now wonder , if someone could help me.
> Im not familiar with encoding/crypting, maybe you could give me a quick
> lesson.A pascal program would be great to show what i should do..

> My other question: How do i save both integers and chars and booleans etc..
> in ONE file. If i do it with records they will be written in a prechosen
> order, what if I dont want that, and I want to save space?

> Im not so familiar with turbo pascal, and its the only thing i have right now,
> i should get a book..I will, but what should i do when i want to use pointers
> (i.e. ^) to see the next record ?

> Thank you for your time! bye!

Re:How do i save textfiles crypted?


Quote
>Hi..
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..

     One possibility is to simply manipulate each character before writing it
out.  If the file is of type "text", then the most "natural" way to process it
will preserve the line structure, but you can easily scramble the characters.  
One thing to take note of, however, is that you probably will want to only
"encrypt" the printable character set, ' '..'~'.  One method of doing this
safely is to first move the characters down from the range 32 .. 126 to 0 ..
94, do some operation that is invertable (add 17, then do a mod 95), and shift
back up.

     FUNCTION encrypt (letter : char) : char;

     BEGIN   { encrypt }
      if (ord(' ') <= letter) and (letter <= ord('~'))
      then encrypt := chr (ord(' ') + ((ord(letter) - ord(' ') + 17)
                mod succ(ord('~') - ord(' ')))
      else encrypt := letter
     END;

ord(letter) - ord(' ') maps the printable characters between space and ~ into
the range 0 .. 94.  I then add 17 (or some other encoding number), keep the
answer in the range 0 .. 94 (by using the mod function, again using ord('~') -
ord(' ') to store the range, in case it really isn't 95), then adding the
space back in to shift it back into the printable character range.

Quote
>My other question: How do i save both integers and chars and

booleans etc..>in ONE file. If i do it with records they will be written in a
prechosen >order, what if I dont want that, and I want to save space?

     Depends.  If you want to save them in "random" order, you are probably
best off saving them in a text file, whereby you save the ascii representation
(i.e. your file might contain the characters "1, 53.4, true, 23, skidoo".  It
would then be up to you to read the file and decide what kind of information
is present (by looking for decimal points, reserved words such as "true",
etc.) to tell you if the data represent reals, integers, booleans, or
characters.

     On the other hand, if you KNOW your data format, and it is fixed, you can
often get some space savings by writing files of (fixed format) records.  
Depends on what your needs are.

Bob Schor
Pascal Enthusiast

Re:How do i save textfiles crypted?


Quote
In article <4ak4as$...@news.kth.se> a...@aa.aa.aa "aaa" writes:
> Hi..
> I want to save some texts in crypted form in a file. It doesnt even have
> to be really crypted, but just in a non-text form , so it cant be read by
> text editors.. Ive tried to save it with array of chars, records, array of
> integers with the chars in ascii numbers. but it wont work, the letters appear
> no matter how i save it.. I now wonder , if someone could help me.
> Im not familiar with encoding/crypting, maybe you could give me a quick
> lesson.A pascal program would be great to show what i should do..

If you save you ascii text file data as a file of byte instead, just
before you write to it, you could say, subtract 32 from each byte and
add 32 when you read it back again.

It won't be fool-proof encription, but it won't be easily human readable
either.

e-mail me if this is unclear.

Regards,

MIke Watson
--
Mayes uk

Re:How do i save textfiles crypted?


Quote
a...@aa.aa.aa (aaa) wrote:
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..

Why not just place an EOF (End Of File) character as the first character in the
file? This protection is not bullet proof, but many editors do not show the char's
after a EOF.

/Pontus

Re:How do i save textfiles crypted?


In article <4ak4as$...@news.kth.se>, a...@aa.aa.aa says...
Quote

>Hi..
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..

>My other question: How do i save both integers and chars and booleans etc..
>in ONE file. If i do it with records they will be written in a prechosen
>order, what if I dont want that, and I want to save space?

>Im not so familiar with turbo pascal, and its the only thing i have right now,
>i should get a book..I will, but what should i do when i want to use pointers
>(i.e. ^) to see the next record ?

>Thank you for your time! bye!

if you want to save different type in one file, make a record.

type dog = record
        bla:integer;
        bla:real;
        bla:whatever;
        end;

then make a file of dog...or you might have to do something like
dogrec : dog;
blabla:file of dogrec;

youll figure it out.
seeya
--
The Crow - thec...@iconn.net
"It can't rain all the time"

Re:How do i save textfiles crypted?


In article <4ak4as$...@news.kth.se>, a...@aa.aa.aa says...
Quote

>Hi..
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..

>My other question: How do i save both integers and chars and booleans etc..
>in ONE file. If i do it with records they will be written in a prechosen
>order, what if I dont want that, and I want to save space?

>Im not so familiar with turbo pascal, and its the only thing i have right now,
>i should get a book..I will, but what should i do when i want to use pointers
>(i.e. ^) to see the next record ?

>Thank you for your time! bye!

records compress amazingly, you can set up a zipping routine or something
--
The Crow - thec...@iconn.net
"It can't rain all the time"

Re:How do i save textfiles crypted?


Quote
Pontus Falk <f...@algonet.se> wrote:
> a...@aa.aa.aa (aaa) wrote:
> >I want to save some texts in crypted form in a file. It doesnt even have
> >to be really crypted, but just in a non-text form , so it cant be read by
> >text editors.. Ive tried to save it with array of chars, records, array of
> >integers with the chars in ascii numbers. but it wont work, the letters appear
> >no matter how i save it.. I now wonder , if someone could help me.
> >Im not familiar with encoding/crypting, maybe you could give me a quick
> >lesson.A pascal program would be great to show what i should do..
> Why not just place an EOF (End Of File) character as the first character in the
> file? This protection is not bullet proof, but many editors do not show the char's
> after a EOF.

A quick and dirty way of scrambling your text would be to
add 128 to each and every character when writing the file
and then subtracting 128 from each character when reading.
It's not a secure encryption technique, but will be basically
unreadable by text editors.

                        Mike

Re:How do i save textfiles crypted?


In article <4ak4as$...@news.kth.se>, a...@aa.aa.aa says...

Quote

>Hi..
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..

>My other question: How do i save both integers and chars and booleans etc..
>in ONE file. If i do it with records they will be written in a prechosen
>order, what if I dont want that, and I want to save space?

>Im not so familiar with turbo pascal, and its the only thing i have right now,
>i should get a book..I will, but what should i do when i want to use pointers
>(i.e. ^) to see the next record ?

>Thank you for your time! bye!

after you read in each character, you need to change each thing in a consistent
way.  pick a number like 30.  convert the each character of the line to an
ordinal (ord) and add 30 to it, then convert back the character (chr).  You can
also have a input keyword and use the ords of that char to save.  Even better
use writeblock and readblock and add the ords of the key to the bytes. youll
figure it out.

--
The Crow - thec...@iconn.net
"It can't rain all the time"

Re:How do i save textfiles crypted?


Quote
a...@aa.aa.aa (aaa) wrote:
>Hi..
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..
>My other question: How do i save both integers and chars and booleans etc..
>in ONE file. If i do it with records they will be written in a prechosen
>order, what if I dont want that, and I want to save space?
>Im not so familiar with turbo pascal, and its the only thing i have right now,
>i should get a book..I will, but what should i do when i want to use pointers
>(i.e. ^) to see the next record ?
>Thank you for your time! bye!

I have used the graphics characters and punctuation symbols that occur
after lower case "z", i.e ASCII 123 to 254. You can also use a
substitutional scramble of these characters instead of just adding a
constant to the normal ASCII value.

<clifp...@airmail.net

Re:How do i save textfiles crypted?


Hi.
I want to thank you all for answering my questions...bye

Re:How do i save textfiles crypted?


Quote
>If you save you ascii text file data as a file of byte instead, just
>before you write to it, you could say, subtract 32 from each byte and
>add 32 when you read it back again.

>It won't be fool-proof encription, but it won't be easily human readable
>either.

It's easier (and more full-proof) to XOR the data with a bit mask,
because if you are encrypting a carriage return which has an ascii value
of 13 decimal and you subtract 32, then you get a negative number which
isn't good, so you have to build in checks to test if the number needs to
be subtracted from 255 minus it's value.  If you're using the byte type,
i guess the value would overflow and loop like you wanted it to, but not
for other types, and if you have range checking on the program will
terminate at that point.

--
          + James Lee * Co-System Administrator: WLJSHS +
     + http://io.wl.k12.in.us/~sleej * sl...@io.wl.k12.in.us +
+ Experience with QB, TP, C, C++, 8086 ASM, OOP, UNIX Programming +

Re:How do i save textfiles crypted?


Quote
>> Hi..
>> I want to save some texts in crypted form in a file. It doesnt even have
>> to be really crypted, but just in a non-text form , so it cant be read by

You could use some very basic XOR encryption.  What you do is before you
write a character to a file, use an eXclusiveOR on it.  Like so:

{ for text files, binary is similar }
for i := 1 to Length(MyString) do
   Write(MyFile,Char(Ord(Mystring[i]) xor $FF));

This writes a string to a file XOR encrypted with a mask of $FF or 256
decimal.  Now the neat thing about XOR is that when you read data back
from the file, to get it back into human readable form, you just reXOR it
with the previous mask, in this case $FF, like so:

for i := 1 to 20 do
   begin
      Read(Ch,MyFile);
      MyString := MyString + Char(Ord(MyString[i]) xor $FF);
   end;

email me if you have any questions.

--
          + James Lee * Co-System Administrator: WLJSHS +
     + http://io.wl.k12.in.us/~sleej * sl...@io.wl.k12.in.us +
+ Experience with QB, TP, C, C++, 8086 ASM, OOP, UNIX Programming +

Re:How do i save textfiles crypted?


Quote
> Hi..
> I want to save some texts in crypted form in a file. It doesnt even have
> to be really crypted, but just in a non-text form , so it cant be read by
> text editors.. Ive tried to save it with array of chars, records, array of
> integers with the chars in ascii numbers. but it wont work, the letters appear
> no matter how i save it.. I now wonder , if someone could help me.
> Im not familiar with encoding/crypting, maybe you could give me a quick
> lesson.A pascal program would be great to show what i should do..

Here try this. you will have to make modifications.

Const
     add_1=23;
     add_2=45;
var
   s,sn,num,numn,x:string;

Procedure enc(var s1,s2:String);
var
   i,q:byte;
Begin
     for i:=1 to byte(s1[0]) do
      Begin
           byte(s2[i]):=byte(s2[i])+byte(s1[i]);
      end;
     for i:=1 to byte(s1[0]) do
      begin
           inc(s1[i],i-add_1);
      end;
     for i:=1 to byte(s2[0]) do
      begin
           dec(s2[i],i+add_2);
      end;
     q:=0;
     for i:=1 to byte(s1[0]) do
      Q:=BYTE(s1[i])+q;
     for i:=1 to byte(s2[0]) do
      Q:=BYTE(s2[i])+q;
     writeln('CRC redemption value is :>',q,'<');
     for i:=1 to byte(s1[0]) do
      inc(BYTE(s1[i]),q);
     for i:=1 to byte(s2[0]) do
      inc(BYTE(s2[i]),q*i);

End;

Procedure de_enc(var s1,s2:String);
var
   i,q:byte;
Begin
     Write('Enter CRC redemption value :>');
     ReadLn(q);
     for i:=1 to byte(s1[0]) do
      dec(BYTE(s1[i]),q);
     for i:=1 to byte(s2[0]) do
      dec(BYTE(s2[i]),q*i);
     for i:=1 to byte(s2[0]) do
      begin
           inc(s2[i],i+add_2);
      end;
     for i:=1 to byte(s1[0]) do
      begin
           dec(s1[i],i-add_1);
      end;
     for i:=1 to byte(s1[0]) do
      Begin
           byte(s2[i]):=byte(s2[i])-byte(s1[i]);
      end;
end;

Procedure out_num(s:string);
var
   i:byte;
Begin
     for i:=1 to byte(s[0]) do
      write(byte(s[i]):4);
     writeln;
end;

Begin
     repeat
     write('Line 1>');Readln(s);
     write('Line 2>');Readln(num);

     enc(s,num);

     WriteLn(s);
     WriteLn(num);
     {out_num(s);
     out_num(num);}
     readln;

     repeat
     sn:=s; numn:=num;
     de_enc(sn,numn);

     WriteLn(sn);
     WriteLn(numn);
     readln(x);
     until x='x';
     readln(x);
     until x='x';
end.
    /::::::::::::::::::::::::::::::::::::::::/
   / Jason Pyeron :{)    | Every thing in   /
  / Radio Jammer :{)     | life is free    /
 / JPye...@HofLink.com   | if you belive! /
/::::::::::::::::::::::::::::::::::::::::/

Go to page: [1] [2]

Other Threads