XOR on HEX value in textbox Visual Basic 2010
I have Vb.net 2010.
I have:
- Textbox1.text with "4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D"
- Textbox2.text with "1b"
Command Button with this code:
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
Textbox1 can be changed as I wish but in this case it's "4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D" hex. Textbox2 contain the value to XOR with...
I want simply to Xor each value in textbox1 with 1B like "4B XOR 0x1b" etc...
When I do it with this code he gives me: "When casting from a number the value must be less than infinity" When I put a "On error resume next" before the code he gave me in result : "2731313830191927282727276528242727272727"
The correct result on xor'ing ONE time "4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D" with 0x1b (27 decimal) is "50 55 7A 43 01 1C 1B D4 8B 68 1B 1B 16"
If we apply Xor another time on the result "50 55 7A 43 01 1C 1B D4 8B 68 1B 1B 16" we must found the Firs开发者_JAVA百科t Hex string as reply "4B 4E 61 58 1A 07 00 CF 90 73 00 00 0D"
I believe this is what you're trying to do:
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())
You should be able to easily switch the variables to match your code -- I specifically wanted to post this as an example that compiles and displays information if run in a console app environment.
The final working code for Xor/unXor the same Hex number is :
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)
I've converted @BeemerGuy's code to an encapsulated function that might make things easier for you. If this works for you please accept @BeemerGuy's answer, not mine.
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
And assumming you've got a button called 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
精彩评论