vb给加密
文件或文件夹的加密、解密
'此方法对 WinXP 系统有效,Win98 没试验过。小心:不能用于系统文件或文件夹,否则会使系统瘫痪。
'加密:利用 API 函数在文件或文件夹名称末尾添上字符“..\”。比如,将文件夹“MyPath”更名为“MyPath..\”,在我的电脑中显示的名称就是“MyPath.”。系统会无法识别,此文件或文件夹就无法打开和修改,也无法删除。着名的病毒 Autorun 就是玩的这个小把戏。
'解密:去掉文件或文件夹名称末尾的字符“..\”
'将以下代码复制到 VB 的窗体代码窗口即可
'例子需控件:Command1、Command2、Text1,均采用默认属性设置
Private Const MAX_PATH = 260
Private Type FileTime ' 8 Bytes
LTime As Long
HTime As Long
End Type
Private Type Win32_Find_Data
dwFileAttributes As Long
ftCreationTime As FileTime
ftLastAccessTime As FileTime
ftLastWriteTime As FileTime
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cNameFile As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function MoveFileEx Lib "kernel32" Alias "MoveFileExA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal dwFlags As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpNameFile As String, lpFindFileData As Win32_Find_Data) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As Win32_Find_Data) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Sub Form_Load()
Text1.Text = "C:\MyPath"
Command1.Caption = "解密": Command2.Caption = "加密"
Me.Caption = "目录或文件的加解密"
End Sub
Private Sub Command1_Click()
Call SetPathName(False) '解密
End Sub
Private Sub Command2_Click()
Call SetPathName(True) '加密
End Sub
Private Sub SetPathName(SetMi As Boolean)
Dim nName As String, NewName As String, nSort As String, nCap As String, dl As Long
nName = Trim(Text1.Text)
If Right(nName, 3) = "..\" Then nName = Left(nName, Len(nName) - 3)
If Right(nName, 1) = "\" Then nName = Left(nName, Len(nName) - 1)
If SetMi Then
NewName = nName & "..\"
Else
NewName = nName
nName = nName & "..\"
End If
If SetMi Then nCap = "加密" Else nCap = "解密"
nSort = GetShortName(nName) '转变其中的 ..\
If nSort = "" Then
MsgBox "文件没有找到:" & vbCrLf & nName, vbCritical, nCap
Exit Sub
End If
If MoveFileEx(nSort, NewName, 0) = 0 Then Exit Sub '文件更名:非零表示成功,支持只读文件
MsgBox nCap & "成功:" & vbCrLf & nName, vbInformation, nCap
End Sub
Public Function GetShortName(F As String, Optional ShortAll As Boolean) As String
'转变为短文件名,如果目录或文件不存在就返回空。可用于判断某目录或文件是否存在
'不能直接用 API 函数 GetShortPathName, 因它不支持 ..\
'ShortAll=T 表示全部转变为短名称,否则只转变其中的点点杠“..\”
Dim FondID As Long, ID1 As Long, S As Long, nPath As String
Dim nF As String, InfoF As Win32_Find_Data, qF As String, hF As String
Dim nName As String, nName1 As String
nF = F
Do
S = InStr(nF, "..\")
If S = 0 Then Exit Do
qF = Left(nF, S + 2): hF = Mid(nF, S + 3) '分为前后两部分
CutPathName qF, nPath, nName
nName = LCase(nName)
qF = nPath & "\" & "*."
FondID = FindFirstFile(qF, InfoF) '-1表示失败。查找所有文件(夹)
ID1 = FondID
Do
If FondID = Find_Err Or ID1 = 0 Then GoTo Exit1 '没有找到符合条件的条目
nName1 = LCase(CutChr0(InfoF.cNameFile)) '文件(夹)名称
If nName1 & ".\" = nName Then
nName1 = CutChr0(InfoF.cAlternate) '用短文件名代替
If hF = "" Then nF = nPath & "\" & nName1 Else nF = nPath & "\" & nName1 & "\" & hF
Exit Do
End If
ID1 = FindNextFile(FondID, InfoF) '查找下一个,0表示失败
Loop
FindClose FondID
Loop
Exit1:
FindClose FondID
S = MAX_PATH: nName = String(S, vbNullChar)
ID1 = GetShortPathName(nF, nName, S) '返回实际字节数,0表示失败
If ID1 = 0 Then Exit Function
If ShortAll Then
If ID1 > S Then
S = ID1: nName = String(S, vbNullChar)
ID1 = GetShortPathName(nF, nName, S) '返回实际字节数
End If
GetShortName = CutChr0(nName)
Else
GetShortName = nF
End If
End Function
Public Sub CutPathName(ByVal F As String, nPath As String, nName As String)
Dim I As Long, LenS As Long
LenS = Len(F)
For I = LenS - 1 To 2 Step -1
If Mid(F, I, 1) = "\" Then
nPath = Left(F, I - 1): nName = Mid(F, I + 1)
GoTo Exit1
End If
Next
nPath = F: nName = ""
Exit1:
If Right(nPath, 2) = ".." Then
nPath = nPath & "\"
Else
If Right(nPath, 1) = "\" Then nPath = Left(nPath, Len(nPath) - 1)
End If
If Right(nName, 1) = "\" And Right(nName, 3) <> "..\" Then nName = Left(nName, Len(nName) - 1)
End Sub
Private Function CutChr0(xx As String) As String
Dim S As Long
S = InStr(xx, vbNullChar)
If S > 0 Then CutChr0 = Left(xx, S - 1) Else CutChr0 = xx
End Function
'参考资料见下
② 怎么用VB给文件夹加密
1、由于采用二进制读取文件的方式,因此加密时一般可以不考虑文件类型。
2、这里只进行一次异或运算,如有需要可以进行多次异或运算。
3、此加密算法速度快,当然加密强度也低 ;
参考代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'-----------------------------------------------------------------------
'函数说明: 使用异或运算加密文件(可加密大部分文件)
'参数说明: key - 密钥
' fileName - 普通文件名,
' encryptFileName - 加密后的文件名
'返回值: true - 成功,false - 失败
'-----------------------------------------------------------------------
Private Function XOR_Encrypt(key As Integer, fileName As String, encryptFileName As String) As Boolean
On Error GoTo errHandler
Dim inputFileNo As Integer
Dim fileBytes() As Byte
Dim length As Long
XOR_Encrypt = False
'打开文件并保存在二进制数组中
inputFileNo = FreeFile
Open fileName For Binary As #inputFileNo
length = LOF(inputFileNo)
If length = 0 Then
MsgBox "退出加密:文件内容为空!", vbInformation, "提示"
Exit Function
End If
ReDim fileBytes(length - 1) As Byte
Get inputFileNo, , fileBytes()
Close #inputFileNo
'将该二进制数组进行异或加密
Dim i As Long
For i = LBound(fileBytes) To UBound(fileBytes)
fileBytes(i) = fileBytes(i) Xor key
Next
'将异或加密后的二进制数组保存在新的文件中
Dim outputFileNo As Integer
outputFileNo = FreeFile
Open encryptFileName For Binary As #outputFileNo
Put outputFileNo, , fileBytes
Close #outputFileNo
XOR_Encrypt = True
errHandler:
If Err.Number Then
MsgBox "加密过程中出错:" & Err.Description, vbCritical, "错误"
XOR_Encrypt = False
Resume Next
End If
End Function
③ vb 字母 加密字符串
PrivateSubCommand1_Click()'加密
DimpAsString,sAsString,tAsString
DimiAsInteger,kAsInteger
p=""
s=Text1.Text
Fori=1ToLen(s)
k=InStr(p,Mid(s,i,1))
Ifk=0Then
MsgBox"数据有误"
ExitSub
Else
t=t&Mid(p,((k+4)Mod52)+1,1)
EndIf
Next
Text2.Text=t
Text1.Text=""
EndSub
PrivateSubCommand2_Click()'解密
DimpAsString,sAsString,tAsString
DimiAsInteger,kAsInteger
p=""
s=Text2.Text
Fori=1ToLen(s)
k=InStr(p,Mid(s,i,1))
Ifk=0Then
MsgBox"数据有误"
ExitSub
Else
t=t&Mid(p,((k+46)Mod52)+1,1)
EndIf
Next
Text1.Text=t
Text2.Text=""
EndSub
以上代码用到四个控件:Text1放加密前的数据,Text2放加密后的数据,Command1点击加密,Command2点击解密
④ vb对字符串加密(偏移3位)
Private Sub Command1_Click()
Dim a() As String, b() As Integer, n As Integer
n = Len(txtinputbox)
Print n
ReDim a(n)
ReDim b(n + 3)
For i = 1 To n
a(i) = Mid(txtinputbox, i, 1)
b(i) = Asc(a(i)) + 3
If b(i) > Asc("Z") And b(i) < Asc("a") Then b(i) = Asc("A") + b(i) - Asc("Z") - 1
If b(i) > Asc("z") Then b(i) = Asc("a") + b(i) - Asc("z") - 1
a(i) = Chr(b(i))
codelabel.Caption = codelabel.Caption & a(i)
Next
End Sub
⑤ 用VB写个简单加密/解密程序
'图上的控件,你就照着摆上去,然后再把以下代码拷进去,就OK了
Dim lg As Integer
'加密
Private Sub Command1_Click()
Text2 = ""
Dim a(), b() As String
lg = Len(Text1)
ReDim a(lg), b(lg)
For i = 1 To lg
a(i) = Mid(Text1, i, 1)
b(i) = AscW(a(i)) Xor 4
Text2 = Text2 & ChrW(b(i))
Next
End Sub
'解密
Private Sub Command2_Click()
Text3 = ""
Dim a(), b() As String
lg = Len(Text2)
ReDim a(lg), b(lg)
For i = 1 To lg
a(i) = Mid(Text2, i, 1)
b(i) = AscW(a(i)) Xor 4
Text3 = Text3 & ChrW(b(i))
Next
End Sub
⑥ 用VB编写程序怎样给文件夹加密码
加密原理:循环使用密码中每个字符的ASCII码值与文件的每个字节进行异或运算,然后写入文件即可。这种加密方法是可逆的,即对明文进行加密得到密文,用相同的密码对密文进行加密就得到明文。
界面设计:在窗体From1上放置驱动器列表框(Driver1)、目录列表框(Dir1)、文件列表框(File1)各一个,这三个控件相互配合,用来确定要加密文件的位置。其中File1的Pattern属性设为“*.TXT”,即仅显示文本文件;再放置一个Check控件,用来控制显示文件的类型,其Caption属性设为“显示全部文件”;接着放置两个文本框,Text1显示文件内容,Text2用来输入密码,其Passchar属性设为“*”,一个Label控件,其Caption属性设为“密码”;最后,放置两个命令按钮,其Caption属性分别设为“加密/解密”和“退出”。
程序代码:
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 = Len(Text2.Text)
ReDim password(password_len) As Byte
For i = 0 To password_len - 1 ’把密码转化为ASCII码
password(i)= Asc(Mid(Text2.Text,i + 1,1))
Next
If filename = "" Then Exit Sub
Open filename For Binary As #1 ’读取要加密的文件内容
ReDim databuff(LOF(1))
Get #1,, databuff
Close #1
ReDim addbuff(UBound(databuff))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 = StrConv(addbuff 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(Error(Err))’发生错误,提示错误内容
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
⑦ VB 加密与解密的程序代码
加密:
PrivateFunction JiaMi(ByVal varPass As String) As String '参数varPass是需要加密的文本内容
Dim varJiaMi As String * 20
Dim varTmp As Double
Dim strJiaMi As String
Dim I
For I = 1 To Len(varPass)
varTmp = AscW(Mid$(varPass, I, 1))
varJiaMi = Str$(((((varTmp * 1.5) / 5.6) * 2.7) * I))
strJiaMi = strJiaMi & varJiaMi
NextI
JiaMi = strJiaMi
EndFunction
解密函数:
PrivateFunction JieMi(ByVal varPass As String) As String '参数varPass是需要解密的密文内容
Dim varReturn As String * 20
Dim varConvert As Double
Dim varFinalPass As String
Dim varKey As Integer
Dim varPasslenth As Long
varPasslenth = Len(varPass)
For I = 1 To varPasslenth / 20
varReturn = Mid(varPass, (I - 1) * 20 + 1, 20)
varConvert = Val(Trim(varReturn))
varConvert = ((((varConvert / 1.5) * 5.6) / 2.7) / I)
varFinalPass = varFinalPass & ChrW(Val(varConvert))
NextI
JieMi = varFinalPass
EndFunction
(7)vb给加密扩展阅读:
注意事项
编写加密程序,将用户输入的一个英文句子加密为加密字符串,然后输出加密字符串。假设句子长度不超过100个字符。
根据给定的句子加密函数原型SentenceEncoding,编写函数SentenceEncoding调用给定的字符加密函数CharEncoding完成句子加密。
然后,编写主程序提示用户输入英文句子,然后调用函数SentenceEncoding对句子加密,最后输出加密后的蔽铅句子。
字符加密规则为大写字母和小写字母均加密为其补码, 我们定义ASCII码值相加为’A’+’Z’即155的两个大写字母互为补码,ASCII码值相加改禅为’a’+’z’即219的两个小写字母互为补码。
空格用@代替,句号以#代替,其它字符用句点代替。
函数原型:
void SentenceEncoding(char *soure,char *code);
功能:对待加密字符串source加密后保存加密字符串到code.
参数:char *soure,指向待加密句子的字符串指针;
char *code 指向加密字符串的字符串指针;
字符加密宏歼好函数代码。
⑧ VB 保存文件并加密!
简单说就是做个加密‘解密的两个函数,
在保存之前把要保存的记录先通过加密函数加密一下,再存到ini文件里,在读取的时候先读取ini里的信息再通过解密函数把他解密,然后在显示在文本框上,
加密的方式有很多,异或加密法是比较快的一种