Also bei mir klappt das wunderbar.
Man könnte das ganze noch zur vereinfachten Verwendung noch um ein paar Methoden erweitern, damit es jeder direkt verwenden kann.
Delphi-Quellcode:
uses
System.SysUtils, System.NetEncoding;
type
function ProtectData(data: string): TBytes;
function UnprotectData(data: TBytes): string;
function ProtectString(data: string): string;
function UnprotectString(data: string): string;
implementation
function ProtectData(data: string): TBytes;
var
inBuffer: TBytes;
begin
inBuffer:=TEncoding.UTF8.GetBytes(data);
Result:=TProtectedData.Protect(inBuffer);
end;
function UnprotectData(data: TBytes): string;
var
outBuffer: TBytes;
begin
outBuffer:=TProtectedData.Unprotect(data);
Result:=TEncoding.UTF8.GetString(outBuffer)
end;
function ProtectString(data: string): string;
var
ne: TNetEncoding;
Buffer: TBytes;
begin
ne:=TNetEncoding.Create;
try
Buffer:=ProtectData(data);
Result:=ne.Base64.EncodeBytesToString(Buffer);
finally
ne.Free;
end;
end;
function UnprotectString(data: string): string;
var
ne: TNetEncoding;
Buffer: TBytes;
begin
ne:=TNetEncoding.Create;
try
Buffer:=ne.Base64.DecodeStringToBytes(data);
Result:=UnprotectData(Buffer);
finally
ne.Free;
end;
end;
end.
Und um diese Daten auch in INI-Datei zu speichern:
Delphi-Quellcode:
procedure WriteMultiLineStringToIni(ini: TIniFile; Section: string; Ident: string; Value: string);
var
i: Integer;
sl: TStringList;
begin
sl:=TStringList.Create;
try
sl.Text:=Value;
for i:=sl.Count-1 downto 0 do
if sl[i]='' then sl.Delete(i);
ini.WriteInteger(Section, Ident+'Count', sl.Count);
for i:=0 to sl.Count-1 do
ini.WriteString(Section, Ident+inttostr(i), sl[i]);
finally
sl.Free;
end;
end;
function ReadMultilineStringFromIni(ini: TIniFile; Section: string; Ident: string): string;
var
i, count: Integer;
sl: TStringList;
begin
sl:=TStringList.Create;
try
Result:='';
if ini.SectionExists(Section) then
begin
if ini.ValueExists(Section, Ident+'Count') then
begin
count:=ini.ReadInteger(Section, Ident+'Count', 0);
for i:=0 to Count-1 do
sl.Add(ini.ReadString(Section, Ident+inttostr(i), ''));
for i:=sl.Count-1 downto 0 do
if sl[i]='' then sl.Delete(i);
Result:=sl.Text;
end;
end;
finally
sl.Free;
end;
end;