加密
PriStrList.Text := Encrypt(PriStrList.Text, cKey);
PriStrList.SaveToFile('font.txt');
解密
PriStrList.LoadFromFile('font.txt');
PriStrList.Text := Decrypt(PriStrList.Text, cKey);
PriStrList.Text := Encrypt(PriStrList.Text, cKey);
PriStrList.SaveToFile('font.txt');
解密
PriStrList.LoadFromFile('font.txt');
PriStrList.Text := Decrypt(PriStrList.Text, cKey);
TStringList只处理可视字符和部分分隔符(如:#9,#10,#13,#32,#33,#34...)
具体请你参考如下代码
//from
kingron.myetang.com(*//
标题:充分利用pascal字符串类型
说明:和PChar不同,string可以保存#0字符在其中;示例文件、内存流字符串之间转换
设计:Zswang
日期:2002-01-25
支持:[email protected]
//*)///////Begin Source
function StringToFile(mString: string; mFileName: TFileName): Boolean;
{ 返回字符串保存到文件是否成功 }
var
vFileChar: file of Char;
I: Integer;
begin
{$I-}
AssignFile(vFileChar, mFileName);
Rewrite(vFileChar);
for I := 1 to Length(mString) do Write(vFileChar, mString[I]);
CloseFile(vFileChar);
{$I+}
Result := (IOResult = 0) and (mFileName <> '');
end; { StringToFile }function FileToString(mFileName: TFileName): string;
{ 返回从文件载入字符串 }
var
vFileChar: file of Char;
vChar: Char;
begin
Result := '';
{$I-}
AssignFile(vFileChar, mFileName);
Reset(vFileChar); while not Eof(vFileChar) do begin
Read(vFileChar, vChar);
Result := Result + vChar;
end;
CloseFile(vFileChar);
{$I+}
end; { FileToString }function StreamToString(mStream: TStream): string;
{ 将内存流转换成字符串 }
var
I: Integer;
begin
Result := '';
if not Assigned(mStream) then Exit;
SetLength(Result, mStream.Size);
for I := 0 to Pred(mStream.Size) do try
mStream.Position := I;
mStream.Read(Result[Succ(I)], 1);
except
Result := '';
end;
end; { StreamToString }function StringToStream(mString: string; mStream: TStream): Boolean;
{ 返回将字符串保存到内存流是否成功 }
var
I: Integer;
begin
Result := True;
try
mStream.Size := 0;
mStream.Position := 0;
for I := 1 to Length(mString) do mStream.Write(mString[I], 1);
except
Result := False;
end;
end; { StringToStream }
///////End Source///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
var
vMemoryStream: TMemoryStream;
begin
Memo1.Text := FileToString('C:\WINDOWS\Desktop\1.txt');
vMemoryStream := TMemoryStream.Create;
try
Memo1.Lines.SaveToStream(vMemoryStream);
Memo2.Text := StreamToString(vMemoryStream);
finally
vMemoryStream.Free;
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
vMemoryStream: TMemoryStream;
begin
StringToFile(Memo2.Text, 'C:\WINDOWS\Desktop\1.txt');
vMemoryStream := TMemoryStream.Create;
try
StringToStream(Memo2.Text, vMemoryStream);
vMemoryStream.Position := 0;
Memo1.Lines.LoadFromStream(vMemoryStream);
finally
vMemoryStream.Free;
end;
end;
///////End Demo(*//
标题:字符串加密;pascal字符表示
说明:应用于文件加密
设计:Zswang
日期:2002-02-19
支持:[email protected]
//*)///////Begin Source
function StringToDisplay(mString: string): string;
var
I: Integer;
S: string;
begin
Result := '';
S := '';
for I := 1 to Length(mString) do
if mString[I] in [#32..#127] then
S := S + mString[I]
else begin
if S <> '' then begin
Result := Result + QuotedStr(S);
S := '';
end;
Result := Result + Format('#$%x', [Ord(mString[I])]);
end;
if S <> '' then Result := Result + QuotedStr(S);
end; { StringToDisplay }function DisplayToString(mDisplay: string): string;
var
I: Integer;
S: string;
B: Boolean;
begin
Result := '';
B := False;
mDisplay := mDisplay;
for I := 1 to Length(mDisplay) do
if B then case mDisplay[I] of
'''': begin
if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]);
if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + '''';
S := '';
B := False;
end;
else S := S + mDisplay[I];
end
else case mDisplay[I] of
'#', '''': begin
if S <> '' then Result := Result + Chr(StrToIntDef(S, 0));
S := '';
B := mDisplay[I] = '''';
end;
'$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I];
end;
if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0));
end; { DisplayToString }function StringEncrypt(mStr: string; mKey: string): string;
var
I, J: Integer;
begin
J := 1;
Result := '';
for I := 1 to Length(mStr) do begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else J := 1;
end;
{ 自己加步骤 }
end; { StringEncrypt }function StringDecrypt(mStr: string; mKey: string): string;
var
I, J: Integer;
begin
J := 1;
Result := '';
{ 自己加步骤 }
for I := 1 to Length(mStr) do begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else J := 1;
end;
end; { StringDecrypt }
///////End Source///////Begin Demo
const
cKey = '给你这一把钥匙,只能打开这一把锁';procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := StringToDisplay(StringEncrypt(Memo1.Text, cKey));
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := StringDecrypt(DisplayToString(Memo2.Text), cKey);
end;
///////End Demo
StringToFile(Encrypt(FileToString('font.txt'), cKey), 'font.dat');
解密
StringToFile(Decrypt(FileToString('font.dat'), cKey), 'font.txt');