vb.net - Dex Algorithm Overflow Exception -
i trying develop encryption algorithm
using dex
. seems getting exception
regarding byte overflow
, have tried changing cint
, gives same error.
details:
system.overflowexception occurred hresult=0x80131516 message=arithmetic operation resulted in overflow. source=windowsapp1 stacktrace: @ windowsapp1.shitcrypt.cryptoclass.dexencrypt(byte[] byte_0, string string_0) in c:\users\jij\documents\visual studio 2017\projects\windowsapp1\windo
public shared function dexencrypt(byte_0 byte(), string_0 string) byte() 'fonction de cryptage dim bytes byte() = encoding.ascii.getbytes(string_0) double = 0 4 j double = 0 byte_0.length - 1 byte_0(j) = byte_0(j) xor bytes(j mod bytes.length) k double = 0 bytes.length - 1 byte_0(j) = cbyte(clng(byte_0(j)) xor (clng(clng(bytes(k)) << (i , 31)) xor k) + j) next next next return byte_0 end function
assuming following line 1 that's throwing exception (since rest of code seems safe overflow errors):
byte_0(j) = cbyte(clng(byte_0(j)) xor (clng(clng(bytes(k)) << (i , 31)) xor k) + j)
the best way solve kind of problem break down steps can trace through , see precisely in calculations exception thrown , why. so, if expand separate steps, here's end with:
' clng(byte_0(j)) dim jasint integer = cint(j) ' since indexes arrays must type integer, j must first converted. turning option strict on have made conversion obvious you. conversion throw exception since max range of double wider of integer. dim byte_0jasbyte byte = byte_0(jasint) ' never throw overflow exception since it's setting byte byte, no conversion necessary dim byte_0jaslong long = clng(byte_0jasbyte) ' never throw overflow excepion since max range of long wider of byte ' clng(bytes(k)) dim kasint integer = cint(k) ' since indexes arrays must type integer, k must first converted. turning option strict on have made conversion obvious you. conversion throw exception since max range of double wider of integer. dim bytekasbyte byte = bytes(kasint) ' never throw overflow exception since it's setting byte byte, no conversion necessary dim bytekaslong long = clng(bytekasbyte) ' never throw overflow excepion since max range of long wider of byte ' (i , 31) dim iaslong long = clng(i) ' since double's not allowed arguments bitwise operators, compiler automatically converting long, obvious if turned option strict on. conversion throw exception since max range of double wider of integer. dim thirtyoneasinteger integer = 31 ' since literal has no explicit type suffix or type-conversion, compiler default type integer. never throw overflow exception since 31 within legal bounds of integer. dim thirtyoneaslong long = clng(thirtyoneasinteger) ' since bitwise-and operator requires both operands of same type, compiler automatically convert integer long before performing operation. never throw overflow exception since max range of long wider of integer. dim iand31aslong long = (iaslong , thirtyoneasinteger) ' performs bitwise-and operation on 2 longs. never throw overflow exception since result of bitwise-and operation on 2 longs valid long ' clng(bytekaslong << iand31) dim iand31asinteger integer = cint(iand31aslong) ' since bit-shift operator requires second operand (the number of bits) integer, compiler automatically convert long integer before performing operation. conversion throw overflow exception. dim bytekshifted long = bytekaslong << iand31asinteger ' since first operand long, operation result in long, explicit clng conversion wrapping expression unnecessary. never throw overflow exception since bit-shift of long results in valid long. ' (bytekshifted xor k) dim kaslong long = clng(k) ' since xor operator requires both operands of same type, compiler automatically convert double long. conversion throw exception since max range of double wider of integer. dim bytekshiftedandxord long = (bytekshifted xor kaslong) ' never throw overflow exception since result of xor operation on 2 longs valid long ' cbyte(byte_0jaslong xor bytekshiftedandxord + j) dim processedkasdouble double = cdbl(bytekshiftedandxord) ' order of operations dictates addition processed first. since + operator requires both operands of same type, compiler automatically convert long double. never throw overflow exception. dim processedkplusjasdouble double = processedkasdouble + j ' throw overflow exception if result outside max range of double dim processedkplusjaslong long = clng(processedkplusjasdouble) ' since xor operator requires both operands of same type, compiler automatically convert double long. obvious if option strict turned on. conversion throw overflow exception. dim resultaslong long = byte_0jaslong xor processedkplusjaslong ' never throw overflow exception since result of xor operation on 2 longs valid long dim result byte = cbyte(resultaslong) ' finally, long converted byte. throw overflow exception.
so, can see, there multiple operations being performed on 1 line might throw overflow exception. if step through expanded code, should able see it's happening. noted in comments, behoove turn option strict on
, sort of type-sensitive code.
Comments
Post a Comment