0

I am attempting to create a binary file that is not readable by notepad in windows. This file needs to contain text information. The current code I run is readable in notepad (with a few extra characters here and there, but still human readable). Any assistance is greatly appreciated.

Using writer As BinaryWriter = New BinaryWriter(File.Open("file.bin", FileMode.Create))
        writer.Write(rtbWriter.Text)
End Using
  • You might want to explain in terms of *what* you are trying to do rather than *how*. This is an XY question because it amounts to *I want to build a car than that does not run on gas and put gas in it* If you put text in the file, Notepad will see it and show and the user can read it. – Ňɏssa Pøngjǣrdenlarp Dec 29 '14 at 15:12
  • @Plutonix I did state that. Essentially I am trying to limit access to a program with a key file. I want to be able to build a key file that contains data such as "User X, Access Y". But I don't want any user to be able to open the file and see this in a text editor and change it. The current project is the key file builder. I need a user of that program to be able to enter "User X, Access Y", and the program convert it to some kind of binary characters that a txt editor wont auto convert back. – KnightsOfTheRoun Dec 29 '14 at 15:39

2 Answers2

1

All files can be read by notepad - whether it is binary or not. If you don't want the text to be readable (or to be more accurate - understandable), consider using encryption.

EDIT: For an introduction on how to use encryption, see the link below to see how to use the 3DES cryptographic service provider in VB.NET: simple encrypting / decrypting in VB.Net

Community
  • 1
  • 1
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – jazzurro Dec 29 '14 at 15:30
  • @jazzurro This is **not** a link only answer. I suggest you read: [Your answer is in another castle: when is an answer not an answer?](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer). – Bjørn-Roger Kringsjå Dec 29 '14 at 16:06
0

*A more sophisticated approach would chain together the File Stream and Crypto Stream...

...but here's a very simple example showing how to encrypt/decrypt individual strings so you have something to play with and learn from:

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class Form1

    Private Key As String = "SomeRandomKeyThatIsHardCoded"

    Private data As New List(Of String)
    Private DataFileName As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "SomeFile.txt")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Some data to play with:
        data.Add("User X, Access Y")
        data.Add("User Y, Access Z")
        data.Add("User Z, Access A")
        ListBox1.DataSource = data
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Write out each entry in encrypted form:
        Using SW As New StreamWriter(DataFileName, False)
            For Each entry As String In data
                SW.WriteLine(Crypto.Encrypt(entry, Key))
            Next
        End Using
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        data.Clear()
        ListBox1.DataSource = Nothing

        ' Read each encrypted line and decrypt it:
        Using SR As New System.IO.StreamReader(DataFileName)
            While Not SR.EndOfStream
                data.Add(Crypto.Decrypt(SR.ReadLine, Key))
            End While
        End Using

        ListBox1.DataSource = data
    End Sub

End Class

Public Class Crypto

    Private Shared DES As New TripleDESCryptoServiceProvider
    Private Shared MD5 As New MD5CryptoServiceProvider

    Public Shared Function MD5Hash(ByVal value As String) As Byte()
        Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
    End Function

    Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
        DES.Key = Crypto.MD5Hash(key)
        DES.Mode = CipherMode.ECB
        Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
        Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
    End Function

    Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
        Try
            DES.Key = Crypto.MD5Hash(key)
            DES.Mode = CipherMode.ECB
            Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
            Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
            Return ""
        End Try
    End Function

End Class
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40