CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Learn How to Create Dual Mode Windows Services
  • XBox live shutting down for original XBox consoles
  • Windows 7 release candidate (RC) will soon expire
  • Facebook removes Microsoft banner ads

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual Basic Programming > Visual Basic 6.0 Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual Basic 6.0 Programming Ask questions about VB 6.0 (or earlier versions) or help others by answering their question.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 4th, 2009, 12:56 AM
    gaya3_k gaya3_k is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 18
    gaya3_k is an unknown quantity at this point (<10)
    Xor on string--help me please

    hi,
    in my program i am Xor ing 3 strings , i am taking each character in the strings and Xor ing their ASCII values
    but manually if i do Xor operation i am getting a different answer,
    Can someone tell me how this result is coming,
    x="7CA674" first string
    lenx=len(x)

    y="17767F" Second string
    leny=len(y)

    z="251270" Third string
    lenz=len(z)

    and my code is

    For l = 1 To Len(x)

    l Mid$(x, l, 1) = (Asc(Mid$(x, l, 1)) Xor Asc(Mid$(y,leny , 1)) Xor Asc(Mid$(z, lenz, 1)))

    Next l
    and my result is
    x = 376456
    Reply With Quote
      #2    
    Old November 4th, 2009, 01:00 AM
    gaya3_k gaya3_k is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 18
    gaya3_k is an unknown quantity at this point (<10)
    Re: Xor on string--help me please

    hi,
    in my program i am Xor ing 3 strings , i am taking each character in the strings and Xor ing their ASCII values
    but manually if i do Xor operation i am getting a different answer,
    Can someone tell me how this result is coming,
    x="7CA674" first string
    lenx=len(x)

    y="17767F" Second string
    leny=len(y)

    z="251270" Third string
    lenz=len(z)

    and my code is
    Quote:
    Originally Posted by gaya3_k View Post

    For l = 1 To Len(x)

    l Mid$(x, l, 1) = (Asc(Mid$(x, l, 1)) Xor Asc(Mid$(y,leny , 1)) Xor Asc(Mid$(z, lenz, 1)))

    Next l
    and my result is
    x = 376456
    Reply With Quote
      #3    
    Old November 4th, 2009, 04:48 PM
    dglienna's Avatar
    dglienna dglienna is offline
    ex MVP - Visual Basic
    Power Poster
     
    Join Date: Jan 2006
    Location: Chicago, IL
    Posts: 10,288
    dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+)
    Re: Xor on string--help me please

    Bitwise operations can only be executed on BYTE data types. You'd have to convert.

    CBYTE() might help
    __________________
    David CodeGuru Article: Bound Controls are Evil-VB6
    101 Samples: VB & C# VS2008 Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!
    Reply With Quote
      #4    
    Old November 4th, 2009, 10:52 PM
    gaya3_k gaya3_k is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 18
    gaya3_k is an unknown quantity at this point (<10)
    Re: Xor on string--help me please

    Thanks alot for your reply,

    Can u please tell me exactly what CBYTE will do to solve my problem.

    Gayathri.k
    Reply With Quote
      #5    
    Old November 5th, 2009, 02:25 AM
    dglienna's Avatar
    dglienna dglienna is offline
    ex MVP - Visual Basic
    Power Poster
     
    Join Date: Jan 2006
    Location: Chicago, IL
    Posts: 10,288
    dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+) dglienna has a brilliant future (2000+)
    Re: Xor on string--help me please

    Each character is has a byte code. You can't use a part of a character, unless you convert it to a byte

    Take a look at this link:
    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx
    __________________
    David CodeGuru Article: Bound Controls are Evil-VB6
    101 Samples: VB & C# VS2008 Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!
    Reply With Quote
      #6    
    Old November 5th, 2009, 05:26 AM
    WoF's Avatar
    WoF WoF is offline
    Elite Member
     
    Join Date: Jul 2006
    Location: Germany
    Posts: 2,790
    WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+)
    Re: Xor on string--help me please

    No, no.
    You can xor integers as well as ASC values.
    The problem is in the expression
    Code:
     Mid$(x, l, 1) = (Asc(Mid$(x, l, 1)) Xor Asc(Mid$(y,leny , 1)) Xor Asc(Mid$(z, lenz, 1)))
    Look what you are doing: from the y string and the z string you are always taking only ever the last character.
    Make it so:
    Code:
     Mid$(x, l, 1) = (Asc(Mid$(x, l, 1)) Xor Asc(Mid$(y,l , 1)) Xor Asc(Mid$(z, l, 1)))
    l is the loop variable which increments with every step in the for loop. leny and lenz are constantly pointing the last character of y and z.
    Reply With Quote
      #7    
    Old November 6th, 2009, 02:32 AM
    gaya3_k gaya3_k is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 18
    gaya3_k is an unknown quantity at this point (<10)
    Unhappy Re: Xor on string--help me please

    thanks alot for your reply,
    my exact code is
    Code:
    Private Sub XORIt(ByRef data As String, ByRef key As String, ByRef Y As String)
        Dim l As Long
        Dim lonLenKey As Long, lonKeyPos, lonLenTime, lonTmePos As Long
        Dim key_file As String
        key_file = "input\key.txt"
        
        lonLenKey = Len(key)
        
        For l = 1 To Len(data)
        
            lonKeyPos = lonKeyPos + 1
                   If lonKeyPos > lonLenKey Then lonKeyPos = 1
            Mid$(data, l, 1) = (Asc(Mid$(data, l, 1)) Xor Asc(Mid$(key, lonKeyPos, 1)))
              
        Next l
        
        lonLenTime = Len(Y)
        For l = 1 To Len(data)
        
           
            lonTmePos = lonTmePos + 1
            If lonTmePos > lonLenTime Then lonKeyPos = 1
            Mid$(data, l, 1) = (Asc(Mid$(data, l, 1)) Xor Asc(Mid$(Y, lonTmePos, 1)))
              
        Next l
        
          
        
        Close #55
         Open key_file For Output As #55
         Print #55, Hex(data)
         Close #55
        
        
    End Sub
    what i am doing is ,each time my key,data and time will be different values taken from a text file as a string.
    say for example,
    data value is "7CA674"
    key is "17767FA"
    time is "25127000"

    i will take the length of each string and i will do the xor operation, till the length of least string

    my result for these value is 376456
    Please someone justify this answer.

    I am not getting how this result is coming


    Thanks in advance,
    Gayathri.K
    Reply With Quote
      #8    
    Old November 6th, 2009, 12:19 PM
    WoF's Avatar
    WoF WoF is offline
    Elite Member
     
    Join Date: Jul 2006
    Location: Germany
    Posts: 2,790
    WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+) WoF has much to be proud of (1500+)
    Re: Xor on string--help me please

    No, I'm getting a different result. Let me show you how I did it:
    Code:
    Private Sub XORTest()
    'data Value Is "7CA674"
    'key is "17767FA"
    'time is "25127000"
    'result is 376456
    
      Dim dat$, key$, tim$ 'the strings
      Dim ld%, lk%, lt%, lm1%, lmin% 'being the lengths and minimum
      Dim i%, res$
      
      dat = "7CA674"
      key = "17767FA"
      tim = "25127000"
      
      ld = Len(dat)
      lk = Len(key)
      lt = Len(tim)
      
      'get the minimum in 2 steps for better overview
      lm1 = IIf(ld < lk, ld, lk)
      lmin = IIf(lt < lm1, lt, lm1)
      'lmin is now the shortest length
      
      For i = 1 To lmin
        res = res + Chr$(Asc(Mid$(dat, i, 1)) Xor Asc(Mid$(key, i, 1)) Xor Asc(Mid$(tim, i, 1)))
      Next
      MsgBox res
        
    End Sub
    The result is "4AG27B". I'm quite confident of the result.
    I get the min length of all three strings, then in a loop I xor the ASC values of corresponding characters in same positions and add the Chr$() of the resultin value to the resulting string res$.
    I'm confident, because I use a similar algorithm to xor strings for simple encoding.

    Let me hint, that you are making a common error.
    Code:
       Dim lonLenKey As Long, lonKeyPos, lonLenTime, lonTmePos As Long
    lonKeyPos and lonLenTime wont be long. You have to declare As Long for every variable individually. Without it, it becomaes a variant.
    If lazy for writing, you can use the shortcuts
    Dim a&, b&, c&, d$, i%
    a, b, and c become Long, d becomes string and i% will be integer.

    Also you don't need longs for this. Integer is sufficient.

    Last edited by WoF; November 6th, 2009 at 12:22 PM.
    Reply With Quote
      #9    
    Old November 6th, 2009, 10:39 PM
    gaya3_k gaya3_k is offline
    Junior Member
     
    Join Date: Oct 2009
    Posts: 18
    gaya3_k is an unknown quantity at this point (<10)
    Smile Re: Xor on string--help me please

    Hi WoF
    Thanks alot yar..............
    I will try out as u said...
    And will tell you

    THANKS,
    Gayathri.k
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual Basic Programming > Visual Basic 6.0 Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:55 AM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.