?? 用注冊(cè)表對(duì)delphi程序進(jìn)行加密 (2001年3月24日).txt
字號(hào):
用注冊(cè)表對(duì)Delphi程序進(jìn)行加密 (2001年3月24日)
網(wǎng)友更新 分類:算法 作者: ming21cn(推薦) 推薦:ming21cn 閱讀次數(shù):610
(http://www.codesky.net)
--------------------------------------------------------------------------------
本加密方法分三部分:
1. 根據(jù)對(duì)注冊(cè)表的搜索結(jié)果判定設(shè)置對(duì)話框的內(nèi)容。
2. 若初次使用,則設(shè)新密碼;若是已經(jīng)設(shè)置密碼,則進(jìn)行驗(yàn)證。
3. 一個(gè)密碼變換小程序(比原來的復(fù)雜得多)。當(dāng)然,如果需要修改密碼的功能,只要將設(shè)置密碼部分改動(dòng)一下即可。
一、程序啟動(dòng)時(shí),通過搜索注冊(cè)表,判斷是否已有密碼,來確定窗口的顯示內(nèi)容。不過事先應(yīng)有以下的聲明然后才能使用:
在user中加入TRegistry,在var聲明中加入以下幾個(gè)窗體變量:
TheReg: TRegistry;
KeyName,ValueStr,tempStr:String;
procedure TfrmPass.FormShow(Sender: TObject);
begin
TheReg := TRegistry.Create;
try TheReg.RootKey := HKEY—LOCAL—MACHINE;
KeyName := ′SOFTWARE\Mypassword′;
//有該鍵則打開,沒有則創(chuàng)建
if TheReg.OpenKey(KeyName, True) then begin
tempStr:=ExtractFileName(Application.ExeName); //讀取密碼
ValueStr:=TheReg.ReadString(tempStr);
//密碼不為空則修改窗體為驗(yàn)證密碼
if ValueStr<>′′ then begin
edit2.Visible:=false; frmPass.Caption:=′驗(yàn)證密碼′;
edit1.SetFocus; OK.Caption:=′確定′; end
//密碼為空則修改窗體為設(shè)置密碼對(duì)話框
else begin
showmessage(′第一次使用請(qǐng)?jiān)O(shè)置密碼!′);
edit2.Visible:=true; frmPass.Caption:=′請(qǐng)?jiān)O(shè)置新密碼′;
edit1.SetFocus; OK.Caption:=′設(shè)置′;
end; TheReg.CloseKey; end;
finally TheReg.Free; end; end;
二、按鈕的響應(yīng)代碼:包括新設(shè)密碼和驗(yàn)證密碼。
procedure TfrmPass.OKClick(Sender: TObject);
begin
//根據(jù)Edit2的顯示與否判斷已有密碼,進(jìn)行驗(yàn)證
if edit2.Visible=false then begin
if pass(edit1.text)=ValueStr then begin
showmessage(′密碼正確!′); end
else begin
showmessage(′密碼不正確!無權(quán)操作!′);
halt; end; end //無密碼,設(shè)置新密碼
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(′再次鍵入的密碼不一致,請(qǐng)重輸!′);
edit1.text:=′′; edit2.text:=′′;
edit1.SetFocus; end; //進(jìn)行下一步操作...
end; end;
三、密碼變換程序:注意要預(yù)先定義。
這個(gè)變換小程序在筆者看來還不算很復(fù)雜,只進(jìn)行了兩次變換,不過,想要破譯也是得費(fèi)點(diǎn)勁。讀者還可以采用其他的數(shù)學(xué)函數(shù)進(jìn)行更為復(fù)雜的變換。
function pass(pstr:string):string;
var str,str1:string;
i,j:integer;
begin
str:=pstr;
for i:=1 to length(str) do begin
//進(jìn)行第一次變換
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;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -