Results 1 to 13 of 13

Thread: Going crazy with encoding...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Going crazy with encoding...

    Some one can please explain me ALL about encoding????
    All I want to do is doing a simple transformation of a string, and I can't believe it's not possible!!!!!!!!

    E.G.

    dim Mystring as string="ABCDEFG"

    I want to take the ascii value of each character in this string and add 1. So that string will be:

    "BCDEFGH".

    What's the simplest way to do that??!??!?

    HEEEEEEEEEEEEEEEEEEEEELP!!!!!!!!!!!!!!!!!!!!!


    Thanks,
    Xmas79
    Learn, this is the Keyword...

  2. #2
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    something like:

    VB Code:
    1. Dim Mystring As String = "ABCDEFG"
    2.       Dim myCharArray As Char() = Mystring.ToCharArray
    3.       Dim newString As String
    4.       For charIndex As Integer = 0 To myCharArray.Length - 1
    5.          newString &= Chr(Asc(myCharArray(charIndex)) + 1)
    6.       Next
    7. MsgBox(newString)

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    or:
    VB Code:
    1. [COLOR=BLUE]Dim[/COLOR] Mystring [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = "ABCDEFG"
    2. [COLOR=BLUE]Dim[/COLOR] x [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer
    3. Dim[/COLOR] retString [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String
    4. For[/COLOR] x = 0 [COLOR=BLUE]To[/COLOR] Mystring.Length - 1
    5. retString += Convert.ToChar(Asc(Mystring.Chars(x)) + 1)
    6. [COLOR=BLUE]Next
    7. [/COLOR]MessageBox.Show(retString)
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Nooooooo..... Incredible! But, the same code in VB and C# produces different results!!!! Why????? For instance the code is the VB.NET and C# translation from C++ of the "DC++" client...
    Learn, this is the Keyword...

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Xmas79
    Nooooooo..... Incredible! But, the same code in VB and C# produces different results!!!! Why????? For instance the code is the VB.NET and C# translation from C++ of the "DC++" client...
    It won't provide different results AT ALL since both are accessing the same function with the same information.

    You're doing something wrong and Ms Cleo does not frequent these boards, so you're going to have to post the code

  6. #6
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158
    The above examples use the Microsoft.VisualBasic namespace. Why didn't you post on a C# board?

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    in C# it's easy also, here's a quick example
    VB Code:
    1. [COLOR=BLUE]private[/COLOR] [COLOR=BLUE]void[/COLOR] button1_Click([COLOR=BLUE]object[/COLOR] sender, System.EventArgs e)
    2. {
    3.     [COLOR=BLUE]char[/COLOR][] Mystring="ABCDEFG".ToCharArray();
    4.     [COLOR=BLUE]string[/COLOR] retString=[COLOR=BLUE]string[/COLOR].Empty;
    5.     [COLOR=BLUE]int[/COLOR] x;
    6.     [COLOR=BLUE]for[/COLOR](x=0;x!=Mystring.Length;x++)
    7.     {
    8.         retString+=Convert.ToChar((([COLOR=BLUE]char[/COLOR])Mystring[x])+1);
    9.     }
    10.         MessageBox.Show(retString);
    11. }
    hope it helps.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    As I said, they produce different results... Can it be because the string I translate is composed with ascii set and not only by letters and numbers? And another thing: The right code is the C#, and I do know nothing about C# and I know VB.NET, so I'm sure I'm not doing strange things!!!

    There's a little problems in VB when you have to manipulate a string byte by byte, and it's better to do it in C#... Encoding.UTF8, Encoding.UFT7... WHAT'S THAT??!??! In C# simply take that byte and transform it! I also tried the code you all posted, with
    Convert.ToChar(Asc(Mystring.Chars(x)) + 1)
    or
    Chr(Asc(myCharArray(charIndex)) + 1)
    but the result is the same. Both this method produces THE SAME result, but THIS result is different from the C# code, that is the RIGHT one.

    Thanks.
    Learn, this is the Keyword...

  9. #9
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i'm not sure if you want vb.net or c# but to do the same in vb.net as the c# example i made, you need to do this...
    VB Code:
    1. [COLOR=BLUE]Private[/COLOR] [COLOR=BLUE]Sub[/COLOR] Button1_Click([COLOR=BLUE]ByVal[/COLOR] sender [COLOR=BLUE]As[/COLOR] System.Object, [COLOR=BLUE]ByVal[/COLOR] e [COLOR=BLUE]As[/COLOR] System.EventArgs) [COLOR=BLUE]Handles[/COLOR] Button1.Click
    2.         [COLOR=BLUE]Dim[/COLOR] Mystring [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Byte[/COLOR]() = System.Text.Encoding.ASCII.GetBytes("ABCDEFG".ToCharArray())
    3.         [COLOR=BLUE]Dim[/COLOR] retString [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]String[/COLOR] = [COLOR=BLUE]String[/COLOR].Empty
    4.         [COLOR=BLUE]Dim[/COLOR] x [COLOR=BLUE]As[/COLOR] [COLOR=BLUE]Integer
    5.  
    6. [/COLOR]        [COLOR=BLUE]For[/COLOR] x = 0 [COLOR=BLUE]To[/COLOR] Mystring.Length - 1
    7.             retString += Convert.ToChar(Mystring(x) + 1)
    8.         [COLOR=BLUE]Next
    9.  
    10. [/COLOR]        MessageBox.Show(retString)
    11.     [COLOR=BLUE]End[/COLOR] [COLOR=BLUE]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Let's explain...

    I was trying to do a little client for the "Direct Connect" network. To do this I took the protocol code from a well done and well known program called DC++. This program is written in C++. I was trying to convert this code into a VB.NET code.

    Now, this network works in this way:

    Client connects to server.
    Server sends a string, let's say SERVERSTRING.
    Client must respond to this string with another string, let's call CLIENTSTRING, that is elaborated from the SERVERSTRING. If the elaborated string is wrong the client can't get authenticate.

    Now, with the help of a net sniffer, I took a SERVERSTRING and a CLIENTSTRING. All I've done is take the SERVERSTRING, elaborate it with the VB code and the C# code. The VB code returns a different string, the C# code the right string. This means that a client can't get authenticate if the elaboration is done with the VB code.
    Clear at this point?

    Now, I figured out the same problem when the client have to send the string. Infact, socket implementation wants a "array of bytes", and the problem is there: VB can't handle strings as well as C and C++ and C# do!!!! I couldn't get authenticate with VB code!! What I've done:
    Made the conversion of CLIENTSTRING (elaborated with the C# code) from string to byte array (in VB) and sent (in VB). I tried with the "two variations" code you posted! Nothing to do...
    Instead, I wrote the "send" routine in C# that manages strings very well, transformed string into an array of byte in C#, and sent always in C#. I got authentication...

    How is it possible if C# and VB use the same functions and library??!??!?!??!?!
    Learn, this is the Keyword...

  11. #11
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    maybe your sending method is off? You should prolly post all the code that deals with the encoding and sending the strings.

    BTW: Moving a character up one place is not the safest method of encryption
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  12. #12
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103
    As I said, they produce different results... Can it be because the string I translate is composed with ascii set and not only by letters and numbers? And another thing: The right code is the C#, and I do know nothing about C# and I know VB.NET, so I'm sure I'm not doing strange things!!!
    Try this.
    Is in C# but is quite simple to convert to VB (although you can use http://www.kamalpatel.net/ConvertCSharp2VB.aspx to convert)

    PHP Code:
    Byte[] System.Text.UnicodeEncoding.ASCII.GetBytes(mystring);
    for ( 
    code.Length += 1) {
        
    b[i]++;
    }
    mystring System.Text.UnicodeEncoding.ASCII.GetString(b); 
    Bye
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Thank you all...

    Thank you all for replying, and excuse me for this late reply, I wasn't home

    <ABX:
    Moving up one character was only an example. What I really do with these strings is xoring bytes, and replacing some special character, like byte 0, with strings like %/DCN000/%.

    My post method wasn't off. Now I have that code nomore, but for instance it was only a thing like this:
    VB Code:
    1. Private Sub SendMethod(ByVal MyString as String)
    2.     dim buf() as byte
    3.     buf=Encoding.ASCII.GetBytes(MyString)
    4.     MySocket.Send(buf)
    5. End sub
    As I already said, I tried to change the code with "Encoding.xxx.GetBytes" with no results! The problem in fact is there.
    Then I wrote a piece of code in C# like this for translating from string to bytes:
    Code:
    byte[] b=new byte[mystring.length];
    int i;
    for (i=0; i<mystrring.length; i++) b[i]=(byte) mystring[i];
    return b;
    and for translating from bytes to string:
    Code:
    StringBuilder sb=new StringBuilder();
    int i;
    for (i=0; i<mystring.length; i++) sb.Append(((char) mystring[i]));
    return sb.ToString();
    Now all works perfectly... Why??? Boohoho???? As you can see this is the only thing I change... Try creating a random string, and make strange operations like xoring bytes, adding, and so on. You'll be surprised to find that the C# version and the VB version produce different results... Or am I wrong at all???
    Learn, this is the Keyword...

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