Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Excerpt

There are lots of way can encrypt a text string based on a key. By the approach, you can use a single numerical key, or text string. In terms of security's point of view, text string-based key has better security performance.

Below code is an example of string encryption module.

Code Block
languagedelphi
function Encrypt(const InString:string; Salt:string): string;
var
  i : Byte;
  StartKey, MultKey, AddKey: Word;
begin
  Result := '';
  if (Salt = '') then begin
    Result := InString;
  end
  else begin
    StartKey := Length(Salt);
    MultKey := Ord(Salt[1]);
    AddKey := 0;
    for i := 1 to Length(Salt) - 1 do AddKey := AddKey + Ord(Salt[i]);
    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;
end;

function Decrypt(const InString:string; Salt:string): string;
var
  i : Byte;
  StartKey, MultKey, AddKey: Word;
begin
  Result := '';
  if (Salt = '') then begin
    Result := InString;
  end
  else begin
    StartKey := Ord(Salt[1]);
    MultKey := Length(Salt);
    AddKey := 0;
    for i := 1 to Length(Salt) - 1 do AddKey := AddKey + Ord(Salt[i]);
    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;
end;

...