2014-07-27 124 views
0

我在Visual Basic中的Windows應用程序窗體項目中運行此代碼。這段代碼應該給我一個字節數組的CRC-32字節,但不管我做什麼我都不能輸出任何字節。對不起,如果有什麼東西真的很明顯,我在這裏失蹤,但我已經試過「MsgBox」,「立即屏幕」與「debug.print」,我也嘗試打印文本框中的字節,但沒有結果。如果你能幫助我,我很感激。這個Visual Basic代碼爲什麼不產生任何結果?

Public Class Form1 

Public Sub Main() 

    Dim lCrc32Value As Long 

    On Error Resume Next 
    lCrc32Value = InitCrc32() 
    lCrc32Value = AddCrc32("This is the original message!", _ 
     lCrc32Value) 
    'Debug.Print(Hex$(GetCrc32(lCrc32Value))) 

    TextBox1.Text = (Hex$(GetCrc32(lCrc32Value))).ToString 
End Sub 
'// Then declare this array variable Crc32Table 
Private Crc32Table(255) As Long 
'// Then all we have to do is write public functions like 
'these... 
Public Function InitCrc32(Optional ByVal Seed As Long = _ 
    &HEDB88320, Optional ByVal Precondition As _ 
    Long = &HFFFFFFFF) As Long 

    '// Declare counter variable iBytes, 
    'counter variable iBits, 
    'value variables lCrc32 and lTempCrc32 

    Dim iBytes As Integer, iBits As Integer, lCrc32 As Long 
    Dim lTempCrc32 As Long 

    '// Turn on error trapping 
    On Error Resume Next 

    '// Iterate 256 times 
    For iBytes = 0 To 255 

     '// Initiate lCrc32 to counter variable 
     lCrc32 = iBytes 

     '// Now iterate through each bit in counter byte 
     For iBits = 0 To 7 
      '// Right shift unsigned long 1 bit 
      lTempCrc32 = lCrc32 And &HFFFFFFFE 
      lTempCrc32 = lTempCrc32 \ &H2 
      lTempCrc32 = lTempCrc32 And &H7FFFFFFF 

      '// Now check if temporary is less than zero and then 
      'mix Crc32 checksum with Seed value 
      If (lCrc32 And &H1) <> 0 Then 
       lCrc32 = lTempCrc32 Xor Seed 
      Else 
       lCrc32 = lTempCrc32 
      End If 
     Next 

     '// Put Crc32 checksum value in the holding array 
     Crc32Table(iBytes) = lCrc32 
    Next 

    '// After this is done, set function value to the 
    'precondition value 
    InitCrc32 = Precondition 

End Function 

'// The function above is the initializing function, now 
'we have to write the computation function 
Public Function AddCrc32(ByVal Item As String, _ 
    ByVal Crc32 As Long) As Long 

    '// Declare following variables 
    Dim bCharValue As Byte, iCounter As Integer, lIndex As Long 
    Dim lAccValue As Long, lTableValue As Long 

    '// Turn on error trapping 
    On Error Resume Next 

    '// Iterate through the string that is to be checksum-computed 
    For iCounter = 1 To Len(Item) 

     '// Get ASCII value for the current character 
     bCharValue = Asc(Mid$(Item, iCounter, 1)) 

     '// Right shift an Unsigned Long 8 bits 
     lAccValue = Crc32 And &HFFFFFF00 
     lAccValue = lAccValue \ &H100 
     lAccValue = lAccValue And &HFFFFFF 

     '// Now select the right adding value from the 
     'holding table 
     lIndex = Crc32 And &HFF 
     lIndex = lIndex Xor bCharValue 
     lTableValue = Crc32Table(lIndex) 

     '// Then mix new Crc32 value with previous 
     'accumulated Crc32 value 
     Crc32 = lAccValue Xor lTableValue 
    Next 

    '// Set function value the the new Crc32 checksum 
    AddCrc32 = Crc32 

End Function 

'// At last, we have to write a function so that we 
'can get the Crc32 checksum value at any time 
Public Function GetCrc32(ByVal Crc32 As Long) As Long 
    '// Turn on error trapping 
    On Error Resume Next 

    '// Set function to the current Crc32 value 
    GetCrc32 = Crc32 Xor &HFFFFFFFF 

End Function 
'// And for testing the routines above... 



End Class 
+0

如果性能很重要,你應該考慮切換到SHA256甚至MD5 - CRC32已經過時了,甚至不再產生可靠的傳輸層有效性信息。您確實應該使用AT LEAST MD5,它可供所有人使用,網址爲http://msdn.microsoft.com/de-de/library/system.security.cryptography.hmacmd5(v=vs.110).aspx – specializt

