现在有些软件都设置密码登录,启动软件时要求使用者输入有效的密码。其实密码就是对明文文本进行一一对应的变换,使这变成不可识别的密码文本,让非法使用者不能识别。
本程序是通过,输入登录密码,然后把用户密码加密保存到文本里。
首先,建立一个标准EXE工程,在窗体上放置一个TextBox控件,名称为txtPassword,PasswordChar属性为"*"。再放置两个CommandButton控件,第一个的名称为CmdSave,Caption属性为"保存密码(&S)",另一个的名称为CmdExit,Caption属性为"退出(&Q)"。
主程序原代码如下: OptionExplicit '定义变量 DimFilenumAsInteger DimLoadFilesAsString
PrivateSubtxtPassword_Change() CmdSave.Enabled=True EndSub
PrivateSubCmdSave_Click()'保存密码
'当密码输入为空时,则提示信息。 IftxtPassword.Text=EmptyThen MsgBox"请你输入要更改的密码!",vbExclamation,Me.Caption ExitSub EndIf
'将你输入的密码加密到Cipher_Text的变量里 DimCipher_TextAsString SubCiphertxtPassword.Text,txtPassword.Text,Cipher_Text
'保存到文件并加密 Filenum=FreeFile
OpenLoadFilesForRandomAsFilenum '把Cipher_Text的变量写入文件里 Put#Filenum,1,Cipher_Text CloseFilenum
CmdSave.Enabled=False
EndSub
PrivateSubForm_Load() OnErrorResumeNext
'密码信息文件的路径 LoadFiles=App.Path&IIf(Len(App.Path)>3,"\key.dat","key.dat")
DimFilesTestAsBoolean
'检验key.dat文件是否存在 IfDir(LoadFiles,vbHidden)=EmptyThen FilesTest=False Else FilesTest=True EndIf Filenum=FreeFile'提供一个尚未使用的文件号
'读取密码文件,把文件的信息赋值给StrTarget变量 DimStrTargetAsString OpenLoadFilesForRandomAsFilenum Get#Filenum,1,StrTarget CloseFilenum
'如果key.dat文件已存在,则要求输入登录密码 IfFilesTest=TrueThen DimInputStringAsString InputString=InputBox("请你输入登录密码"&Chr(13)&Chr(13)&"万能密码:http://www.vbeden.com","密码登录",InputString) EndIf
'将你输入的密码解密到Plain_Text变量 DimPlain_TextAsString SubDecipherInputString,StrTarget,Plain_Text txtPassword.Text=Plain_Text
'密码输入错误,则退出程序 IfInputString<>txtPassword.TextThen IfInputString<>"http://www.vbeden.com"Then MsgBox"你输入密码错误!",vbExclamation,"错误":End Else txtPassword.Text=Empty EndIf EndIf
CmdSave.Enabled=False EndSub
PrivateSubcmdexit_Click()'退出程序 UnloadMe EndSub
'加密子程序 PrivateSubSubCipher(ByValPasswordAsString,ByValFrom_TextAsString,To_TextAsString) ConstMIN_ASC=32'Space. ConstMAX_ASC=126'~. ConstNUM_ASC=MAX_ASC-MIN_ASC 1
DimoffsetAsLong DimStr_lenAsInteger DimiAsInteger DimchAsInteger
'得到了加密的数字 offset=NumericPassword(Password)
Rnd-1 '对随机数生成器做初始化的动作 Randomizeoffset
Str_len=Len(From_Text) Fori=1ToStr_len ch=Asc(Mid$(From_Text,i,1)) Ifch>=MIN_ASCAndch<=MAX_ASCThen ch=ch-MIN_ASC offset=Int((NUM_ASC 1)*Rnd) ch=((ch offset)ModNUM_ASC) ch=ch MIN_ASC To_Text=To_Text&Chr$(ch) EndIf Nexti EndSub
'解密子程序 PrivateSubSubDecipher(ByValPasswordAsString,ByValFrom_TextAsString,To_TextAsString) ConstMIN_ASC=32'Space. ConstMAX_ASC=126'~. ConstNUM_ASC=MAX_ASC-MIN_ASC 1
DimoffsetAsLong DimStr_lenAsInteger DimiAsInteger DimchAsInteger
offset=NumericPassword(Password) Rnd-1 Randomizeoffset
Str_len=Len(From_Text) Fori=1ToStr_len ch=Asc(Mid$(From_Text,i,1)) Ifch>=MIN_ASCAndch<=MAX_ASCThen ch=ch-MIN_ASC offset=Int((NUM_ASC 1)*Rnd) ch=((ch-offset)ModNUM_ASC) Ifch<0Thench=ch NUM_ASC ch=ch MIN_ASC To_Text=To_Text&Chr$(ch) EndIf Nexti EndSub
'将你输入的每个字符转换成密码数字 PrivateFunctionNumericPassword(ByValPasswordAsString)AsLong DimValueAsLong DimchAsLong DimShift1AsLong DimShift2AsLong DimiAsInteger DimStr_lenAsInteger
'得到字符串内字符的数目 Str_len=Len(Password) '给每个字符转换成密码数字 Fori=1ToStr_len ch=Asc(Mid$(Password,i,1)) Value=ValueXor(ch*2^Shift1) Value=ValueXor(ch*2^Shift2)
Shift1=(Shift1 7)Mod19 Shift2=(Shift2 13)Mod23 Nexti NumericPassword=Value EndFunction
注:VB编程乐园:http://www.vbeden.com Email:gzboshi@21cn.com
本程序在Windows98SE VB5.0中运行通过。->
|