community.borland.com

Article #15761: How to encrypt a String

 Technical Information Database

TI761D.txt - How to encrypt a String

Category   :Turbo Pascal
Platform   :All
Product    :

Description:
Here is a program that demonstrates routines for encryption and
decryption of strings. Note: We claim no responsibily for the
security of these functions.

{ Begin code }

program Crypt;


uses WinCRT;

const
  C1 = 52845;
  C2 = 22719;

function Encrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  Result[0] := S[0];
  for I := 1 to Length(S) do begin
    Result[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(Result[I]) + Key) * C1 + C2;
  end;
end;

function Decrypt(const S: String; Key: Word): String;
var
  I: byte;
begin
  Result[0] := S[0];
  for I := 1 to Length(S) do begin
    Result[I] := char(byte(S[I]) xor (Key shr 8));
    Key := (byte(S[I]) + Key) * C1 + C2;
  end;
end;

var
  S: string;
begin
  Write('>');
  ReadLn(S);
  S := Encrypt(S,12345);
  WriteLn(S);
  S := Decrypt(S,12345);
  WriteLn(S);
end.

Reference:
 

3/30/99 12:28:33 PM
 

Last Modified: 01-SEP-99