我自己写了个,你参考下:(在你的界面上增加一个textbox和一个commandbutton按钮) 定义一个person.mdb数据库,里面有张person表,字段为(no(mainkey),name,passwd)三个Option Explicit Dim Ctext1 As String Dim Cipher_text As String, PassWord As StringPrivate Sub Form_Activate() Text1.Text = "" Text2.Text = "" Text1.SetFocus End SubPrivate Sub Form_Load() Form1.Height = 9000 Form1.Width = 12000 PassWord = "abc" '定义加密和解密的密匙为ABC Data1.DatabaseName = App.Path + "\person.mdb" '打开数据库 Data1.RecordSource = "person" '打开数据表 Data1.RefreshEnd Sub Private Sub Command1_Click() '比较用户名和密码,权限后。 If Text1.Text = "" Then MsgBox "用户名不能为空,请输入你的用户名。", vbOKOnly, "提示信息" Text1.SetFocus Exit Sub Else Data1.Recordset.MoveFirst Data1.Recordset.FindFirst "[name]= " + "'" + Text1 + "'" If Data1.Recordset.NoMatch Then MsgBox "没有该用户。", vbOKOnly, "提示信息" Text1.SetFocus Else Ctext1 = Text3.Text Decipher PassWord, Ctext1, Cipher_text Ctext1 = Cipher_text Cipher_text = "" If Text2.Text = Ctext1 Then '原密码和数据库中的经解密后的密码相同。 Unload Me MDIForm1.Show Else '原密码错误 MsgBox "密码错误。", vbOKOnly, "提示信息" Text2 = "" Text2.SetFocus End If End If End If End SubPrivate Sub Command1_KeyPress(KeyAscii As Integer) If KeyAscii = 9 Then Command2.SetFocus End If If KeyAscii = 13 Then If Text1.Text = "" Then MsgBox "用户名不能为空,请输入你的用户名。", vbOKOnly, "提示信息" Text1.SetFocus Exit Sub Else Data1.Recordset.MoveFirst Data1.Recordset.FindFirst "[name]= " + "'" + Text1 + "'" If Data1.Recordset.NoMatch Then MsgBox "没有该用户。", vbOKOnly, "提示信息" Text1.SetFocus Else Ctext1 = Text3.Text Decipher PassWord, Ctext1, Cipher_text Ctext1 = Cipher_text Cipher_text = "" If Text2.Text = Ctext1 Then '原密码和数据库中的经解密后的密码相同。 Unload Me MDIForm1.Show Else '原密码错误 MsgBox "密码错误。", vbOKOnly, "提示信息" Text2 = "" Text2.SetFocus End If End If End If End If End SubPrivate Sub Decipher(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, I, 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 >= MIN_ASC And Ch <= MAX_ASC Then Ch = Ch - MIN_ASC Offset = Int((NUM_ASC + 1) * Rnd) Ch = ((Ch - Offset) Mod NUM_ASC) If Ch < 0 Then Ch = Ch + NUM_ASC Ch = Ch + MIN_ASC To_text = To_text & Chr$(Ch) End If Next I End SubPrivate Function NumericPassword(ByVal PassWord As String) As Long Dim Value As Long, Ch As Long, Shift1 As Long, Shift2 As Long Dim I As Integer, 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 = Value End Function
如下是例子: person表里的name: system passwd: system 经加密后为: name: system passwd: $as*Mr
ACCESS Tool/Security/Set password...!
定义一个person.mdb数据库,里面有张person表,字段为(no(mainkey),name,passwd)三个Option Explicit
Dim Ctext1 As String
Dim Cipher_text As String, PassWord As StringPrivate Sub Form_Activate()
Text1.Text = ""
Text2.Text = ""
Text1.SetFocus
End SubPrivate Sub Form_Load()
Form1.Height = 9000
Form1.Width = 12000
PassWord = "abc" '定义加密和解密的密匙为ABC
Data1.DatabaseName = App.Path + "\person.mdb" '打开数据库
Data1.RecordSource = "person" '打开数据表
Data1.RefreshEnd Sub
Private Sub Command1_Click()
'比较用户名和密码,权限后。
If Text1.Text = "" Then
MsgBox "用户名不能为空,请输入你的用户名。", vbOKOnly, "提示信息"
Text1.SetFocus
Exit Sub
Else
Data1.Recordset.MoveFirst
Data1.Recordset.FindFirst "[name]= " + "'" + Text1 + "'"
If Data1.Recordset.NoMatch Then
MsgBox "没有该用户。", vbOKOnly, "提示信息"
Text1.SetFocus
Else
Ctext1 = Text3.Text
Decipher PassWord, Ctext1, Cipher_text
Ctext1 = Cipher_text
Cipher_text = ""
If Text2.Text = Ctext1 Then '原密码和数据库中的经解密后的密码相同。
Unload Me
MDIForm1.Show
Else '原密码错误
MsgBox "密码错误。", vbOKOnly, "提示信息"
Text2 = ""
Text2.SetFocus
End If
End If
End If
End SubPrivate Sub Command1_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Then
Command2.SetFocus
End If
If KeyAscii = 13 Then
If Text1.Text = "" Then
MsgBox "用户名不能为空,请输入你的用户名。", vbOKOnly, "提示信息"
Text1.SetFocus
Exit Sub
Else
Data1.Recordset.MoveFirst
Data1.Recordset.FindFirst "[name]= " + "'" + Text1 + "'"
If Data1.Recordset.NoMatch Then
MsgBox "没有该用户。", vbOKOnly, "提示信息"
Text1.SetFocus
Else
Ctext1 = Text3.Text
Decipher PassWord, Ctext1, Cipher_text
Ctext1 = Cipher_text
Cipher_text = ""
If Text2.Text = Ctext1 Then '原密码和数据库中的经解密后的密码相同。
Unload Me
MDIForm1.Show
Else '原密码错误
MsgBox "密码错误。", vbOKOnly, "提示信息"
Text2 = ""
Text2.SetFocus
End If
End If
End If
End If
End SubPrivate Sub Decipher(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, I, 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 >= MIN_ASC And Ch <= MAX_ASC Then
Ch = Ch - MIN_ASC
Offset = Int((NUM_ASC + 1) * Rnd)
Ch = ((Ch - Offset) Mod NUM_ASC)
If Ch < 0 Then Ch = Ch + NUM_ASC
Ch = Ch + MIN_ASC
To_text = To_text & Chr$(Ch)
End If
Next I
End SubPrivate Function NumericPassword(ByVal PassWord As String) As Long
Dim Value As Long, Ch As Long, Shift1 As Long, Shift2 As Long
Dim I As Integer, 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 = Value
End Function
person表里的name: system passwd: system
经加密后为: name: system passwd: $as*Mr