注册 登录
编程论坛 VB6论坛

关于一个加密代码的问题

chenyuexiong 发布于 2012-08-28 13:34, 683 次点击
Function ByteToBin(m As Byte) As String ' 将字节型数据转换成八位二进制字符串
  Dim c$
  c$ = ""
  Do While m <> 0
  r = m Mod 2
  m = m \ 2
  c$ = r & c$
  Loop
  c$ = Right("00000000" & c$, 8)
  ByteToBin = c$
  End Function
  Function Reverse(m As String) As String ' 将八位二进制字符串颠倒顺序
  Dim i%, x$
  x = ""
  For i = 1 To 8
  x = Mid(m, i, 1) & x
  Next i
  Reverse = x
  End Function
  Function BinToByte(m As String) As Byte ' 将八位二进制串转换成十进制
  Dim x As String * 1, y%, z%
  z = 0
  For i = 1 To 8
  x = Mid(m, i, 1)
  y = x * 2 ^ (8 - i)
  z = z + y
  Next i
  BinToByte = z
  End Function
  Private Sub Command1_Click()
  Dim x As Byte, i%, fname$
  fname = InputBox("请输入要加密的文件名!注意加上路径名:")
  If Dir(fname) = "" Then
  MsgBox "文件不存在!"
  Exit Sub
  End If
  Open fname For Binary As #1 ' 以二进制访问模式打开待加密文件
  For i = 1 To LOF(1) ' LOF函数是求文件长度的内部函数
  Get #1, i, x ' 取出第i个字节
  x = BinToByte(Reverse(ByteToBin(x))) ' 这里调用了三个自定义函数
  Put #1, i, x ' 将加密后的这个字节写回到文件原位置
  Next i
  Close
  MsgBox "任务完成!"
  End Sub

这里是对单个文件加密,怎么写代码能让一个文件夹里的所有文件都按照上面的代码加密,求高手给个全代码.万分感激
6 回复
#2
风吹过b2012-08-28 19:43
窗体上放一个 驱动器控件,放一个目录控件,放一个文件列表控件。
写好关联代码来。

COmmand1 ,用来执行一个文件,在 file1 里选定的那个文件。
COMMAND2 ,用来执行所有在 FILE1 里的文件。

Private Sub Command1_Click()
  Dim x As Byte, i%, fname$
    fname=file1.path & "\" & file1.list(file1.listindex)    '取每个文件名
    '忘了 path 里是不是包括 这个 "\" 符号了。

  Open fname For Binary As #1 ' 以二进制访问模式打开待加密文件
  For i = 1 To LOF(1) ' LOF函数是求文件长度的内部函数
  Get #1, i, x ' 取出第i个字节
  x = BinToByte(Reverse(ByteToBin(x))) ' 这里调用了三个自定义函数
  Put #1, i, x ' 将加密后的这个字节写回到文件原位置
  Next i
  Close
  MsgBox "任务完成!"
  End Sub

Private Sub Command2_Click()
  Dim x As Byte, i%, fname$,j%
for j=1 to file1.listcount                 '以file1 文件清单为目录
    fname=file1.path & "\" & file1.list(j-1)    '取每个文件名
    '忘了 path 里是不是包括 这个 "\" 符号了。

  Open fname For Binary As #1 ' 以二进制访问模式打开待加密文件
  For i = 1 To LOF(1) ' LOF函数是求文件长度的内部函数
  Get #1, i, x ' 取出第i个字节
  x = BinToByte(Reverse(ByteToBin(x))) ' 这里调用了三个自定义函数
  Put #1, i, x ' 将加密后的这个字节写回到文件原位置
  Next i
  Close

next j
  MsgBox "任务完成!"
  End Sub
#3
chenyuexiong2012-08-29 09:56
问题解决了,还是感谢版主,不过这代码我执行起来有溢出.
#4
bczgvip2012-08-29 11:39
程序代码:

Function ReverseByte(bData As Byte) As Byte '将二进制倒序 Byte
    If bData And &H80 Then ReverseByte = ReverseByte Or &H1
    If bData And &H40 Then ReverseByte = ReverseByte Or &H2
    If bData And &H20 Then ReverseByte = ReverseByte Or &H4
    If bData And &H10 Then ReverseByte = ReverseByte Or &H8
    If bData And &H8 Then ReverseByte = ReverseByte Or &H10
    If bData And &H4 Then ReverseByte = ReverseByte Or &H20
    If bData And &H2 Then ReverseByte = ReverseByte Or &H40
    If bData And &H1 Then ReverseByte = ReverseByte Or &H80
End Function
这样不快过吗?
#5
chenyuexiong2012-08-30 13:48
请问还有其他代码实现吗.换了算法后不知道怎么替换代码
#6
bczgvip2012-08-30 23:00
  Get #1, i, x ' 取出第i个字节
  x = ReverseByte(x) ' 这里调用了三个自定义函数
  Put #1, i, x ' 将加密后的这个字节写回到文件原位置
#7
邵帅2012-08-31 12:49
加密。。。。。
1