📄 024.htm
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->DELPHI专题文档-程序应用-->用注册表对Delphi程序进行加密</TITLE>
<META NAME="keywords" CONTENT=" DELPHI专题文档-程序应用 用注册表对Delphi程序进行加密">
<META NAME="description" CONTENT=" - DELPHI专题文档-程序应用 - 用注册表对Delphi程序进行加密">
<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<a href="index6.html">返回</a>
<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>
<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">用注册表对Delphi程序进行加密</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<p align="center"><font color="#008000"><strong><span style="font-size: 9pt"> </span></strong></font>
<p><span style="font-size: 9pt"><br>
本加密方法分三部分:</span>
<p><span style="font-size: 9pt"> 1.
根据对注册表的搜索结果判定设置对话框的内容。</span>
<p><span style="font-size: 9pt"> 2.
若初次使用,则设新密码;若是已经设置密码,则进行验证。</span>
<p><span style="font-size: 9pt"> 3.
一个密码变换小程序(比原来的复杂得多)。当然,如果需要修改密码的功能,只要将设置密码部分改动一下即可。</span>
<p><span style="font-size: 9pt"> 一、程序启动时,通过搜索注册表,判断是否已有密码,来确定窗口的显示内容。不过事先应有以下的声明然后才能使用:</span>
<p><span style="font-size: 9pt"> 在user中加入TRegistry,在var声明中加入以下几个窗体变量:</span>
<p><span style="font-size: 9pt"> TheReg: TRegistry;</span>
<p><span style="font-size: 9pt"> KeyName,ValueStr,tempStr:String;</span>
<p><span style="font-size: 9pt"> procedure TfrmPass.FormShow(Sender: TObject);</span>
<p><span style="font-size: 9pt"> begin</span>
<p><span style="font-size: 9pt"> TheReg := TRegistry.Create;</span>
<p><span style="font-size: 9pt"> try TheReg.RootKey := HKEY—LOCAL—MACHINE;</span>
<p><span style="font-size: 9pt"> KeyName := ′SOFTWARE\Mypassword′;</span>
<p><span style="font-size: 9pt"> //有该键则打开,没有则创建</span>
<p><span style="font-size: 9pt"> if TheReg.OpenKey(KeyName, True) then begin</span>
<p><span style="font-size: 9pt"> tempStr:=ExtractFileName(Application.ExeName); //读取密码</span>
<p><span style="font-size: 9pt"> ValueStr:=TheReg.ReadString(tempStr);</span>
<p><span style="font-size: 9pt"> //密码不为空则修改窗体为验证密码</span>
<p><span style="font-size: 9pt"> if ValueStr<>′′ then begin</span>
<p><span style="font-size: 9pt"> edit2.Visible:=false; frmPass.Caption:=′验证密码′;</span>
<p><span style="font-size: 9pt"> edit1.SetFocus; OK.Caption:=′确定′; end</span>
<p><span style="font-size: 9pt"> //密码为空则修改窗体为设置密码对话框</span>
<p><span style="font-size: 9pt"> else begin</span>
<p><span style="font-size: 9pt"> showmessage(′第一次使用请设置密码!′);</span>
<p><span style="font-size: 9pt"> edit2.Visible:=true; frmPass.Caption:=′请设置新密码′;</span>
<p><span style="font-size: 9pt"> edit1.SetFocus; OK.Caption:=′设置′;</span>
<p><span style="font-size: 9pt"> end; TheReg.CloseKey; end;</span>
<p><span style="font-size: 9pt"> finally TheReg.Free; end; end;</span>
<p><span style="font-size: 9pt"> 二、按钮的响应代码:包括新设密码和验证密码。</span>
<p><span style="font-size: 9pt"> procedure TfrmPass.OKClick(Sender: TObject);</span>
<p><span style="font-size: 9pt"> begin</span>
<p><span style="font-size: 9pt"> //根据Edit2的显示与否判断已有密码,进行验证</span>
<p><span style="font-size: 9pt"> if edit2.Visible=false then begin</span>
<p><span style="font-size: 9pt"> if pass(edit1.text)=ValueStr then begin</span>
<p><span style="font-size: 9pt"> showmessage(′密码正确!′); end</span>
<p><span style="font-size: 9pt"> else begin</span>
<p><span style="font-size: 9pt"> showmessage(′密码不正确!无权操作!′);</span>
<p><span style="font-size: 9pt"> halt; end; end //无密码,设置新密码</span>
<p><span style="font-size: 9pt"> else begin</span>
<p><span style="font-size: 9pt"> if edit1.text=edit2.text then begin</span>
<p><span style="font-size: 9pt"> TheReg := TRegistry.Create;</span>
<p><span style="font-size: 9pt"> TheReg.RootKey := HKEY—LOCAL—MACHINE;</span>
<p><span style="font-size: 9pt"> KeyName := ′SOFTWARE\Mypassword′;</span>
<p><span style="font-size: 9pt"> if TheReg.OpenKey(KeyName, True) then</span>
<p><span style="font-size: 9pt"> TheReg.WriteString(tempStr,pass(edit1.text));</span>
<p><span style="font-size: 9pt"> TheReg.CloseKey; end</span>
<p><span style="font-size: 9pt"> else begin</span>
<p><span style="font-size: 9pt"> showmessage(′再次键入的密码不一致,请重输!′);</span>
<p><span style="font-size: 9pt"> edit1.text:=′′; edit2.text:=′′;</span>
<p><span style="font-size: 9pt"> edit1.SetFocus; end; //进行下一步操作...</span>
<p><span style="font-size: 9pt"> end; end;</span>
<p><span style="font-size: 9pt"> 三、密码变换程序:注意要预先定义。</span>
<p><span style="font-size: 9pt"> 这个变换小程序在笔者看来还不算很复杂,只进行了两次变换,不过,想要破译也是得费点劲。读者还可以采用其他的数学函数进行更为复杂的变换。</span>
<p><span style="font-size: 9pt"> function pass(pstr:string):string;</span>
<p><span style="font-size: 9pt"> var str,str1:string;</span>
<p><span style="font-size: 9pt"> i,j:integer;</span>
<p><span style="font-size: 9pt"> begin</span>
<p><span style="font-size: 9pt"> str:=pstr;</span>
<p><span style="font-size: 9pt"> for i:=1 to length(str) do begin</span>
<p><span style="font-size: 9pt"> //进行第一次变换</span>
<p><span style="font-size: 9pt"> j:=(i*i*i mod (i+20))+(i*i mod (i+10))+i*2+1;</span>
<p><span style="font-size: 9pt"> str1:=str1+chr(ord(str[i])+j); //第二次变换</span>
<p><span style="font-size: 9pt"> j:=(i*i*i mod (i+10))+(i*i mod (i+20))+i*2+1;</span>
<p><span style="font-size: 9pt"> str1:=str1+chr(ord(str[i])+j); end;</span>
<p><span style="font-size: 9pt"> pass:=str1;</span>
<p><span style="font-size: 9pt"> end;</span></p>
<BR>
<hr color="#EE9B73" size="1" width="94%">
</TD>
</TR>
</table>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -