Graham
Graham

Reputation: 153

Open/Read a binary file - access rights

I am trying to convert VB5 to .NET and cannot get a binary read to work. My VB.NET decode only reads the first two characters correctly. The (VB5->VB.NET) encode is

    ' 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

And the (VB5->VB.NET) decode is

    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

My VB.NET code which fails (cannot read the file correctly) is;

      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

Can anyone help me with translation from VB5 to .NET?

Upvotes: 1

Views: 5782

Answers (1)

igrimpe
igrimpe

Reputation: 1785

In VB.Net everything is a bit shorter ;)

    ' 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)

Why read byte by byte (no sense to read 'char' anyway, since you only want the 8bit ASCII code), when .Net can read it at once? Your file is not larger > 100 MB, I assume? Then after getting the array, you simply XOR each element with your "random" value. If you dont need to be compatible to old versions, you might better use Random. Or maybe even better ... USE REAL ENCRYPTION (in .Net it's built-in!)

' 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)

Same for "encrypting". Convert the string to an array of byte (=ASCII values), XOR it and then write it back in ONE operation.

See the dangers of 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

The output is 65 -> 65, 192 -> 63

Upvotes: 1

Related Questions