2017-07-25 43 views
0

我正在尝试使用dex开发加密algorithm。 它似乎得到exception关于字节overflow,我试图将其更改为CInt,这给出了相同的错误。Dex算法溢出异常

详细信息:出现

System.OverflowException的HResult = 0x80131516消息=算术运算导致溢出。源= WindowsApp1堆栈跟踪:在WindowsApp1.ShitCrypt.Cryptoclass.DexEncrypt(字节[] byte_0,字符串STRING_0)在C:\用户\ JIJ \文件\视觉工作室2017 \项目\ WindowsApp1 \的Windo

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 
+0

您切断了指定行号的错误消息部分。据推测,这是最内线,是Xor用'k'抛出异常的那条线? –

回答

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,特别是对于这种类型敏感的代码。