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.
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;
By the way, above was not helpful for me - I use multibyte character, and below code was so helpful even though I can't use any key on the documentation
uses IdCoder, IdCoderMIME, IdGlobal;
..
..
begin
// encode string
sMasterXML.Text := TIdEncoderMIME.EncodeString(sMasterXML.Text, IndyTextEncoding_UTF8);
// deocde string
sMasterXML.Text := TIdDecoderMIME.DecodeString(sMasterXML.Text, IndyTextEncoding_UTF8);
end;