DooDu(杜霖:I want,I can.(MmMVP马甲)) : 用ascw把字符转成unicode,放到数组,然后加密,解密用chrw,加密的算法你自己选择试过了,不行!wumylove1234(毁于随):加密之前先转成ANSI的, dim t() as byte t=strconv("abc我",vbfromunicode) 然后再做字节的操作.试过了,不行 对strconv这个函数转过去就转不过来了不知是我方法错了还是什么,你们有没有具体算法?我用数据测试一下
dim t() as byte t="abc我"'加密_最简单的 for i=0 to ubound(t) t(i)=t(i) xor 125 next i___输出t'解密 ___输入t for i=0 to ubound(t) t(i)=t(i) xor 125 next i dim s as string s=t ___输出s
这是根据早上说的写的一个例子,供你参考:Private Sub Form_Load() Dim MyText As String '待加密数据,即明文 Dim TextByte(40) As Byte '存放明文的二进制数类型 Dim MyKey As String '密钥数据 Dim KeyByte(40) As Byte '存放密钥数据的二进制数类型 Dim Mw(40) As Byte '加密结果 Dim RByte(40) As Byte Dim Result As String '解密后得到的数据 Dim MwLen As Integer '密文长度 Dim I As Integer Dim PrintText As String
StrToByte TextByte(), MyText, MwLen '转换 StrToByte KeyByte(), MyKey, I
Debug.Print "明文=" & MyText
PrintText = "加密结果=" ' 这里仅仅是用普通的异或加密,应根据实际情况更改 For I = 0 To MwLen - 1 ' 加密结果放在Mw()中 Mw(I) = VbXor(TextByte(I), KeyByte(I)) PrintText = PrintText & Str(Mw(I)) Next I Debug.Print PrintText
For I = 0 To MwLen - 1 ' 解密是逆运算,从Mw()取已加密的数据 RByte(I) = VbXor(Mw(I), KeyByte(I)) Next I ByteToStr Result, RByte(), MwLen '转换成VB字串 Debug.Print "解密=" & Result End SubPublic Function VbXor(A As Byte, B As Byte) As Byte Dim C1, C2, D1, D2 As Byte Dim I, J As IntegerC1 = A D1 = B VbXor = 0 For I = 7 To 0 Step -1 J = 2 ^ I C2 = C1 \ J D2 = D1 \ J If (C2 = 1 And D2 = 0) Or (C2 = 0 And D2 = 1) Then VbXor = VbXor + J End If If C2 = 1 Then C1 = C1 Mod J If D2 = 1 Then D1 = D1 Mod J Next I End FunctionPublic Sub StrToByte(ByteData() As Byte, StrData As String, ByRef ByteLen As Integer) Dim I As Integer Dim J As Integer Dim Word As LongJ = 0 For I = 1 To Len(StrData) Word = Asc(Mid(StrData, I, 1)) If Word < 0 Then Word = 65536 + Word ByteData(J) = Word \ 256 J = J + 1 ByteData(J) = Word Mod 256 Else ByteData(J) = Word End If J = J + 1 Next I ByteLen = J End SubPublic Sub ByteToStr(ByRef StrData As String, ByteData() As Byte, ByteLen As Integer) Dim I As Integer Dim J As Integer Dim Word As Long Dim MyStr As StringMyStr = "" For I = 0 To ByteLen - 1 If ByteData(I) < 128 Then Word = ByteData(I) Else Word = ByteData(I) Word = Word * 256 + ByteData(I + 1) - 65536 I = I + 1 End If MyStr = MyStr & Chr(Word) Next I StrData = MyStr End Sub
用VB6实现文本文件(.txt)加密的程序代码如下: Private Sub FileEncryptAndDecrypt (inputFile As String,OutputFile As String PasswordKey As String) Dim Char As String*1 Dim i as Integer Open InputFile For Binary As#1 '打开要加密/解密的文件 Open OutputFile For Binary As#2 '打开加密/解密后生成的文件 For i =1 to Filelen(InputFile) '读取文件中的字符 Get 1 ,Char Char=Chr$((Asc(Char)Xor PasswordKey)) '进行异或计算 Put 2,,Char Next i Close#1 C1ose#2 End Sub
我想你只要把汉字转成字节数组就行了。字节数组中的每个元素都可以得到一个ascii值,对其进行加密。同理,解密时再将字节数组转成字符串即可。下面是主要的转换过程,至于加密过程你就可以随便写了。Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)'********************************************** ' 把字符串存入字节数组, 返回字符串的长度 '********************************************** Private Function SaveStringToByteArry(strString As String, BytArray() As Byte) As Long Dim lngStrLen As Long
SaveStringToByteArry = lngStrLen End Function'********************************** ' 字节数组中的数据连接成字符串 '********************************** Private Function CreateStringFromByte(p As Long, ByteLength As Long) As String Dim StringData As String
2、加密后得到的字节流是不规则的,破坏了VB中双字节编码规律,因此只能用字节数组进行储存。请问:你想做成什么样子的加密方法?
我的一个文件中存有汉字和字符我现在需要对其进行加密,需要的时候对其进行解密.就这么简单
谢谢!
用ascw把字符转成unicode,放到数组,然后加密,解密用chrw,加密的算法你自己选择试过了,不行!wumylove1234(毁于随):加密之前先转成ANSI的,
dim t() as byte
t=strconv("abc我",vbfromunicode)
然后再做字节的操作.试过了,不行 对strconv这个函数转过去就转不过来了不知是我方法错了还是什么,你们有没有具体算法?我用数据测试一下
t="abc我"'加密_最简单的
for i=0 to ubound(t)
t(i)=t(i) xor 125
next i___输出t'解密
___输入t
for i=0 to ubound(t)
t(i)=t(i) xor 125
next i
dim s as string
s=t
___输出s
Dim MyText As String '待加密数据,即明文
Dim TextByte(40) As Byte '存放明文的二进制数类型
Dim MyKey As String '密钥数据
Dim KeyByte(40) As Byte '存放密钥数据的二进制数类型
Dim Mw(40) As Byte '加密结果
Dim RByte(40) As Byte
Dim Result As String '解密后得到的数据
Dim MwLen As Integer '密文长度
Dim I As Integer
Dim PrintText As String
MyText = "abc我们def大家" '待加密数据
MyKey = "真的很高兴认识你!!" '密钥数据
StrToByte TextByte(), MyText, MwLen '转换
StrToByte KeyByte(), MyKey, I
Debug.Print "明文=" & MyText
PrintText = "加密结果=" ' 这里仅仅是用普通的异或加密,应根据实际情况更改
For I = 0 To MwLen - 1 ' 加密结果放在Mw()中
Mw(I) = VbXor(TextByte(I), KeyByte(I))
PrintText = PrintText & Str(Mw(I))
Next I
Debug.Print PrintText
For I = 0 To MwLen - 1 ' 解密是逆运算,从Mw()取已加密的数据
RByte(I) = VbXor(Mw(I), KeyByte(I))
Next I
ByteToStr Result, RByte(), MwLen '转换成VB字串
Debug.Print "解密=" & Result
End SubPublic Function VbXor(A As Byte, B As Byte) As Byte
Dim C1, C2, D1, D2 As Byte
Dim I, J As IntegerC1 = A
D1 = B
VbXor = 0
For I = 7 To 0 Step -1
J = 2 ^ I
C2 = C1 \ J
D2 = D1 \ J
If (C2 = 1 And D2 = 0) Or (C2 = 0 And D2 = 1) Then
VbXor = VbXor + J
End If
If C2 = 1 Then C1 = C1 Mod J
If D2 = 1 Then D1 = D1 Mod J
Next I
End FunctionPublic Sub StrToByte(ByteData() As Byte, StrData As String, ByRef ByteLen As Integer)
Dim I As Integer
Dim J As Integer
Dim Word As LongJ = 0
For I = 1 To Len(StrData)
Word = Asc(Mid(StrData, I, 1))
If Word < 0 Then
Word = 65536 + Word
ByteData(J) = Word \ 256
J = J + 1
ByteData(J) = Word Mod 256
Else
ByteData(J) = Word
End If
J = J + 1
Next I
ByteLen = J
End SubPublic Sub ByteToStr(ByRef StrData As String, ByteData() As Byte, ByteLen As Integer)
Dim I As Integer
Dim J As Integer
Dim Word As Long
Dim MyStr As StringMyStr = ""
For I = 0 To ByteLen - 1
If ByteData(I) < 128 Then
Word = ByteData(I)
Else
Word = ByteData(I)
Word = Word * 256 + ByteData(I + 1) - 65536
I = I + 1
End If
MyStr = MyStr & Chr(Word)
Next I
StrData = MyStr
End Sub
而VB对字节、对位的处理不是很方便,所以额外增大了运算量,你可改用VC等写成动态库,
再用VB去调用,速度就快多了,而且也简单多了。
不过实际用起来,速度也不算慢。注意:要注意明文与密钥之间的长度,由于是演示,我没有做得很完善。
Private Sub FileEncryptAndDecrypt (inputFile As String,OutputFile As String PasswordKey As String)
Dim Char As String*1
Dim i as Integer
Open InputFile For Binary As#1
'打开要加密/解密的文件
Open OutputFile For Binary As#2
'打开加密/解密后生成的文件
For i =1 to Filelen(InputFile)
'读取文件中的字符
Get 1 ,Char
Char=Chr$((Asc(Char)Xor PasswordKey))
'进行异或计算
Put 2,,Char
Next i
Close#1
C1ose#2
End Sub
我的方法的确是不行的.
Sorry,如果不用来加密的话可以转回来,如果加密就破坏了ANSI代码.
你试试这个字 齱
' 把字符串存入字节数组, 返回字符串的长度
'**********************************************
Private Function SaveStringToByteArry(strString As String, BytArray() As Byte) As Long
Dim lngStrLen As Long
'** 获取字符串的长度(字节)
lngStrLen = LenB(StrConv(strString, vbFromUnicode))
'** 分配数组空间
ReDim BytArray(lngStrLen - 1) '** 将字符串地址中的内容拷贝到数组
CopyMemory ByVal VarPtr(BytArray(0)), ByVal strString, lngStrLen
SaveStringToByteArry = lngStrLen
End Function'**********************************
' 字节数组中的数据连接成字符串
'**********************************
Private Function CreateStringFromByte(p As Long, ByteLength As Long) As String
Dim StringData As String
'** 分配字符串空间
StringData = Space(ByteLength)
'** 复制字符数组地址内容到字符串地址
CopyMemory ByVal StringData, ByVal p, ByteLength
'** 返回字符串
CreateStringFromByte = StringData
End Function
完整的示例:http://www.vb99.com/code.asp?findmode=9&id=78
我只是给个简单的例子而已,再说了,你没看到我得的分也比你少啊,哈哈......
再说了,即使那个字那么复杂,不加密直接放着也无妨哦!(说笑).不过有机会还是请你多多指教哦!:)