1

我正在尝试将 VB5 转换为 .NET,但无法进行二进制读取。我的 VB.NET 解码只能正确读取前两个字符。(VB5->VB.NET) 编码是

    ' Open file
    x = Rnd(-mKeyValue)
    filenum = FreeFile()
    Try
        FileOpen(filenum, Filename, OpenMode.Binary)
    Catch ex As IO.IOException
        MsgBox(ex.ToString, MsgBoxStyle.Critical, "File opening error")
        Exit Sub
    End Try
    ' write data
    filecontents = ""       

    For i = 1 To Len(stringdate)
        charnum = Asc(Mid(stringdate, i, 1))
        randomint = Int(256 * Rnd())
        charnum = charnum Xor randomint
        singlechar = Chr(charnum)
        FilePut(filenum, singlechar, i)
        filecontents = filecontents & singlechar
    Next i

并且 (VB5->VB.NET) 解码是

    x = Rnd(-mKeyValue)
    filenum = FreeFile()
    FileOpen(filenum, Filename, OpenMode.Binary)
    For i = 1 To LOF(filenum)

        'To VB.NET
        FileGet(filenum, singlechar, i)
        charnum = Asc(singlechar)
        Debug.Print("VB5 singlechar = " & singlechar)
        randomint = Int(256 * Rnd())
        charnum = charnum Xor randomint
        singlechar = Chr(charnum)
     Next i

我的 VB.NET 代码失败(无法正确读取文件)是;

      Using reader As New BinaryReader(File.Open(Filename, FileMode.Open))
        ' Loop through length of file.
        Dim pos As Integer = 0
        Dim length As Integer = reader.BaseStream.Length


        While pos < length
            ' Read the integer.
            singlechar = reader.ReadChar()
            charnum = Asc(singlechar)    'singlechar is type Char
            randomint = Int(256 * Rnd())
            charnum = charnum Xor randomint
            singlechar = Chr(charnum)

            i += 1
        End While
    End Using

谁能帮我从 VB5 翻译成 .NET?

4

1 回答 1

1

在 VB.Net 中,一切都短了一点;)

    ' get a string from an encrypted file file:
    Dim b() As Byte = IO.File.ReadAllBytes("path")
    For i = 0 To b.Length - 1
        b(i) = b(i) Xor (256 * Rnd())
    Next
    Dim s As String = System.Text.Encoding.ASCII.GetString(b)

当.Net可以一次读取它时,为什么要逐字节读取(无论如何读取'char'没有意义,因为您只需要8位ASCII码)?我假设您的文件不大于 100 MB?然后在获得数组后,您只需将每个元素与您的“随机”值进行异或。如果不需要兼容旧版本,最好使用Random. 或者甚至更好......使用真正的加密(在.Net中它是内置的!)

' put a string into a file
    Dim c() As Byte = System.Text.Encoding.ASCII.GetBytes("The String you want to store encrypted")
    For i = 0 To c.Length - 1
        c(i) = c(i) Xor (256 * Rnd())
    Next
    IO.File.WriteAllBytes("another path", c)

“加密”也是如此。将字符串转换为字节数组(=ASCII 值),对其进行异或,然后在 ONE 操作中将其写回。

查看 Unicode 的危险:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' Beware of UNICODE ... !!!
    Using sw As New FileStream("foo.foo", FileMode.OpenOrCreate, FileAccess.Write)
        ' with old VB you effectively wrote BYTE data
        sw.Write({65, 192}, 0, 2)
    End Using

    Using br As New BinaryReader(File.Open("foo.foo", FileMode.Open, FileAccess.Read))
        ' You are telling. Net that you expect a CHAR, which is not ASCII, but UNICODE
        Dim c As Char = br.ReadChar
        Dim d As Char = br.ReadChar
        Dim cc = Asc(c)
        Dim dd = Asc(d)
        Debug.Print("65 -> {0}, 192 -> {1}", cc, dd)
    End Using

End Sub

输出是65 -> 65, 192 -> 63

于 2012-11-27T23:47:39.823 回答