2017-07-25 84 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,特別是對於這種類型敏感的代碼。