对文本框Visual Basic 2010中的HEX值进行XOR

时间:2010-12-14 03:03:42

标签: vb.net visual-studio-2010

你好,我有一个问题,我可以自己解决,Vb.net 2010

我有:

  1. Textbox1.text with“4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D”
  2. Textbox2.text with“1b”
  3. 使用以下代码的命令按钮:

    Dim M As String = TextBox1.Text
    Dim hex2 As String = TextBox2.Text
    Dim R
    ' StringTest2 += Chr(Convert.ToInt32(Mid(StringTest1, i, 2), 16)).ToString
    For i = 1 To M.Length Step 2
        TextBox1.Text = TextBox1.Text & "-"
        Dim dec1 As Integer = Mid(M, i, 2).ToString
        Dim dec2 As Integer = Convert.ToInt32(hex2, 16)
        ' MsgBox(dec2)
        Dim result As Integer = dec1 Xor dec2
        R = R & result
    Next
    
    
    TextBox1.Text = R
    

  4. Textbox1可以根据需要进行更改,但在这种情况下,它是“4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D”hex。 Textbox2包含XOR值......

    我只想简单地将textbox1中的每个值与“4B XOR 0x1b”等的1B进行Xor等等。

    当我使用此代码执行此操作时,他会给我:“从数字转换时,值必须小于无穷大” 当我在他给我结果的代码之前放了一个“On error resume next”:“2731313830191927282727276528242727272727”

    xor'ing ONE time的正确结果“4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D”与0x1b(十进制27)是“50 55 7A 43 01 1C 1B D4 8B 68 1B 1B 16”< / p>

    如果我们将Xor再次应用于结果“50 55 7A 43 01 1C 1B D4 8B 68 1B 1B 16”,我们必须找到第一个十六进制字符串作为回复“4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D “

    我在这里上传代码hxxp://dl.free.fr/iG2mDDdVh

3 个答案:

答案 0 :(得分:1)

我相信这就是你要做的事情:

    Dim input As String = "4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D"
    Dim sb As New System.Text.StringBuilder()

    Dim xorhex As String = "1B"
    Dim xornum As Integer = Integer.Parse(xorhex, Globalization.NumberStyles.HexNumber)

    For Each numhex As String In input.Split(" "c)
        Dim num As Integer = Integer.Parse(numhex, Globalization.NumberStyles.HexNumber)
        sb.Append((num Xor xornum).ToString("X2")).Append(" ")
    Next

    Console.WriteLine(sb.ToString())

您应该能够轻松切换变量以匹配您的代码 - 我特别想发布此示例,以便在控制台应用环境中运行时编译和显示信息。

答案 1 :(得分:0)

Xor / unXor的最终工作代码是相同的十六进制数:

  Dim input As String = TextBox1.Text
    Dim sb As New System.Text.StringBuilder()

    Dim xorhex As String = TextBox2.Text
    Dim xornum As Integer = Integer.Parse(xorhex, Globalization.NumberStyles.HexNumber)

    For Each numhex As String In input.Split(" "c)
        Dim num As Integer = Integer.Parse(numhex, Globalization.NumberStyles.HexNumber)
        sb.Append((num Xor xornum).ToString("X2")).Append(" ")
    Next

    'Console.WriteLine(sb.ToString())
    TextBox1.Text = sb.ToString
    TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Length - 1, 1)

答案 2 :(得分:0)

我已将@ BeemerGuy的代码转换为封装函数,可能会让您更轻松。如果这适合你,请接受@ BeemerGuy的答案,而不是我的答案。

Public Shared Function XORHexValues(ByVal input As String, ByVal xorWith As String) As String
    Dim sb As New System.Text.StringBuilder()

    'Convert the string representation of our byte to a true byte
    Dim xornum As Byte = Byte.Parse(xorWith, Globalization.NumberStyles.HexNumber)
    Dim Num As Byte = Nothing

    'Loop through each string "byte" in the text
    For Each numhex As String In input.Split(" "c)
        'Conver the string to a true byte
        Num = Byte.Parse(numhex, Globalization.NumberStyles.HexNumber)
        'Add to our output text with a trailing space
        sb.Append((Num Xor xornum).ToString("X2")).Append(" ")
    Next

    'Return our string removing the last trailing space
    Return sb.ToString().TrimEnd(" "c)
End Function

假设你有一个名为Button1的按钮:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Initial value
    Dim StartValue = "4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D"
    'String (byte) to XOR with
    Dim XORWith As String = "1B"
    'Value that we expect to get from our function
    Dim ExpectedValue = "50 55 7A 43 01 1C 1B D4 8B 68 1B 1B 16"
    'XOR the original string
    Dim Result1 = XORHexValues(StartValue, XORWith)
    'XOR the output to get our original string back
    Dim Result2 = XORHexValues(Result1, XORWith)

    ''This code just proves that it works
    'Test the first result with what we expected
    Trace.WriteLine("Round 1 matches expected: " & If(String.Compare(ExpectedValue, Result1, StringComparison.InvariantCultureIgnoreCase) = 0, "yes", "no"))
    'Test the second result with the original string
    Trace.WriteLine("Round 2 matches original: " & If(String.Compare(StartValue, Result2, StringComparison.InvariantCultureIgnoreCase) = 0, "yes", "no"))
End Sub