Re: encryption with Delphi


[ DelphiLand FAQ ]

Posted by webmaster Guido on July 15, 2004 at 18:29:21:

In Reply to: encrypt posted by matt on July 15, 2004 at 06:17:18:

: can someone please tell me how to call the function to encrypt or decrypt... i found some code online and it seems to work without error but i dont know how to get it to call it. I just want an editbox and have the encrypted message go into another editbox. same for the decrypted message. here is the code i got.

: unit Crypt32;
: interface
: const
: StartKey = 981; {Start default key}
: MultKey = 12674; {Mult default key}
: AddKey = 35891; {Add default key}

: function Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
: function Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;

: implementation
: {$R-}
: {$Q-}
{*******************************************************
: * Standard Encryption algorithm - Copied from Borland *
: *******************************************************}
: function Encrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
: var
: I : Byte;
: begin
: Result := '';
: for I := 1 to Length(InString) do
: begin
: Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
: StartKey := (Byte(Result[I]) + StartKey) * MultKey + AddKey;
: end;
: end;
: {*******************************************************
: * Standard Decryption algorithm - Copied from Borland *
: *******************************************************}
: function Decrypt(const InString:string; StartKey,MultKey,AddKey:Integer): string;
: var
: I : Byte;
: begin
: Result := '';
: for I := 1 to Length(InString) do
: begin
: Result := Result + CHAR(Byte(InString[I]) xor (StartKey shr 8));
: StartKey := (Byte(InString[I]) + StartKey) * MultKey + AddKey;
: end;
: end;
: {$R+}
: {$Q+}
: end.
----------------------------------------

Example of use:

unit Test;
interface
  ...
  ...
implementation
{$R *.DFM}
uses Crypt32;
var SEncrypted: string;
procedure TForm1.btnEncryptClick(Sender: TObject);
begin
  SEncrypted := Encrypt(Edit1.Text, StartKey, MultKey, AddKey);
  Edit2.Text := SEncrypted;
end;
procedure TForm1.btnDecryptClick(Sender: TObject);
begin
  Edit3.Text := Decrypt(SEncrypted, StartKey, MultKey, AddKey);
end;

Instead of StartKey, MultKey and AddKey, you can specify integer numbers of your choice, but of course use the same numbers for encrypting and decrypting. For example:

SEncrypted := Encrypt(Edit1.Text, 817, 3007, 19411);
...
Edit3.Text := Decrypt(SEncrypted, 817, 3007, 19411);

Regards,
webmaster Guido


Related Articles and Replies:




[ DelphiLand FAQ ]