Dex算法溢出异常

时间:2017-07-25 09:53:29

标签: vb.net algorithm exception overflow dex

我正在尝试使用algorithm开发加密dex。 似乎是关于字节exception获得overflow,我尝试将其更改为CInt,这会产生同样的错误。

详细信息:

  

发生System.OverflowException HResult = 0x80131516消息=算术运算导致溢出。 Source = WindowsApp1 StackTrace:位于C:\ Users \ jij \ documents \ visual studio 2017 \ Projects \ WindowsApp1 \ Windo

中的WindowsApp1.ShitCrypt.Cryptoclass.DexEncrypt(Byte [] byte_0,String string_0)
Public Shared Function DexEncrypt(byte_0 As Byte(), string_0 As String) As Byte()
        'Fonction de cryptage  
        Dim bytes As Byte() = Encoding.ASCII.GetBytes(string_0)
        For i As Double = 0 To 4
            For j As Double = 0 To byte_0.Length - 1
                byte_0(j) = byte_0(j) Xor bytes(j Mod bytes.Length)
                For k As Double = 0 To bytes.Length - 1
                    byte_0(j) = CByte(CLng(byte_0(j)) Xor (CLng(CLng(bytes(k)) << (i And 31)) Xor k) + j)
                Next
            Next
        Next
        Return byte_0
    End Function

1 个答案:

答案 0 :(得分:0)

假设以下行是抛出异常的行(因为其余代码似乎可以防止溢出错误):

byte_0(j) = CByte(CLng(byte_0(j)) Xor (CLng(CLng(bytes(k)) << (i And 31)) Xor k) + j)

解决此类问题的最佳方法是将其分解为步骤,以便您可以跟踪它并精确查看计算中抛出异常的位置以及原因。因此,如果我们将其扩展为单独的步骤,那么我们最终会得到:

' CLng(byte_0(j))
Dim jAsInt As Integer = CInt(j)  ' Since indexes to arrays must be as type Integer, j must first be converted.  Turning Option Strict On would have made this conversion obvious for you.  This conversion could throw the exception since the max range of Double is wider than that of Integer.
Dim byte_0JAsByte As Byte = byte_0(jAsInt)  ' This will never throw an overflow exception since it's setting a Byte to a Byte, so no conversion is necessary
Dim byte_0JAsLong As Long = CLng(byte_0JAsByte)  ' This will never throw an overflow excepion since the max range of Long is wider than that of Byte

' CLng(bytes(k))
Dim kAsInt As Integer = CInt(k)  ' Since indexes to arrays must be as type Integer, k must first be converted.  Turning Option Strict On would have made this conversion obvious for you.  This conversion could throw the exception since the max range of Double is wider than that of Integer.
Dim byteKAsByte As Byte = bytes(kAsInt)  ' This will never throw an overflow exception since it's setting a Byte to a Byte, so no conversion is necessary
Dim byteKAsLong As Long = CLng(byteKAsByte)  ' This will never throw an overflow excepion since the max range of Long is wider than that of Byte

' (i And 31)
Dim iAsLong As Long = CLng(i)  ' Since Double's are not allowed as arguments to bitwise operators, the compiler is automatically converting i to a Long, which would be obvious if you turned Option Strict On.  This conversion could throw the exception since the max range of Double is wider than that of Integer.
Dim thirtyOneAsInteger As Integer = 31  ' Since the literal has no explicit type suffix or type-conversion, the compiler will default its type to Integer.  This will never throw an overflow exception since 31 is always within the legal bounds of Integer.
Dim thirtyOneAsLong As Long = CLng(thirtyOneAsInteger)  ' Since the bitwise-and operator requires both operands to be of the same type, the compiler will automatically convert the Integer into a Long before performing the operation.  This will never throw an overflow exception since the max range of Long is wider than that of Integer.
Dim iAnd31AsLong As Long = (iAsLong And thirtyOneAsInteger)  ' Performs a bitwise-and operation on the two Longs.  This will never throw an overflow exception since the result of a bitwise-and operation on two Longs is always a valid Long

' CLng(byteKAsLong << iAnd31)
Dim iAnd31AsInteger As Integer = CInt(iAnd31AsLong)  ' Since the bit-shift operator requires the second operand (the number of bits) to be an Integer, the compiler will automatically convert the Long into an Integer before performing the operation.  This conversion could throw an overflow exception.
Dim byteKShifted As Long = byteKAsLong << iAnd31AsInteger  ' Since the first operand is a Long, the operation will always result in a long, so the explicit CLng conversion wrapping the expression is unnecessary.  This will never throw an overflow exception since a bit-shift of a Long always results in a valid Long.

' (byteKShifted Xor k)
Dim kAsLong As Long = CLng(k)  ' Since the Xor operator requires both operands to be of the same type, the compiler will automatically convert the Double to a Long.  This conversion could throw the exception since the max range of Double is wider than that of Integer.
Dim byteKShiftedAndXord As Long = (byteKShifted Xor kAsLong)  ' This will never throw an overflow exception since the result of a Xor operation on two Longs is always a valid Long

' CByte(byte_0JAsLong Xor byteKShiftedAndXord + j)
Dim processedKAsDouble As Double = CDbl(byteKShiftedAndXord)  ' Order of operations dictates that the addition will be processed first.  Since the + operator requires both operands to by of the same type, the compiler will automatically convert the Long to a Double.  This will never throw an overflow exception.
Dim processedKPlusJAsDouble As Double = processedKAsDouble + j  ' This could throw an overflow exception if the result is outside the max range of a Double
Dim processedKPlusJAsLong As Long = CLng(processedKPlusJAsDouble)  ' Since the Xor operator requires both operands to be of the same type, the compiler will automatically convert the Double into a Long.  This would be obvious if Option Strict was turned On.  This conversion could throw an overflow exception.
Dim resultAsLong As Long = byte_0JAsLong Xor processedKPlusJAsLong  ' This will never throw an overflow exception since the result of a Xor operation on two Longs is always a valid Long
Dim result As Byte = CByte(resultAsLong)  ' Finally, the long is converted to a Byte.  This could throw an overflow exception.

因此,正如您所看到的,在该行上执行了多个操作,这可能会引发溢出异常。如果您单步执行该扩展代码,您应该能够看到它实际发生的位置。如评论中所述,您应该转向Option Strict On,特别是对于此类型敏感代码。