Posts Tagged ‘XOR

03
Apr
09

[VB.Net] String XOR

This XOR is slightly more useful, you can encrypt a string using a byte as the key:

    Public Function StringXOR(ByVal str1 As String, ByVal XORByte As Byte) As String
        StringXOR = ""
        For i As Integer = 0 To str1.Length - 1
            StringXOR &= CChar(ChrW(Asc(str1(i)) Xor XORByte))
        Next
    End Function

Usage:

Debug.WriteLine(StringXOR("sim0n", 222))

Would Output:
­·³î°

and so

Debug.WriteLine(StringXOR("­•³î°", 222))

Would Output:
­sim0n

02
Apr
09

[VB.Net] String/Binary XOR

Got bored and decided to write this function. It can convert the input strings to binary and then XOR’s the strings together and returns a binary output.
Serves very little purpose..
Basic flow is:

  • Check if strings are in binary format
    • If needed, converts
  • Checks each strings length
    • Pads to same length if needed
  • Loops through each character in the first string
  • Compares with corresponding character in second string
    • If they are the same appends 0 to output, else appends 1
  • Returns output
    ''' <summary >
    ''' A function to XOR two strings together
    ''' </summary >
    ''' <param name="str1" > The first string to be XOR'd</param>
    ''' <param name="str2" > The second string to be XOR'd</param>
    ''' <param name="str1binary" > Boolean: Initial = False
    ''' False; str1 requires converting to binary;
    ''' True; str1 is already in binary format</param >
    ''' <param name="str2binary" > Boolean: Initial = False
    ''' False; str2 requires converting to binary;
    ''' True; str2 is already in binary format</param >
    ''' <returns > A binary string representing the two values XOR'd together</returns>
    ''' <remarks > This function serves little purpose.</remarks>
    Public Function StringToBinaryXOR(ByVal str1 As String, ByVal str2 As String, Optional ByVal str1binary As Boolean = False, Optional ByVal str2binary As Boolean = False) As String
        Dim BinaryString1 As String = ""
        Dim BinaryString2 As String = ""
        Dim BinaryOutput As String = ""
        ''If the string is not already binary, convert it
        If str1binary = False Then
            For Each cha As Byte In ASCIIEncoding.ASCII.GetBytes(str1)
                BinaryString1 &= Convert.ToString(cha, 2)
            Next
        Else
            BinaryString1 = str1
        End If
        ''If the string is not already binary, convert it
        If str2binary = False Then
            For Each cha As Byte In ASCIIEncoding.ASCII.GetBytes(str2)
                BinaryString2 &= Convert.ToString(cha, 2)
            Next
        Else
            BinaryString2 = str2
        End If
        ''Pad the strings
        If BinaryString1.Length < BinaryString2.Length Then
            BinaryString1.PadLeft(BinaryString2.Length - BinaryString1.Length, "0")
        ElseIf BinaryString2.Length < BinaryString2.Length Then
            BinaryString2.PadLeft(BinaryString1.Length - BinaryString2.Length, "0")
        End If
        ''Loop through the string comparing each of the values
        For i As Integer = 0 To BinaryString1.Length - 1
            If BinaryString1.Chars(i) = BinaryString2.Chars(i) Then
                BinaryOutput &= 0
            Else
                BinaryOutput &= 1
            End If
        Next
        Return BinaryOutput
    End Function

Usage:
Since it can convert strings for you, you can either input binary, or strings.

        Debug.WriteLine(StringToBinaryXOR("sim0n", "x0r"))
        Debug.WriteLine(StringToBinaryXOR("1110011110100111011011100001101110", "x0r", True))
        Debug.WriteLine(StringToBinaryXOR("sim0n", "11110001100001110010", , True))
        Debug.WriteLine(StringToBinaryXOR("1110011110100111011011100001101110", "11110001100001110010", True, True))

Would All Output:
1110011110100100101010000000011100

To prove that it works correctly, you can then use this outputted value to xor one of the original values, and you will get the other initial input.

        Debug.WriteLine(StringToBinaryXOR("sim0n", "1110011110100100101010000000011100", , True))
        Debug.WriteLine(StringToBinaryXOR("1110011110100111011011100001101110", "1110011110100100101010000000011100", True, True))

Would both output:
0000000000000011110001100001110010 (binary for x0r)

or

        Debug.WriteLine(StringToBinaryXOR("x0r", "1110011110100100101010000000011100", , True))
        Debug.WriteLine(StringToBinaryXOR("11110001100001110010", "1110011110100100101010000000011100", True, True))

Would both output:
1110011110100111011011100001101110 (binary for sim0n)