Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net function to convert text to something confusing

Hi

In my VB.net windows forms project I want to write text files with names that the user won't now.
So I want to use a function to convert a string to a confusing name and then another function
to convert the function back to the original string
I am hoping someone has done this before and can give me some example VB.net code.

Thanks
SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thank you all
I know this is closed, but I would suggest just using a simple substitution cipher (e.g. "X" = "Z", "B" = "N", etc...). The issue with other encryption ciphers is that they will usually produce binary data which cannot be represented using standard, filesystem-compatible characters. For example, let's say that after encryption, the original filename has some bytes that are 0x10 or 0x13, which are line break/carriage feed characters. You would be unable to use those characters in the filename unless you encoded it with something like Base64 (and even then, you'd have to substitute a couple of characters, since Base64 also has a few filesystem-incompatible characters, and also increases the length of the filename itself by around 33%).

So with encryption/encoding, you end up doing a lot more work.

As someone else mentioned earlier, you could use a SHA-1 hash, but that requires a lookup table of some kind in order to convert it back to the original value, and that table would have to be stored somewhere and be accessible to the code (and thus accessible to the end user), so the lookup table itself could be used to easily decipher an original filename.

A substitution cipher is quick to implement and fast and doesn't require any extra encoding, and it's fast to decipher, too. The only catch to it is that if a user were really determined, they could eventually figure it out. But it's nearly impossible to prevent a determined user from determining the technique - especially on a .NET application that can very easily be disassembled back into readable source code.

So ultimately, if it's important that it be difficult for even determined users to decipher, then you would probably need to use multiple ciphers and use a non-.NET language, like C or C++ - something that can be compiled and is not decompiled quite as easily as a .NET app.
Also, if you want an example of a simple substitution cipher:

Subby.vb
Public Class Subby

    Private MapFrom As String
    Private MapTo As String

    Public Sub New()
        MapFrom = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
        MapTo = "VqSR0Ao4gw52bGCZXlaIHtxD9cP1mfipeEMzWkvFuhyJ3UnjTsrK7LdN6YB8OQ"
    End Sub

    Public Function Encode(ByRef CurrentString As String) As String
        Return Swap(CurrentString, MapFrom, MapTo)
    End Function

    Public Function Decode(ByRef CurrentString As String) As String
        Return Swap(CurrentString, MapTo, MapFrom)
    End Function

    Private Function Swap(ByRef CurrentString As String, ByRef _MapFrom As String, ByRef _MapTo As String) As String

        Dim sb As New System.Text.StringBuilder(CurrentString.Length)
        Dim pos As Integer

        For Each c As Char In CurrentString
            pos = _MapFrom.IndexOf(c)
            If (pos >= 0) Then
                sb.Append(_MapTo(pos))
            Else
                sb.Append(c)
            End If
        Next

        Return sb.ToString()

    End Function

End Class

Open in new window


Usage:
        Dim subby As New Subby
        Dim encoded As String = subby.Encode("Hello World.txt")
        Dim decoded As String = subby.Decode(encoded)
        Console.WriteLine(encoded) // "4ikku xuJkf.UsU"
        Console.WriteLine(decoded) // "Hello World.txt"

Open in new window

thanks gr8gonzo. That is very helpful