Question Decrypting XOR

hypermx

Member
Joined
Jun 23, 2009
Messages
5
Programming Experience
Beginner
hey.

I'm trying to make a function to decrypt a xor encryption.

Currently i have this code, But I'm unable to make it work.

VB.NET:
Imports System
Imports System.Text


Public Class Form1

    Public Function dexor(ByVal xor1 As String) As String
        Dim Sbuilder As StringBuilder
        Dim i As Long = 0
        While i < (xor1.Length - 1)
            Sbuilder.Append(i Xor DirectCast(((i Mod 5) + 1), Char))
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        Return xor1
    End Function

End Class


What did i do wrong?
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
Firs thing, StringBuilder needs to be initialized with new. Second, what's this line doing:

VB.NET:
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)

Bobby
 

hypermx

Member
Joined
Jun 23, 2009
Messages
5
Programming Experience
Beginner
To be honest, Then i don't know.

It's because i had a C++ code, That i needed to have converted to VB.Net, So one of my friends gave it a try, And he said that this would be the vb.net version.
 

Robert_Zenz

Well-known member
Joined
Jun 3, 2008
Messages
503
Location
Vienna, Austria
Programming Experience
3-5
Try this instead:

VB.NET:
Public Function dexor(ByVal xor1 As String) As String
        ' The StringBuilder needs to be initiliazed
        Dim Sbuilder As New StringBuilder()

        ' we only need Integer (32-Bit, signed, up to 2 Billion,
        ' if you've got a string with more than 2 Billion characters
        ' then is this the least of your problems. ;) )
        For i As Integer = 0 To xor1.Length
            ' Do yourself a favor and turn Option Strict On!
            Sbuilder.Append(ToChar(i Xor ((i Mod 5) + 1)))
        Next

        ' you should really return the Stringbuilder and NOT xor 1
        dexor = Sbuilder.ToString()
    End Function

Bobby
 
Top Bottom