Results 1 to 11 of 11

Thread: Xoring String

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    4

    Xoring String

    Hi. Which is the best way to xor two strings
    which are in binary mode?
    For example
    Text1=011000100 01110001 ....
    Text2=001010111 00100010 ...

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    819

    Re: Xoring String

    convert your strings in binary numbers and xor the binary numbers
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,290

    Re: Xoring String

    The Convert class has methods that will convert between numbers and Strings using base 2.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,660

    Re: Xoring String

    By XORING I assume you mean flipping the bits. There are several ways to do that. What are you going to use that for?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,290

    Re: Xoring String

    Quote Originally Posted by Peter Swinkels View Post
    By XORING I assume you mean flipping the bits. There are several ways to do that. What are you going to use that for?
    XORing two values would mean creating a result where each bit is set if and only if the corresponding bit is set in one and only one of the operands. XORing a single value is basically doing that with a second value where all bits are set.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    4

    Re: Xoring String

    Quote Originally Posted by Peter Swinkels View Post
    By XORING I assume you mean flipping the bits. There are several ways to do that. What are you going to use that for?
    I have two messages and I want to XOR them(kind of encryption) but the two messages each character is in binary mode.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,290

    Re: Xoring String

    Quote Originally Posted by requiem78 View Post
    I have two messages and I want to XOR them(kind of encryption) but the two messages each character is in binary mode.
    What do you mean by "binary mode"? Are you talking about actually binary data, as opposed to text containing "1" and "0" characters? Are you aware that XORing two arbitrary values is a one-way process, i.e. you cannot get the original values back again? If you have one arbitrary value and one known value then you can use the known value to get the arbitrary value again.

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2009
    Posts
    4

    Re: Xoring String

    Quote Originally Posted by jmcilhinney View Post
    What do you mean by "binary mode"? Are you talking about actually binary data, as opposed to text containing "1" and "0" characters? Are you aware that XORing two arbitrary values is a one-way process, i.e. you cannot get the original values back again? If you have one arbitrary value and one known value then you can use the known value to get the arbitrary value again.
    Assume that the one string is Hallo and its binary str1="01001000 01100001 01101100 01101100 01101111"
    the other is World and its binary str2="01010111 01101111 01110010 01101100 01100100"

    and I want the str1 XOR str2

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,535

    Re: Xoring String

    Quote Originally Posted by requiem78 View Post
    Assume that the one string is Hallo and its binary str1="01001000 01100001 01101100 01101100 01101111"
    the other is World and its binary str2="01010111 01101111 01110010 01101100 01100100"

    and I want the str1 XOR str2
    Here is a solution. It should be apparent that this only works if the strings are the same.

    Code:
            Dim s1 As String = "hello"
            Dim s2 As String = "world"
    
            Dim b1() As Byte = System.Text.Encoding.Default.GetBytes(s1)
            Dim b2() As Byte = System.Text.Encoding.Default.GetBytes(s2)
            Dim rslt As New List(Of Byte)
            For x As Integer = 0 To b1.Length - 1
                rslt.Add(b1(x) Xor b2(x))
            Next
            Stop
            'decode
            For x As Integer = 0 To b2.Length - 1
                rslt(x) = rslt(x) Xor b2(x)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,194

    Re: Xoring String

    I don't understand why you would want to do this because .NET has built-in support for encryption which is fairly easy to use and much more secure.
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,289

    Re: Xoring String

    Quote Originally Posted by dday9 View Post
    I don't understand why you would want to do this because .NET has built-in support for encryption which is fairly easy to use and much more secure.
    9 times out of 10, a question like this is part of a homework assignment. A quasi-nonsensical objective that demonstrates the ability to solve a problem using previously learned (and potentially disparate) concepts.

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