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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
men xinCEOCommented:
Xor then Char.

 :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.