How to cipher/decipher a text file in Delphi?(simple)

How to cipher/decipher a text file in Delphi? (simple)

thanks...
neoxAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
well heres an example :

implementation

{$R *.DFM}
const
  C1 = 52845;
  C2 = 22719;

procedure EncryptFile(INFName, OutFName : String; Key : Word);
VAR
   MS, SS : TMemoryStream;
   X : Integer;
   C : Byte;
begin
MS := TMemoryStream.Create;
SS := TMemoryStream.Create;
    TRY
       MS.LoadFromFile(INFName);
       MS.Position := 0;
       FOR X := 0 TO MS.Size - 1 DO
             begin
                  MS.Read(C, 1);
                  C := (C xor (Key shr 8));
                  Key := (C + Key) * C1 + C2;
                  SS.Write(C,1);
             end;
       SS.SaveToFile(OutFName);
    FINALLY
           SS.Free;
           MS.Free;
    end;
end;

procedure DecryptFile(INFName, OutFName : String; Key : Word);
VAR
   MS, SS : TMemoryStream;
   X : Integer;
   C, O : Byte;
begin
MS := TMemoryStream.Create;
SS := TMemoryStream.Create;
    TRY
       MS.LoadFromFile(INFName);
       MS.Position := 0;
       FOR X := 0 TO MS.Size - 1 DO
             begin
                  MS.Read(C, 1);
                  O := C;
                  C := (C xor (Key shr 8));
                  Key := (O + Key) * C1 + C2;
                  SS.Write(C,1);
             end;
       SS.SaveToFile(OutFName);
    FINALLY
           SS.Free;
           MS.Free;
    end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
EncryptFile('c:\autoexec.bat','c:\autoexec.txt',121);
memo1.lines.loadfromfile('c:\autoexec.txt');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
DecryptFile('c:\autoexec.txt','c:\autoexec.txt',121);
memo1.clear;
memo1.lines.loadfromfile('c:\autoexec.txt');
end;



and heres a good link to find more stuff:
http://www.efg2.com/Lab/Library/Delphi/MathFunctions/Cryptography.htm 


Regards Barry
0
 
men xinCEOCommented:
Xor then Char.

 :-)
0
All Courses

From novice to tech pro — start learning today.