Board index » delphi » Encrypting a text file

Encrypting a text file

Hi,

Does anyone know ehre I can get code/component (code would be better) that
will encrypt and decrypt a file (text file, but not the .txt extension) and
WILL NOT add other characters to the decrypted file?

I have a componenet and it encrypts fine, but when it decrypts a file with
more that one line it adds random characters (eg, ({ and other non-keyboard
characters).

Any help?

Regards,
Nick

 

Re:Encrypting a text file


A simple way is to XOR the characters of the text with a secrete string,
repeated all along the text.

if your text is 'This is the text i want to be encrypted'
and if the key string is 'My special secret string',
the characters (bytes) of the resulting string (or array of bytes) are
'T xor 'M' = 84 xor 77 = 25
'h xor 'y'   = 104 xor 121 = 17
'i  xor ' '  = 105 xor 32 = 73
's' xor 's' = .....
' ' xor  'p'
'i' xor 'e'
's' xor 'c'
.....

To decode, apply exactly the same method, of course with the same key string
:
25 xor 'M' = 84 = T
17 xor 121 = 104 = h
.....

use ord(text[n]) to get the numerical value of the characters and apply the
XOR operation.

The text is completly modified, but with no extra character.
This is simple and quick to implement.

Denis

Re:Encrypting a text file


What's happening is this: your encryption algorithm is not coping with
the <CRLF> chars at the end of your lines.

Why not try this - a simple, single, secret-key system:
 const MYSECRETKEY = 'This is my secret Key';
       MAXASC      = 255
       MINASC      = 0
var
   PlainData : string;
   EncodedData : string;

procedure encode;
var
   x,y,z : integer;
begin
   EncodedData := '';
   y := 1;
   for x := 1 to length(PlainData) do
   begin
     z := ord(PlainData[x]) + ord(MYSECRETKEY[Y]);  // ADD plain char &
Key char
     if (z > MAXASC) then     // wrap-around if result > MAXASC
         z := z - MAXASC + MINASC;    
     EncodedData[x] := chr(z);  
     if (y = length(MYSECRETKEY)) then
         y := 1       // start at beginning of key again
     else
         inc(y);     // next char of key
  end;
  SetLength(EncodedData,pred(x));
end;

procedure Decode;
var
  x,y,z : integer;
begin
    PlainData := '';
    y := 1;
    for x := 1 to length(EncodedData) do
    begin
        z := ord(EncodedData[x]) - ord(MYSECRETKEY[y]); // SUBTRACT key
char from encoded char
        if (z < 0) then        // wrap-around if result < 0
            z := z + MAXASC - MINASC ;
        PlainData[x] := chr(z);
        if (y = length(MYSECRETKEY)) then
            y := 1    // start at beginning of key again
        else
           inc(y);   // next char of key
    end;  
    SetLength(PlainData,pred(x);
end;

The above will encrypt one string from another - the longer your secret
key, the better the encryption.
The encoded result is literally "alphabet soup", & handles *all* ascii
chars.
If you wish to keep the encrypted result in "readable" ASCII, then set
MINASC = 31 & MAXASC = 127, but control chars may not work too well.
You can apply the above logic to a TFileStream & encode/decode on the
fly.
Good Luck!  Mike Nixon.

Quote
Nick Wiseman wrote:

> Hi,

> Does anyone know ehre I can get code/component (code would be better) that
> will encrypt and decrypt a file (text file, but not the .txt extension) and
> WILL NOT add other characters to the decrypted file?

> I have a componenet and it encrypts fine, but when it decrypts a file with
> more that one line it adds random characters (eg, ({ and other non-keyboard
> characters).

> Any help?

> Regards,
> Nick

--
Mike Nixon                     w] (27)(21)4103111
Systems Analyst                c] 082-800-7113
Buycom Computer Services   email] nix...@dietrich.co.za
Capetown                   :-)

Re:Encrypting a text file


I have D4 implementations of several strong encryption algorithms (RC5, RC6,
IDEA, Blowfish) which could suit your purposes. It even allows you to encrypt
while writing to a stream or decrypt while reading from the stream
(transparently to the program/procedure that uses the stream class).

Since it uses some of the new features of D4, if you're using older versions,
there might be some small editing involved to get it to compile/work. I'm
working on a D3 fix right now, so let me know what you want/need, if anything.

Quote
Nick Wiseman wrote:
> Hi,

> Does anyone know ehre I can get code/component (code would be better) that
> will encrypt and decrypt a file (text file, but not the .txt extension) and
> WILL NOT add other characters to the decrypted file?

> I have a componenet and it encrypts fine, but when it decrypts a file with
> more that one line it adds random characters (eg, ({ and other non-keyboard
> characters).

> Any help?

> Regards,
> Nick

--
Lasse V?gs?ther Karlsen
lasse.karl...@stavanger.online.no
http://home.sol.no/~lassevk/

Re:Encrypting a text file


Quote
On Wed, 26 Aug 1998 19:59:30 +0200, "Denis Jeanroy" <denj...@hrnet.fr> wrote:

I have been trying to build encryption into something I am writing at the
moment but I want to encrypt data files (ZIPs, DOCs, any type of file) in a
Delphi program but then decode the data back again in C. Any ideas to further
the solutions offered here?

The attempts I have made encrypt a file fine and decrypt OK in Delphi, but the
C does not bring the file back exactly. (Plain ASCII chars seem to be decoded
fine, but binary / control chars do not pass back through the decryption
process correctly and the returned file is not valid).

<TIA>

Quote
>A simple way is to XOR the characters of the text with a secrete string,
>repeated all along the text.

>if your text is 'This is the text i want to be encrypted'
>and if the key string is 'My special secret string',
>the characters (bytes) of the resulting string (or array of bytes) are
>'T xor 'M' = 84 xor 77 = 25
>'h xor 'y'   = 104 xor 121 = 17
>'i  xor ' '  = 105 xor 32 = 73
>'s' xor 's' = .....
>' ' xor  'p'
>'i' xor 'e'
>'s' xor 'c'
>.....

>To decode, apply exactly the same method, of course with the same key string
>:
>25 xor 'M' = 84 = T
>17 xor 121 = 104 = h
>.....

>use ord(text[n]) to get the numerical value of the characters and apply the
>XOR operation.

>The text is completly modified, but with no extra character.
>This is simple and quick to implement.

>Denis

Re:Encrypting a text file


The C should not treat the characters differently.
If you used the XOR method i suggested, a problem occurs if the character to
encode is the same as the coding one, because the XOR operator gives a null
byte. So you get an unexpected end of string in C, or in Delphi with AZT
strings.
That's why it's better to treat text or binary data as a byte array, not as
a string.
At the end, when the text (if is is text) is uncrypted, you can copy the
bytes in a string or text file, when bytes are Ascii characters back.

Denis

Re:Encrypting a text file


Quote
On Sun, 30 Aug 1998 23:40:39 +0200, "Denis Jeanroy" <denj...@hrnet.fr> wrote:
>The C should not treat the characters differently.
>If you used the XOR method i suggested, a problem occurs if the character to
>encode is the same as the coding one, because the XOR operator gives a null
>byte. So you get an unexpected end of string in C, or in Delphi with AZT
>strings.
>That's why it's better to treat text or binary data as a byte array, not as
>a string.
>At the end, when the text (if is is text) is uncrypted, you can copy the
>bytes in a string or text file, when bytes are Ascii characters back.

>Denis

This is exactly the problem I am having. The C will not write the null bytes
back out. I have attempted to skip encrypting every null byte of the
unencrypted file during the encryption phase in the Delphi code, so as to leave
the chars intact and un-changed. The decryption in C is spot on apart from the
null bytes it encounters within the encrypted file - when it spots those it
just skips them and I am left with a corrupted file.

How can I get the C code to write these null bytes back out? (I'm relatively
new to C, so please excuse my lack of knowledge here)! You mention a BYTE
array? Could it be something like:

BYTE *TheBuffer;
TheBuffer = ( BYTE * ) calloc( 128, sizeof( BYTE ) );

..Then manipulating, reading and writing using the BYTE buffer?

* I've been using wsprintf with the %c qualifier.

Thanks for your reply.

Re:Encrypting a text file


I have D4 implementations of several strong encryption algorithms (RC5, RC6,
IDEA, Blowfish) which could suit your purposes. It even allows you to encrypt
while writing to a stream or decrypt while reading from the stream
(transparently to the program/procedure that uses the stream class).

Since it uses some of the new features of D4, if you're using older versions,
there might be some small editing involved to get it to compile/work. I'm
working on a D3 fix right now, so let me know what you want/need, if anything.

Quote
Nick Wiseman wrote:
> Hi,

> Does anyone know ehre I can get code/component (code would be better) that
> will encrypt and decrypt a file (text file, but not the .txt extension) and
> WILL NOT add other characters to the decrypted file?

> I have a componenet and it encrypts fine, but when it decrypts a file with
> more that one line it adds random characters (eg, ({ and other non-keyboard
> characters).

> Any help?

> Regards,
> Nick

--
Lasse V?gs?ther Karlsen
lasse.karl...@stavanger.online.no
http://home.sol.no/~lassevk/

Other Threads