转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
如何实现VB程序登录密码加密         ★★★★

如何实现VB程序登录密码加密

作者:闵涛 文章来源:闵涛的学习笔记 点击数:613 更新时间:2009/4/23 14:58:05
现在有些软件都设置密码登录,启动软件时要求使用者输入有效的密码。

  其实密码就是对明文文本进行一一对应的变换,使这变成不可识别的密码文本,让非法使用者不能识别。

  本程序是通过,输入登录密码,然后把用户密码加密保存到文本里。

  首先,建立一个标准EXE工程,在窗体上放置一个TextBox控件,名称为txtPassword,PasswordChar属性为"*"。

  再放置两个CommandButton控件,第一个的名称为CmdSave,Caption属性为"保存密码(&S)",另一个的名称为CmdExit,Caption属性为"退出(&Q)"。

  主程序原代码如下:

  Option Explicit定义变量Dim Filenum As IntegerDim LoadFiles As String

  Private Sub txtPassword_Change()

  CmdSave.Enabled = TrueEnd Sub

  Private Sub CmdSave_Click() 保存密码

  当密码输入为空时,则提示信息。

  If txtPassword.Text = Empty Then

  MsgBox "请你输入要更改的密码!", vbExclamation, Me.Caption

  Exit Sub

  End If

  将你输入的密码加密到Cipher_Text的变量里

  Dim Cipher_Text As String

  SubCipher txtPassword.Text, txtPassword.Text, Cipher_Text

  保存到文件并加密

  Filenum = FreeFile

  

  Open LoadFiles For Random As Filenum

  把Cipher_Text的变量写入文件里

  Put #Filenum, 1, Cipher_Text

  Close Filenum

  

  CmdSave.Enabled = False

  End Sub

  Private Sub Form_Load()On Error Resume Next

  密码信息文件的路径

  LoadFiles = App.Path & IIf(Len(App.Path) $#@62; 3, "\key.dat", "key.dat")

  

  Dim FilesTest As Boolean

  检验key.dat文件是否存在

  If Dir(LoadFiles, vbHidden) = Empty Then

  FilesTest = False

  Else

  FilesTest = True

  End If

  Filenum = FreeFile 提供一个尚未使用的文件号

  读取密码文件,把文件的信息赋值给StrTarget变量

  Dim StrTarget As String

  Open LoadFiles For Random As Filenum

  Get #Filenum, StrTarget

  Close Filenum

  如果key.dat文件已存在,则要求输入登录密码

  If FilesTest = True Then

  Dim InputString As String

  InputString = InputBox("请你输入登录密码" & Chr(13) & Chr(13) & "万能密码:http://vbboshi.126.com", "密码登录", InputString)

  End If

  将你输入的密码解密到Plain_Text变量

  Dim Plain_Text As String

  SubDecipher InputString, StrTarget, Plain_Text

  txtPassword.Text = Plain_Text

  密码输入错误,则退出程序

  If InputString $#@60;$#@62; txtPassword.Text Then

  If InputString $#@60;$#@62; "http://vbboshi.126.com" Then

  MsgBox "你输入密码错误!", vbExclamation, "错误": End

  Else

  txtPassword.Text = Empty

  End If

  End If

  

  CmdSave.Enabled = FalseEnd Sub

  Private Sub cmdexit_Click() 退出程序

  Unload MeEnd Sub

  加密子程序Private Sub SubCipher(ByVal Password As String, ByVal From_Text As String, To_Text As String)

  Const MIN_ASC = 32 Space.

  Const MAX_ASC = 126 ~.

  Const NUM_ASC = MAX_ASC - MIN_ASC + 1

  

  Dim offset As Long

  Dim Str_len As Integer

  Dim i As Integer

  Dim ch As Integer

  

  得到了加密的数字

  offset = NumericPassword(Password)

  

  Rnd -1

  对随机数生成器做初始化的动作

  Randomize offset

  Str_len = Len(From_Text)

  For i = 1 To Str_len

  ch = Asc(Mid$(From_Text, i, 1))

  If ch $#@62;= MIN_ASC And ch $#@60;= MAX_ASC Then

  ch = ch - MIN_ASC

  offset = Int((NUM_ASC + 1) * Rnd)

  ch = ((ch + offset) Mod NUM_ASC)

  ch = ch + MIN_ASC

  To_Text = To_Text & Chr$(ch)

  End If

  Next iEnd Sub

  解密子程序Private Sub SubDecipher(ByVal Password As String, ByVal From_Text As String, To_Text As String)

  Const MIN_ASC = 32 Space.

  Const MAX_ASC = 126 ~.

  Const NUM_ASC = MAX_ASC - MIN_ASC + 1

  

  Dim offset As Long

  Dim Str_len As Integer

  Dim i As Integer

  Dim ch As Integer

  

  offset = NumericPassword(Password)

  Rnd -1

  Randomize offset

  Str_len = Len(From_Text)

  For i = 1 To Str_len

  ch = Asc(Mid$(From_Text, i, 1))

  If ch $#@62;= MIN_ASC And ch $#@60;= MAX_ASC Then

  ch = ch - MIN_ASC

  offset = Int((NUM_ASC + 1) * Rnd)

  ch = ((ch - offset) Mod NUM_ASC)

  If ch $#@60; 0 Then ch = ch + NUM_ASC

  ch = ch + MIN_ASC

  To_Text = To_Text & Chr$(ch)

  End If

  Next iEnd Sub

  将你输入的每个字符转换成密码数字Private Function NumericPassword(ByVal Password As String) As Long

  Dim Value As Long

  Dim ch As Long

  Dim Shift1 As Long

  Dim Shift2 As Long

  Dim i As Integer

  Dim Str_len As Integer

  

  得到字符串内字符的数目

  Str_len = Len(Password)

  给每个字符转换成密码数字

  For i = 1 To Str_len

  ch = Asc(Mid$(Password, i, 1))

  Value = Value Xor (ch * 2 ^ Shift1)

  Value = Value Xor (ch * 2 ^ Shift2)

  

  Shift1 = (Shift1 + 7) Mod 19

  Shift2 = (Shift2 + 13) Mod 23

  Next i

  NumericPassword = ValueEnd Function

  本程序在Windows98SE+VB5.0中运行通过。


[VB.NET程序]GSM短信模块库函数,可以用VB,VC,调用简单实用  [办公软件]PowerPoint做交互课件之弃用VBA
[办公软件]VBA获取U盘、主板、CPU序列号和网卡MAC地址  [办公软件]VBA设置文件属性及加密源代码示例
[办公软件]VBA中初始化ADO连接的几种方法  [网络安全]“VB破坏者变种N”病毒摘要
[Web开发]ASP.NET上传文件到数据库VB版  [办公软件]在Excel中利用VBA实现多表单元格数据的读取与赋值…
[办公软件]使用Vba读取已关闭的Excel工作薄数据到当前工作表…  [办公软件]Excel编程基础之VBA文件操作详解
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台