Results 1 to 3 of 3

Thread: [RESOLVED] convert Xor C++ to VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Resolved [RESOLVED] convert Xor C++ to VB6

    I am struggling to convert the below code from C++ to VB6, any chance of some help?

    i can see the constant "CSTST" is used as the xor key for the first 5 chars of the string, its the "buffit[8 + i] = i * buffit[i];" which i cant get my head around.

    TIA

    Code:
    void Do_enc(uint8_t *buffit) {
        const char cst[] = "CSTST";
        uint8_t i;
    
        for (i = 0; i < 8; i++) {
            buffit[8 + i] = i * buffit[i];
            if (i <= 5) {
                buffit[i] ^= cst[i];
            }
        }
    }
    Last edited by k1nk0; May 1st, 2012 at 06:13 AM. Reason: [Resolved]

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: convert Xor C++ to VB6

    The original code seems to have a bug leading to a Subscript Error in a safe language like VB6. C and C++ often play fast and loose with many things, making them extremely hazardous except in the hands of a competent and hypervigilant programmer. They seem to be playing dangerous games with character encoding here too.

    If we fix the bug and make an assumption about character encoding (which might be off base) we can easily arrive at this conversion:
    Code:
    Private Sub Do_enc(ByRef buffit() As Byte)
        Const cst_chars = "CSTST"
        Dim cst() As Byte
        Dim i As Integer
        
        cst = StrConv(cst_chars, vbFromUnicode)
    
        For i = 0 To 7
            buffit(8 + i) = (i * buffit(i)) And &HFF
            If i <= 4 Then 'Original code had a bug here.
                buffit(i) = buffit(i) Xor cst(i)
            End If
        Next
    End Sub
    However because we have to make an assumption about the character encoding and because the silly bug in that code (a.) probably doesn't crash the original program you cribbed this from and (b.) might well impact the result... all bets may be off.

    But if we hazard a guess that the original compiler's implementation of const char arrays pads with a guard byte of 00 at the end and that they are really using ANSI based on the same codepage as you are using or your inputs stick to the printable ASCII subset of ANSI (which is almost always identical for all locales)... this conversion might be "good enough."


    In my sample program (attached) I'm displaying the output using a two-line "over and under hex" approach. I.e. in the screenshot:

    The first output byte is &H17, the second output byte is ASCII/ANSI "6" and so on.
    Attached Images Attached Images  
    Attached Files Attached Files

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Re: convert Xor C++ to VB6

    Thanks for the informative response. Very helpful and appreciated.

    I too thought there was something up at the point you have pointed out as the original loop was looking for 6 items in an array with ubound of 5 (or 4 as we are based at 0)

    Thanks again.

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