Results 1 to 5 of 5

Thread: Problem porting JS function to VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    3

    Problem porting JS function to VB.NET

    With my very basic knowledge of both worlds i am trying to use a JS function in my VB.NET application.
    This is the working function in JS:

    Code:
    function testjs(r, t) {
        var n = []
          , e = void 0
          , o = void 0
          , A = void 0
          , i = [];
        for (e = 0; e < 256; e++)
            n[e] = e;
        for (e = 0,
        o = 0; e < 256; e++)
            o = (o + n[e] + r[e % r.length]) % 256,
            A = n[e],
            n[e] = n[o],
            n[o] = A;
        for (var c = 0, f = 0, u = 0; c < t.length; c++)
            u = (u + n[f = (f + 1) % 256]) % 256,
            A = n[f],
            n[f] = n[u],
            n[u] = A,
            i.push(t[c] ^ n[(n[f] + n[u]) % 256]);
        return i
    }

    This is my version in VB.NET:

    Code:
     Function testvb(ByVal input1 As String, ByVal input2 As String) As String
            Dim keybytes As Byte() = StringToByteArray(input1)
            Dim msgbyte As Byte() = FromHex(input2)
            Dim n(255) As Byte
            Dim e As Integer = 0
            Dim o As Integer = 0
            Dim A As Integer = 0
            Dim i As Byte() = New Byte() {}
    
            For e = 0 To 255
                n(e) = BitConverter.GetBytes(e)(0)
            Next
    
            For e = 0 To 255
                o = (o + n(e) + keybytes(e Mod keybytes.Length)) Mod 256
                A = n(e)
                n(e) = n(o)
                n(o) = BitConverter.GetBytes(A)(0)
            Next
    
            Dim f = 0
            Dim u = 0
            For c = 0 To msgbyte.Length - 1
                f += 1
                u = (u + n(f Mod 256)) Mod 256
                A = n(f)
                n(f) = n(u)
                n(u) = BitConverter.GetBytes(A)(0)
                ReDim Preserve i(i.Length)
                i(i.Length - 1) = msgbyte(c) Xor n((n(f) + n(u)) Mod 256)
            Next
            Return ByteArrayToString(i)
        End Function
    With same inputs, the code seems to be fine until the second time it tries to input a value into i.

    This part
    Code:
    i(i.Length - 1) = msgbyte(c) Xor n((n(f) + n(u)) Mod 256)
    is where the code breaks.
    While debugging, all the values seem to be the same as with using the JS function.

    The error is the following:
    system.overflowexception arithmetic operation resulted in an overflow
    I tried changing the datatypes for e, o and A from Int to Long, BigInt... but no change.
    I am kinda out of ideas here.
    Any help would be greatly appreciated.

  2. #2
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: Problem porting JS function to VB.NET

    Couple of notes here: You're evidently ripping javascript that's been minified. For the love of God, give those variables descriptive names. Secondly you evidently didn't try changing the datatype of I to something larger. Byte only allows values of 0-255. I would first though just try putting parantheses around the first part of the function so mod can only return the correct range. It could be an order-of-operations issue. i(i.Length - 1) = (msgbyte(c) Xor n((n(f) + n(u))) Mod 256)
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    3

    Re: Problem porting JS function to VB.NET

    Call me the ripper then^^

    Because mods did have to check my question first, i figured it out before. You were right with your assumption that the problem were byes only allowing values from 0-255. Converting them to int before comparison worked like a charm.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2022
    Posts
    3

    Re: Problem porting JS function to VB.NET

    Since you seem to know JS, might i ask another question?
    Is there any difference between JS as .NET how characters are converted to bytes?
    In some cases the byte array is a little bit longer in VB then in JS.

    When using
    Code:
    var chars = encodeURIComponent(input).split("")
    on JS side and
    Code:
    Dim chars As Char() = (Uri.EscapeUriString(input)).ToCharArray
    on .NET side

    I also noticed a difference in length.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Problem porting JS function to VB.NET

    That's. different question that deserves its own thread... but ... if I had to guess, there's a difference between encoding for the URL, and escaping it ... I'd run it through the encode and the escaper and see what the differences are. That should be the difference between the lengths. It's two different types of apples.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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