vb中怎样在数据文件加密码
通过使用以下vb函数,对数据文件进行加密,代码以下:
Option Explicit
Dim i As Long
Dim databuff As Byte //定义数组用于寄存文件内容
Dim addbuff As Byte //定义数组用于寄存加密后的文件内容
Dim password As Byte //定义数组用于寄存密码的ASCII值
Dim filename As String
Private Sub Check1_Click
If Check1.Value Then //控制是否是显示全部文件
File1.Pattern = "."
Else
File1.Pattern = ".txt"
End If
End Sub
Private Sub Command1_Click
Dim j As Integer
Dim password_len As Integer
password_len = LenText2.Text
ReDim passwordpassword_len As Byte
For i = 0 To password_len - 1 //把密码转化为ASCII码
password i = AscMidText2.Text i + 1 1
Next
If filename = "" Then Exit Sub
Open filename For Binary As #1 //读取要加密的文件内容
ReDim databuffLOF1
Get #1 databuff
Close #1
ReDim addbuffUBounddatabuff As Byte
For i = 0 To UBound databuff
If j >= password_len Then //循环使用密码
j = 0
Else
j = j + 1
End If
addbuff i = databuff i Xor password j //进行异或运算
Next
Open filename For Binary As #1 //把加密后的内容写入文件
Put #1 addbuff
Close #1
Text1 = StrConvaddbuff vbUnicode //显示加密后的文件内容
Text2.Text = ""
End Sub
Private Sub Command2_Click
End
End Sub
Private Sub Dir1_Change
File1.Path = Dir1.Path //与文件列表框相干联
End Sub
Private Sub Drive1_Change
On Error GoTo a0
Dir1.Path = Drive1.Drive //与目录列表框相干联
a0 If Err Then MsgBox ErrorErr '产生毛病,提示毛病内容
End Sub
Private Sub File1_Click //单击文件时,显示文件内容
filename = Dir1.Path + File1.filename
If filename = "" Then Exit Sub
Open filename For Binary As #1
ReDim databuff LOF 1
Get #1 databuff
Close #1
Text1 = StrConv databuff vbUnicode
End Sub
本文来源:https://www.yuntue.com/post/61524.html | 云服务器网,转载请注明出处!

微信扫一扫打赏
支付宝扫一扫打赏