简体   繁体   中英

VB.NET Dealing with large integers

So I am making a file crypter so that I can encrypt my VB.NET Application that I am making so that people can't decompile it. I had made this in C# and am transfering it to VB.NET, Everything worked fine in C# but once I had re-written the code in VB.NET i get this error inside of my RC4 Encryption method:

'Arithmetic operation resulted in an overflow.'

The error is occuring here:

Dim t As Int64 = (s(i) + s(j)) Mod 256

This is the same code above in c#:

int t = (s[i] + s[j]) % 256;

Is there anyway to make that calculation with it erroring? And why does it work in C# but not VB.NET?

Check your advanced compile options in the VB.NET project (project properties -> Compile -> Advanced Compile Options...). Compare with your C# project. The VB version may be checking for integer overflows, and the C# version not.

When I run a test in VB, this code generates the exception:

Dim x As Int16 = Int16.MaxValue
Dim y As Int16 = Int16.MaxValue

Dim t As Int64 = (x + y) Mod 256

But this code does not:

Dim t As Int64 = (32767 + 32767) Mod 256

If I turn off overflow checking, both sets of code complete without error.

Check values of each variable in the equation to see if there might be an overflow occurring based upon the addition part. The datatype of the result of the addition operation is based upon the datatypes of the variables/values involved in the operation. Those values may be yielding a result too large for the derived datatype.

In .Net 4.0 there is a new type called BigInteger: http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

Although I think the solution you really want is to obfuscate your code so even if someone decompiles they wont be able to read it: Visual Studio > Tools Menu > Dotfuscator

If you want to completely protect your code your going to have to pay for something like Remotesoft's Salamander

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM