用注册表对Delphi程序进行加密

来源:互联网 发布:有诗意的淘宝店名字 编辑:程序博客网 时间:2024/04/29 04:53
本加密方法分三部分:
  1. 根据对注册表的搜索结果判定设置对话框的内容。
  2. 若初次使用,则设新密码;若是已经设置密码,则进行验证。
  3. 一个密码变换小程序(比原来的复杂得多)。当然,如果需要修改密码的功能,只要将设置密码部分改动一下即可。


一、程序启动时,通过搜索注册表,判断是否已有密码,来确定窗口的显示内容。不过事先应有以下的声明然后才能使用:

  在user中加入TRegistry,在var声明中加入以下几个窗体变量:
var  TheReg: TRegistry;  KeyName,ValueStr,tempStr:String;   procedure TfrmPass.FormShow(Sender: TObject);  begin   TheReg := TRegistry.Create;   try TheReg.RootKey := HKEY-LOCAL-MACHINE;   KeyName := ′SOFTWARE\Mypassword′;   //有该键则打开,没有则创建   if TheReg.OpenKey(KeyName, True) then begin   tempStr:=ExtractFileName(Application.ExeName); //读取密码   ValueStr:=TheReg.ReadString(tempStr);   //密码不为空则修改窗体为验证密码   if ValueStr<>′′ then begin   edit2.Visible:=false; frmPass.Caption:=′验证密码′;  edit1.SetFocus; OK.Caption:=′确定′;end   //密码为空则修改窗体为设置密码对话框   else begin   showmessage(′第一次使用请设置密码!′);   edit2.Visible:=true; frmPass.Caption:=′请设置新密码′;   edit1.SetFocus; OK.Caption:=′设置′;   end;TheReg.CloseKey;end;   finallyTheReg.Free;end;end;


二、按钮的响应代码:包括新设密码和验证密码。

  procedure TfrmPass.OKClick(Sender: TObject);  begin   //根据Edit2的显示与否判断已有密码,进行验证   if edit2.Visible=false then begin   if pass(edit1.text)=ValueStr then begin   showmessage(′密码正确!′);end   else begin   showmessage(′密码不正确!无权操作!′);   halt;end;end //无密码,设置新密码   else begin   if edit1.text=edit2.text then begin   TheReg := TRegistry.Create;   TheReg.RootKey := HKEY-LOCAL-MACHINE;   KeyName := ′SOFTWARE\Mypassword′;   if TheReg.OpenKey(KeyName, True) then   TheReg.WriteString(tempStr,pass(edit1.text));   TheReg.CloseKey;end   else begin   showmessage(′再次键入的密码不一致,请重输!′);   edit1.text:=′′; edit2.text:=′′;   edit1.SetFocus;end; //进行下一步操作...   end;end;

三、密码变换程序:注意要预先定义。
  这个变换小程序在笔者看来还不算很复杂,只进行了两次变换,不过,想要破译也是得费点劲。读者还可以采用其他的数学函数进行更为复杂的变换。
  
function pass(pstr:string):string;  var str,str1:string;  i,j:integer;  begin   str:=pstr;   for i:=1 to length(str) do begin  //进行第一次变换   j:=(i*i*i mod (i+20))+(i*i mod (i+10))+i*2+1;   str1:=str1+chr(ord(str[i])+j); //第二次变换   j:=(i*i*i mod (i+10))+(i*i mod (i+20))+i*2+1;   str1:=str1+chr(ord(str[i])+j); end;   pass:=str1;  end;

原创粉丝点击