-
Sep 13th, 2022, 04:30 AM
#1
Thread Starter
New Member
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.
-
Sep 13th, 2022, 09:47 AM
#2
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)
-
Sep 14th, 2022, 03:58 AM
#3
Thread Starter
New Member
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.
-
Sep 14th, 2022, 04:15 AM
#4
Thread Starter
New Member
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.
-
Sep 14th, 2022, 02:29 PM
#5
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|