想做加密文件,但是苦于找不到好的算法,各位提供一个函数吧要可以指定密匙的,而且解密后不会损坏数据
解决方案 »
- 如何让commondialog控件弹出的另存对话框在屏幕正中间显示出来啊?
- 我如何读取一个外部文件,把它形成二进制数据.????????
- 芳心似雪、郎心如蝎、恩情尽灭、红颜遭劫!
- 老师给留的一个问题,想破头了!急急急急,求您帮忙
- 有关VsFlexGrid中的二个问题
- 怎么会这样呢?
- 如何模拟键盘输入。。模拟过程中的一些问题。。有实践的朋友,讨论一下。
- 版主大人,求我呀!!!
- BitBlt时出错,不能创建AUTOREDRAW图象??
- 我在GOOGLE查VISUAL BASIC 宝典,它竟然把性爱宝典也给我列出来了。真是搞笑:)
- 请问,sql是否可以支持变量数组?
- vb 外接应用程序菜单下怎么才能出现API文本浏览器?
Windows的还是像office的文件的?
shell("rar.exe a -hp密码 加密后文件 要加密的文件")'加密
shell("rar.exe e -hp密码 加密后文件")'解密
VC写的MMEC加解密用的DLL的VB使用范例
'加密文件测试
FileCopy "C:\boot.ini", "C:\boot2.ini" '将 C:\boot.ini 备份出来操作
Dim fs As Integer
Dim FileBytes() As Byte
fs = FreeFile
ReDim FileBytes(FileLen("C:\boot2.ini") - 1)
Open "C:\boot2.ini" For Binary As #fs
Get #fs, 1, FileBytes
BytesToMMEC FileBytes(0), UBound(FileBytes) + 1, 18, 5, 1358, 26743
Put #fs, 1, FileBytes
Close #fs
End SubPrivate Sub Command3_Click()
'解密文件测试
Dim fs As Integer
Dim FileBytes() As Byte
fs = FreeFile
ReDim FileBytes(FileLen("C:\boot2.ini") - 1)
Open "C:\boot2.ini" For Binary As #fs
Get #fs, 1, FileBytes
MMECToBytes FileBytes(0), UBound(FileBytes) + 1, 18, 5, 1358, 26743
Put #fs, 1, FileBytes
Close #fs
End Sub
Event Progress(Percent As Long)
Private m_Key As String
Private m_sBox(0 To 255) As Integer
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Sub EncryptFile(SourceFile As String, DestFile As String, Optional Key As String)
Dim Filenr As Integer
Dim ByteArray() As Byte
If (Not FileExist(SourceFile)) Then
Call Err.Raise(vbObjectError, , "Error in Skipjack EncryptFile procedure (Source file does not exist).")
Exit Sub
End If
Filenr = FreeFile
Open SourceFile For Binary As #Filenr
ReDim ByteArray(0 To LOF(Filenr) - 1)
Get #Filenr, , ByteArray()
Close #Filenr
Call EncryptByte(ByteArray(), Key)
If (FileExist(DestFile)) Then Kill DestFile
Filenr = FreeFile
Open DestFile For Binary As #Filenr
Put #Filenr, , ByteArray()
Close #Filenr
End Sub
Public Sub DecryptFile(SourceFile As String, DestFile As String, Optional Key As String)
Dim Filenr As Integer
Dim ByteArray() As Byte
If (Not FileExist(SourceFile)) Then
Call Err.Raise(vbObjectError, , "Error in Skipjack EncryptFile procedure (Source file does not exist).")
Exit Sub
End If
Filenr = FreeFile
Open SourceFile For Binary As #Filenr
ReDim ByteArray(0 To LOF(Filenr) - 1)
Get #Filenr, , ByteArray()
Close #Filenr
Call DecryptByte(ByteArray(), Key)
If (FileExist(DestFile)) Then Kill DestFile
Filenr = FreeFile
Open DestFile For Binary As #Filenr
Put #Filenr, , ByteArray()
Close #FilenrEnd Sub
Public Sub DecryptByte(ByteArray() As Byte, Optional Key As String)
Call EncryptByte(ByteArray(), Key)
End Sub
Public Function EncryptString(Text As String, Optional Key As String) As String
Dim ByteArray() As Byte
ByteArray() = StrConv(Text, vbFromUnicode)
Call EncryptByte(ByteArray(), Key)
EncryptString = StrConv(ByteArray(), vbUnicode)
End Function
Public Function DecryptString(Text As String, Optional Key As String) As String
Dim ByteArray() As Byte
ByteArray() = StrConv(Text, vbFromUnicode)
Call DecryptByte(ByteArray(), Key)
DecryptString = StrConv(ByteArray(), vbUnicode)
End Function
Public Sub EncryptByte(ByteArray() As Byte, Optional Key As String)
Dim i As Long
Dim j As Long
Dim Temp As Byte
Dim Offset As Long
Dim OrigLen As Long
Dim CipherLen As Long
Dim CurrPercent As Long
Dim NextPercent As Long
Dim sBox(0 To 255) As Integer
If (Len(Key) > 0) Then Me.Key = Key
Call CopyMem(sBox(0), m_sBox(0), 512)
OrigLen = UBound(ByteArray) + 1
CipherLen = OrigLen
For Offset = 0 To (OrigLen - 1)
i = (i + 1) Mod 256
j = (j + sBox(i)) Mod 256
Temp = sBox(i)
sBox(i) = sBox(j)
sBox(j) = Temp
ByteArray(Offset) = ByteArray(Offset) Xor (sBox((sBox(i) + sBox(j)) Mod 256))
If (Offset >= NextPercent) Then
CurrPercent = Int((Offset / CipherLen) * 100)
NextPercent = (CipherLen * ((CurrPercent + 1) / 100)) + 1
RaiseEvent Progress(CurrPercent)
End If
Next
If (CurrPercent <> 100) Then RaiseEvent Progress(100)
End Sub
Public Property Let Key(New_Value As String)
Dim a As Long
Dim b As Long
Dim Temp As Byte
Dim Key() As Byte
Dim KeyLen As Long
If (m_Key = New_Value) Then Exit Property
m_Key = New_Value
Key() = StrConv(m_Key, vbFromUnicode)
KeyLen = Len(m_Key)
For a = 0 To 255
m_sBox(a) = a
Next a
For a = 0 To 255
b = (b + m_sBox(a) + Key(a Mod KeyLen)) Mod 256
Temp = m_sBox(a)
m_sBox(a) = m_sBox(b)
m_sBox(b) = Temp
Next
End Property
Option ExplicitPrivate Sub Command1_Click()
Dim a As String
a = "如何对汉字字符进行加密、解密是?"
Debug.Print jm("123fadf///", a)
Debug.Print jm("123fadf///", jm("123fadf///", a))
End SubPrivate Function jm(ByVal key As String, ByVal iStr As String) As String
Dim i As Long, tmp As String
For i = 0 To Len(iStr) - 1
tmp = tmp & Chr(Asc(Mid(iStr, i + 1, 1)) Xor Asc(Mid(key, i Mod Len(key) + 1, 1)))
Next
jm = tmp
End Function