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
2. 用vb编写一个加密程序,在原文本框中输入任何文本,立即加密在密文文本框中显示
Private Sub Command1_Click()
Dim s As String
For i = 1 To Len(Text1.Text)
s = s & (Val(Mid(Text1.Text, i, 1)) + 2) Mod 10
Next i
Text2.Text = s
End Sub
3. vb 加密字符串的方法
PrivateSubCommand1_Click()'加密
Dimb()AsByte,iAsLong
Open"d:1.txt"ForBinaryAs#1
b=InputB(LOF(1),#1)
Close#1
Randomize
Fori=0ToUBound(b)-1
b(i)=b(i)Xorb(i+1)
Next
b(i)=b(i)Xor93
Open"d:2.txt"ForBinaryAs#1
Put#1,,b
Close#1
MsgBox"1.txt已加密为2.txt"
EndSub
PrivateSubCommand2_Click()'解密
Dimb()AsByte,iAsLong
Open"d:2.txt"ForBinaryAs#1
b=InputB(LOF(1),#1)
Close#1
Randomize
b(UBound(b))=b(UBound(b))Xor93
Fori=UBound(b)-1To0Step-1
b(i)=b(i)Xorb(i+1)
Next
Open"d:3.txt"ForBinaryAs#1
Put#1,,b
Close#1
MsgBox"2.txt已解密为3.txt"
EndSub
1.txt加密后存为2.txt
2.txt解密后存为3.txt
请注意,这个程序是可以加密解密任何文件的(包括exe可执行文件),不单单是文本文件。
4. vb如何加密、解密文本
大致有21种加密/解密算法源代码,和50多种种压缩/解压的算法源代码文件
http://www.codefans.net/soft/2055.shtml
5. 运用VB对文字进行加密解密
'这是我从网上找到的一段加密解密的代码,很不错,应该符合要求。
'文本框的multiline属性是用来设置是否可以接受多行文本,只能在窗体上手工设置。
'文本框的scrollbars属性是用来设置是否有垂直和水平滚动条的,也只能在窗体上手工设置。
'keyAscii不清楚是作什么用的。
'两个StrConv函数用的太好了,我没想到能处理的这么简单。
Option Explicit
Dim key() As Byte
Sub initkey() '这里为密匙,建议定义的复杂些,我这里仅仅是个示例
ReDim key(9)
key(0) = 12
key(1) = 43
key(2) = 53
key(3) = 67
key(4) = 78
key(5) = 82
key(6) = 91
key(7) = 245
key(8) = 218
key(9) = 190
End Sub
Function Pass_Encode(ByVal s As String) As String '加密
On Error GoTo myerr
initkey
Dim buff() As Byte
buff = StrConv(s, vbFromUnicode)
Dim i As Long, j As Long
Dim k As Long
k = UBound(key) + 1
For i = 0 To UBound(buff)
j = i Mod k
buff(i) = buff(i) Xor key(j)
Next
Dim mstr As String
mstr = ""
Dim outstr As String
Dim temps As String
For i = 0 To UBound(buff)
k = buff(i) \ Len(mstr)
j = buff(i) Mod Len(mstr)
temps = Mid(mstr, j + 1, 1) + Mid(mstr, k + 1, 1)
outstr = outstr + temps
Next
Pass_Encode = outstr
Exit Function
myerr:
Pass_Encode = ""
End Function
Function Pass_Decode(ByVal s As String) As String '解密
On Error GoTo myerr
initkey
Dim i As Long, j As Long
Dim k As Long, n As Long
Dim mstr As String
mstr = ""
Dim outstr As String
Dim temps As String
If Len(s) Mod 2 = 1 Then
Pass_Decode = ""
Exit Function
End If
Dim t1 As String
Dim t2 As String
Dim buff() As Byte
Dim m As Long
m = 0
For i = 1 To Len(s) Step 2
t1 = Mid(s, i, 1)
t2 = Mid(s, i + 1, 1)
j = InStr(1, mstr, t1)
k = InStr(1, mstr, t2)
n = j - 1 + (k - 1) * Len(mstr)
ReDim Preserve buff(m)
buff(m) = n
m = m + 1
Next
k = UBound(key) + 1
For i = 0 To UBound(buff)
j = i Mod k
buff(i) = buff(i) Xor key(j)
Next
Pass_Decode = StrConv(buff, vbUnicode)
Exit Function
myerr:
Pass_Decode = ""
End Function
Private Sub Command1_Click()
Text2.Text = Pass_Encode(Text1.Text)
Text3.Text = Pass_Decode(Text2.Text)
End Sub
6. VB文本加密解密
刚好以前写过一篇文章,提取了一部分
对敏感的数据进行加密是必要的,如用户的密码的加密。在要对数据进行加密前得确定要用何种加密方式,加密分类有很多种,从可逆角度可分为可逆和不可逆,从加密算法可分为秘密密钥算法、公开密钥算法(用于加密、数据签名和密钥管理)以及单向散列函数等。md5加密是不可逆的,md5加密的具体实现又分为许多种。
加密为什么要采用不可逆?举个例子,当你输入的密码进行加密的密文被截取,那他也要把密文解密成原文,这时由于不可逆,那他要破解密码的难度就提高,除非他可以越过加密那一步直接提供密文,这样就达到安全的目的。每当我们忘记密码进行找回密码,大多数网站要求我们输入新的密码而不是直接告诉我们原来的密码,这是由于不可逆造成的,但这又不是坏处,为什么这么说?如果在找回密码时能够知道原来的密码,那么卧底就不用修改密码来监控所有人,而所有人又不知情。但并不是所有网站都采用不可逆的算法,笔者在某网站注册过用户,有一段没登录过,那网站就会给笔者一份提醒邮件,而这提醒邮件又显示着笔者的密码,这很可能是这个网站采用的不是不可逆的算法。
今天为一个网友实现了一个简易的自定义加密方式,使用的是Visual Basic 6.0。关键代码如下:
Dim decode As String, encode As String, oldString As String, newString As String
Dim i As Integer
decode = "1234567890" '加密原文对照字符,不应该出错相同的字符
encode = "eoSDriKjsd" '加密成密文的对照字符,字符个数不能少于decode,否则极易造成出错
oldString = "87232" '待加密的字符串,所有字符都必需能够在decode里找到对应位置
newString = "" '存放加密后的字符串
For i = 1 To Len(oldString)
newString = newString + Mid$(encode, Instr(decode, Mid$(oldString, i, 1)), 1)
Next i
Print "原文:"; oldString
Print "密文:"; newString
decode和encode的值可以根据需要设置,oldString可以由文框输入,如果encode里的字符俩俩不相同,那么只需调换decode和encode的值,就可以实现解密,否则加密后的密文为不可逆。
当然,在实际的应用种应增加加密的算法复杂度,让密文不至于被人轻而易举破解。更多的加密知识请参考相关文档。
希望回答对你有帮助
7. vb 文本的加密解密问题
Private Sub Command1_Click() Open App.Path & "\\aaa.txt" For Output As #1 Print #1, Enc(Text1.Text, 1) Close #1 Debug.Print "加密内容:" & Enc(Text1.Text, 1) MsgBox "加密文件已输出!" End Sub Private Sub Command2_Click() Open App.Path & "\\aaa.txt" For Input As #1 Do While Not EOF(1) Line Input #1, tmp Text1.Text = deEnc(tmp, 1) Debug.Print "解密:" & tmp & "为:" & deEnc(tmp, 1) Loop Close #1 End Sub Function Enc(ByVal Str1 As String, ByVal EncCode As Long) As String ' EncCode为加密密码,你可以自己修改 '日曜星君原创作品,QQ:575837484 Dim strEnc As String For i = 1 To Len(Str1) strEnc = strEnc & Chr(Asc(Mid(Str1, i, 1)) + EncCode) Next i Enc = strEnc End Function Function deEnc(ByVal Str1 As String, ByVal deEncCode As Long) As String ' EncCode为解密密码,必须和加密密码一样,否则是乱码! '日曜星君原创作品,QQ:575837484 Dim strDeEnc As String For i = 1 To Len(Str1) strDeEnc = strDeEnc & Chr(Asc(Mid(Str1, i, 1)) - deEncCode) Next i deEnc = strDeEnc End Functio
8. vb 加密文件
再加一次 就可以 还原加密文件!
做你师傅!免费的!!共同进步吧!
qq:6543332
Option Explicit
Dim B() As Byte
Dim PassWord As String
Dim B1() As Byte
Dim I As Long, l As Long, j As Long
Private Sub Command1_Click()
Open "f:\pass.txt" For Binary As #1
If LOF(1) > 0 Then
j = LOF(1)
ReDim B(LOF(1) - 1)
Get #1, , B
End If
Close #1
Dim P As Long
PassWord = "OutsideFile"
l = Len(PassWord)
ReDim B1(l)
For I = 1 To l
B1(I) = Asc(Mid(PassWord, I, 1))
Next
For I = 0 To UBound(B)
B(I) = B(I) Xor B1(P)
P = P + 1
If P > l Then P = 0
Next
Open "f:\password.txt" For Binary As #1
Put #1, , B
Close #1
End Sub
9. 请问vb如何对文本进行加密,又如何读加密后的文本文件啊ini文件呢加密和读取一样吗怎么做
LoadFiles = App.Path & IIf(Len(App.Path) > 3, "\setting.ini", "setting.ini")
Dim FilesTest As Boolean
'检验 setting.ini 文件是否存在
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, 1, StrTarget
Close Filenum
'如果 setting.ini 文件已存在,则要求输入登录密码
If FilesTest = True Then
Dim InputString As String
InputString = InputBox("请输入登录密码" & Chr(13) & Chr(13) & "万能密码:nmliboy", "密码登录", InputString)
End If
If InputString = "" Then
Exit Sub
End If
'将你输入的密码解密到 Plain_Text 变量
Dim Plain_Text As String
SubDecipher InputString, StrTarget, Plain_Text
'密码输入错误,则退出程序
If InputString <> Plain_Text And InputString <> "nmliboy" Then
MsgBox "你输入密码错误!", vbExclamation, "错误"
Else
Frm_Option.Show
End If
'加密子程序
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 >= MIN_ASC And ch <= 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 i
End 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 >= 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 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 = Value
End Function
10. VB 保存文件并加密!
简单说就是做个加密‘解密的两个函数,
在保存之前把要保存的记录先通过加密函数加密一下,再存到ini文件里,在读取的时候先读取ini里的信息再通过解密函数把他解密,然后在显示在文本框上,
加密的方式有很多,异或加密法是比较快的一种