Results 1 to 5 of 5

Thread: [RESOLVED] XOR'ing in C#

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] XOR'ing in C#

    Hi I need little help here

    Here is my given string "8f43ae1a3319c649cc57408b6a392d6b"

    8f xor 33 xor cc xor 6a =1a
    43 xor 19 xor 57 xor 39 =34
    ae xor c6 xor 40 xor 2d =05
    1a xor 49 xor 8b xor 6b =b3

    1a3405b3 and 1ffffff or 2000000 = 23405B3


    How to apply that using C#?
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: XOR'ing in C#

    Hi, Try this, it should help you get going. What I did is I used regular expressions to split your input string and then I used two FOR loops to get the values the same way you posted.

    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using System.Text.RegularExpressions;
    10.  
    11.  
    12. namespace test
    13. {
    14.     public partial class Form1 : Form
    15.     {
    16.         public Form1()
    17.         {
    18.             InitializeComponent();
    19.         }
    20.  
    21.         private void button1_Click(object sender, EventArgs e)
    22.         {
    23.             byte[] valuearray = new byte[4];
    24.             string s = "8f43ae1a3319c649cc57408b6a392d6b";
    25.             Regex rx = new Regex(@"\w{2}");
    26.             MatchCollection mc = rx.Matches(s);
    27.  
    28.  
    29.             int n = mc.Count / 4;
    30.             for (int i = 0; i < n; i++)
    31.             {
    32.                 for (int j = 0; j < n; j++)
    33.                 {
    34.                     valuearray[j] = Convert.ToByte(mc[i + (j * n)].Value, 16);
    35.                 }
    36.                 int value = valuearray[0] ^ valuearray[1] ^ valuearray[2] ^ valuearray[3];
    37.                 MessageBox.Show(value.ToString("X2"));
    38.             }              
    39.         }
    40.     }
    41. }

    I didn't do that part though :
    Code:
    1a3405b3 and 1ffffff or 2000000 = 23405B3
    as I don't know where you got the last 2 values from, but I think you should be able to figure it out with the sample I posted.

    I hope this helps you.
    Last edited by stlaural; Mar 9th, 2010 at 07:27 AM. Reason: cause I can
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: XOR'ing in C#

    It works
    BTW I modify some to suite my needs thanks.

    Code:
    private void button2_Click(object sender, EventArgs e)
            {
                byte[] valuearray = new byte[4];
                string s = "8f43ae1a3319c649cc57408b6a392d6b";
                Regex rx = new Regex(@"\w{2}");
                MatchCollection mc = rx.Matches(s);
                StringBuilder sb = new StringBuilder();
    
                int n = mc.Count / 4;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        valuearray[j] = Convert.ToByte(mc[i + (j * n)].Value, 16);
                    }
                    int value = valuearray[0] ^ valuearray[1] ^ valuearray[2] ^ valuearray[3];
                    
                    sb.Append(value.ToString("X2"));
                    textBox3.Text = sb.ToString();
    
                }
            }
    1a3405b3 and 1ffffff or 2000000 = 23405B3

    green color is the result of that xor
    I need to AND and OR to that value to get some codes in red color
    Last edited by dr_aybyd; Mar 9th, 2010 at 08:48 AM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  4. #4
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: XOR'ing in C#

    Oh I see, its all about color. I was wondering.

    Glad you got it working.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: XOR'ing in C#

    I got it all works
    thanks stlaural
    Here is my final codes
    csharp Code:
    1. private void button2_Click(object sender, EventArgs e)
    2.         {
    3.             byte[] valuearray = new byte[4];
    4.             string s = "8f43ae1a3319c649cc57408b6a392d6b";
    5.             Regex rx = new Regex(@"\w{2}");
    6.             MatchCollection mc = rx.Matches(s);
    7.             StringBuilder sb = new StringBuilder();
    8.  
    9.             int n = mc.Count / 4;
    10.             for (int i = 0; i < n; i++)
    11.             {
    12.                 for (int j = 0; j < n; j++)
    13.                 {
    14.                     valuearray[j] = Convert.ToByte(mc[i + (j * n)].Value, 16);
    15.                 }
    16.                 int value = valuearray[0] ^ valuearray[1] ^ valuearray[2] ^ valuearray[3];
    17.  
    18.                 sb.Append(value.ToString("X2"));
    19.                 textBox3.Text = sb.ToString();
    20.                 int myCodes = (0x1a3405b3 & 0x1ffffff) | 0x2000000;
    21.                 string newCodes = myCodes.ToString("X2");
    22.  
    23.                 String hexNumber = newCodes;
    24.                 int hextodecs = Int32.Parse(hexNumber, NumberStyles.HexNumber);
    25.                 //MessageBox.Show(i.ToString());
    26.  
    27.  
    28.                 textBox4.Text = hextodecs.ToString();
    29.             }
    30.         }
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width