自己程序原来调用的配置文件是INI格式的,就是文本文件,这样的文件可以加密存取吗?谢谢
解决方案 »
- Delphi中使用的关键字你知道几个?
- 我公司请人
- 怎样捕捉indypop3的异常????请大家赐教
- 初学。请多都指教(50)
- Delphi+OpenGL到底还能用多久???
- 有没有关于Delphi7.0控件详细介绍(包括事件、属性、还有一些函数)的书。
- 怎样在dbgrid控件中用Enter代替Down key
- 【回家了,散分,再问个买火车票的问题】
- 在DELPHI中如何实现拷贝目录!!!!!!!!!!!!!!!
- 怎么在动态增加 的 TPageControl.TTabsheet 中加入一个浏览器窗口啊?像有些浏览器类似!非常感谢!
- 一年多Delphi开发,我将何去何从?
- 怎样将XML文件保存到SQL表中
DCP控件上有很多加密方法,
SysUtils, Classes, Controls, Forms,
Dialogs, StdCtrls,IniFiles,Base64;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}var
iniinfo:tiniFile;
dir:string;
const
title='INI 文件操作';procedure TForm1.Button1Click(Sender: TObject);
begin
iniinfo:=TIniFile.Create(dir+'\info.ini');
try
iniinfo.WriteString('persioninfo','name',Base64Encode(Edit1.Text));
iniinfo.WriteString('persioninfo','borndate',Base64Encode(Edit3.Text));
iniinfo.WriteString('persioninfo','work',Base64Encode(edit2.Text));
iniinfo.WriteString('persioninfo','Phone',Base64Encode(Edit4.Text));
iniinfo.WriteString('persioninfo','Home',Base64Encode(Edit5.Text));
finally
FreeAndNil(iniinfo);
end;
Form1.Caption:=title+' -- 文件写入成功'
end;procedure TForm1.Button2Click(Sender: TObject);
begin
iniinfo.Free;
Close;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
iniinfo:=TIniFile.Create(Dir+'\info.ini');
try
Edit1.Text:=Base64Decode(iniinfo.ReadString('persioninfo','name',''));
Edit3.text:=Base64Decode(iniinfo.ReadString('persioninfo','borndate',''));
Edit2.Text:=Base64Decode(iniinfo.ReadString('persioninfo','work',''));
Edit4.text:=Base64Decode(iniinfo.ReadString('persioninfo','Phone',''));
Edit5.Text:=Base64Decode(iniinfo.ReadString('persioninfo','Home',''));
finally
FreeAndNil(iniinfo);
end;
Form1.Caption:=title+' -- 文件读取成功'
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
form1.Caption:=title;
GetDir(0,dir);
end;end.找到一个很早做练习的时候写的小程序,用的加密算法是Base64,楼主可以改成自己的算法,希望对楼主有用。
若key不够长,则循环重复使用
function Decrypt(Src:String; Key:String):String;stdcall;
var KeyLen,KeyPos,Offset,SrcPos,SrcAsc,TmpSrcAsc:Integer;
Dest:String;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then Key:='Windows XP';
KeyPos:=0;
Offset:=StrToInt('$'+ Copy(Src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ Copy(Src,SrcPos,2));
if KeyPos < KeyLen Then KeyPos := KeyPos + 1
else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc<=Offset then TmpSrcAsc:=255+TmpSrcAsc-Offset
else TmpSrcAsc:=TmpSrcAsc-Offset;
Dest:=Dest + Chr(TmpSrcAsc);
Offset:=SrcAsc;
SrcPos:=SrcPos+2;
until SrcPos>=Length(Src);
Result:=Dest;
end;
//加密函数
function Encrypt(Src:String; Key:String):String;stdcall;
var KeyLen,KeyPos,Offset,SrcPos,SrcAsc:Integer;
Dest :ShortString;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then Key:='Windows XP';
KeyPos:=0;
Randomize;
Offset:=Random(256);
Dest:=Format('%1.2x',[Offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + Offset) MOD 255;
if KeyPos < KeyLen then KeyPos:= KeyPos + 1
else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
Dest:=Dest + Format('%1.2x',[SrcAsc]);
Offset:=SrcAsc;
end;
Result:=Dest;
end;procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
s:='windows';
Edit2.Text:=Encrypt(Edit1.Text,s);
end;procedure TForm1.Button2Click(Sender: TObject);
var
s:string;
begin
s:='windows';
Edit2.text:=Decrypt(edit1.Text,s);
end;
上面的s:='windows'; 就是算法的key ,没key还原不了你的数据的