+2

全部那些On Error Resume Next似乎只是爲了隱藏錯誤。並且請設置OPTION STRICT ON,並在將長整型轉換爲整數時發現代碼中存在很多錯誤 – Steve

+0

@Steve:如果可以的話,我會提供+10的評論。 –

回答

2

有幾個問題。首先,所有的CRC代碼都應該放在一個不與表格混合的類中。其次,Option Strict不是開啓的,因爲有幾個隱式轉換可能會破壞結果:

' converting Integer to Byte 
bCharValue = Asc(Mid$(Item, iCounter, 1)) 

' see note 7 
DIm lIndex As Long 
' converting integer to Long 
lIndex = Crc32 And &HFF 

' here too: 
lTableValue = Crc32Table(lIndex) 

#3。擺脫On Error Resume next。這不是錯誤處理 - 它隱藏了你的錯誤。

#4。你永遠不會調用Main函數。 不要從表格調用它加載從那裏調試將會非常困難。添加一個按鈕並從click事件中調用Main。

#5。強烈建議從舊的VB函數遷移到它們在.NET中的更強大的等價物。

#6。更改AddCRC32:


Public Function AddCrc32(ByVal Item As String, ByVal Crc32 As Long) As Long 
    '// Declare following variables 
    Dim bCharValue As Byte, iCounter As Integer, lIndex As Long 
    Dim lAccValue As Long, lTableValue As Long 

    ' convert text to char array all at once 
    Dim strChars = Item.ToCharArray 

    '// Iterate through the string that is to be checksum-computed 
    For iCounter = 0 To Item.Length - 1 

     '// Get ASCII value for the current character 
     'bCharValue = Asc(Mid$(Item, iCounter, 1)) 

     ' NET version 
     bCharValue = Convert.ToByte(strChars(iCounter)) 

     '// Right shift an Unsigned Long 8 bits 
     lAccValue = Crc32 And &HFFFFFF00 
     lAccValue = lAccValue \ &H100 
     lAccValue = lAccValue And &HFFFFFF 

     '// Now select the right adding value from the 
     'holding table 
     lIndex = Crc32 And &HFF 
     lIndex = lIndex Xor bCharValue 
     lTableValue = Crc32Table(CInt(lIndex)) 

     '// Then mix new Crc32 value with previous 
     'accumulated Crc32 value 
     Crc32 = lAccValue Xor lTableValue 
    Next 

    '// Set function value the the new Crc32 checksum 
    ' a REALLY big indicator that this came from VB6: 
    AddCrc32 = Crc32 

    ' NET is usually: 
    ' Return Crc32 

End Function 

#7:LINDEX修復上述應該是從我記得CRC32的權利。替代方案是相反的:將其更改爲整數並將Crc32 And &HFF轉換爲整數。

請注意,我沒有檢查任何CRC32的東西(如表初始化程序),只是選擇嚴格的問題並將其掛鉤,並檢查初始異常。


附錄

我發現我自己的老CRC32代碼和進行比較時,我發現了一些CRC相關的問題:

幾乎一切都在你的Buildtable/InitCrc32程序,包括迴歸應該是整型(Int32)已不長(Int64)。它看起來像你從VB6源複製代碼,並沒有正確更新(爲此,不應該需要Long)。

對於文本的編碼,你可能希望這樣的:

Dim encoding As New System.Text.UTF8Encoding() 
Dim byt As Byte() = encoding.GetBytes(s) 

,而不是上面給出的Convert.ToByte。正如我所說的,我主要着眼於讓它以某種形式或方式工作,而不是驗證算法。

+0

我會使用'返回值'而不是'FunctionName = value'。原文中的一些評論似乎具有誤導性,例如''//向右移位無符號長整型8位'看起來應該是''右移8位並截斷'。 Form_Load中的代碼的好處是難以調試。您的lIndex修復與OP代碼的結果相同。 –

+0

@AndrewMorton是的,舊風格的'返回'和註釋是其他指標源是一些舊的VB6代碼轉換;在NET中用於計算CRC32的Long(Int64)也有太多的用處。我並沒有修改他的算法,以免它全部解開,只是修正了Option Strict問題並將其燒燬。現在看看CRC的東西。 – Plutonix

+0

System.Text.UTF8Encoding.UTF8.GetBytes是一個公共共享函數..並且我更喜歡FunctionName = value ..肯定有布爾返回函數 –

相關問題