計(jì)算法就是依據(jù)CRC校驗(yàn)碼的產(chǎn)生原理來設(shè)計(jì)程序。其優(yōu)點(diǎn)是模塊代碼少,修改靈活,可移植性好。其缺點(diǎn)為計(jì)算量大。為了便于理解,這里假定了三位數(shù)據(jù),而多項(xiàng)式碼為A001(hex)。
成都創(chuàng)新互聯(lián)公司專注于博山企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開發(fā)。博山網(wǎng)站建設(shè)公司,為博山等地區(qū)提供建站服務(wù)。全流程按需設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
在窗體上放置一命令按鈕Command1,并添加如下代碼:
Private Sub Command1_Click()
Dim CRC() As Byte
Dim d() As Byte '待傳輸數(shù)據(jù)
ReDim d(2) As Byte
d(0) = 123
d(1) = 112
d(2) = 135
CRC = CRC16(d) '調(diào)用CRC16計(jì)算函數(shù)
'CRC(0)為高位
'CRC(1)為低位
End Sub
注意:在數(shù)據(jù)傳輸時(shí)CRC的低位可能在前,而高位在后。
大概看了下。有變量定義類型錯(cuò)誤,修改如下:
Public
Function
crc16(ByRef
cmdstring()
As
Byte,
DataLen
As
Integer)
As
String
Dim
data
As
Integer
Dim
i
As
Integer
Dim
CRCHi
As
long,
CRCLo
As
long'這里應(yīng)該定義為long.因?yàn)橄旅尜x值是long型。朋友。
Dim
iIndex
As
Long
Dim
CRCStr
As
String
Dim
DataStr
As
String
CRCLo
=
HFF'看這里的賦值。long型
CRCHi
=
HFF
For
i
=
To
DataLen
iIndex
=
CRCLo
Xor
cmdstring(i)
CRCLo
=
CRCHi
Xor
GetCRCLo(iIndex)
'低位處理
CRCHi
=
GetCRCHi(iIndex)
'高位處理
DataStr
=
DataStr
Chr(cmdstring(i))
Next
i
Dim
ReturnData(1)
As
Byte
ReturnData(1)
=
CRCHi
ReturnData(0)
=
CRCLo
CRCStr
=
StrConv(ReturnData,
vbUnicode)
crc16
=
DataStr
+
CRCStr
End
Function
就是收集boot.ini的不變的一些相關(guān)的信息(如文件大小,文件名等等),然后組成字符串,再用md5函數(shù)把組成的字符串編碼。
以后就用這個(gè)編碼進(jìn)行校驗(yàn)了,字符串比較(compare即可)。
不過對(duì)boot.ini文件校驗(yàn)沒有多大的意義,隨便裝一個(gè)什么dos工具箱或者用優(yōu)化軟件優(yōu)化以下那個(gè)校驗(yàn)碼準(zhǔn)變。
Private Sub Form_Load()
'text1為校驗(yàn)對(duì)象
Text1.Text = "31303432"
'text2為校驗(yàn)多項(xiàng)式
Text2.Text = "180D"
End Sub
Private Sub Command1_Click()
Text3.Text = CRC(Text1.Text, Text2.Text, 12)
End Sub
Private Function CRC(ByVal Hex_Number As String, ByVal Hex_CRC As String, Optional ByVal C As Integer = 16) As String
'Hex_Number為校驗(yàn)項(xiàng)16進(jìn)制數(shù)字,Hex_CRC為校驗(yàn)多項(xiàng)式,16進(jìn)制,C為默認(rèn)位數(shù)CRC-16
Dim i As Integer
Dim BinNumber As String
Dim BinCRC As String
Dim Temp As String
CRC = "0"
If Hex_Number = "0" Then Exit Function
BinNumber = HEXtoBin(Hex_Number)
BinCRC = HEXtoBin(Hex_CRC)
i = Len(BinCRC)
BinNumber = BinNumber String(C, "0")
CRC = Left(BinNumber, i)
Temp = Right(BinNumber, Len(BinNumber) - i)
Temp = HEXtoBin(Hex(CLng("H" BINtoHEX(CRC)) Xor CLng("H" Hex_CRC))) Temp
CRC = BINtoHEX(Temp)
If Len(Temp) = i Then
CRC = CRC(CRC, Hex_CRC, 0)
End If
End Function
Private Function HEXtoBin(Hex_Number) As String
'十六進(jìn)制轉(zhuǎn)換為二進(jìn)制
Dim i As Long
Dim B As String
Hex_Number = UCase(Hex_Number)
For i = 1 To Len(Hex_Number)
Select Case Mid(Hex_Number, i, 1)
Case "0": B = B "0000"
Case "1": B = B "0001"
Case "2": B = B "0010"
Case "3": B = B "0011"
Case "4": B = B "0100"
Case "5": B = B "0101"
Case "6": B = B "0110"
Case "7": B = B "0111"
Case "8": B = B "1000"
Case "9": B = B "1001"
Case "A": B = B "1010"
Case "B": B = B "1011"
Case "C": B = B "1100"
Case "D": B = B "1101"
Case "E": B = B "1110"
Case "F": B = B "1111"
End Select
Next i
While Left(B, 1) = "0"
B = Right(B, Len(B) - 1)
Wend
HEXtoBin = B
End Function
Private Function BINtoHEX(ByVal Bin_Number As String) As String
'二進(jìn)制轉(zhuǎn)換為十六進(jìn)制
Dim i As Long
Dim H As String
If Len(Bin_Number) Mod 4 0 Then
Bin_Number = String(4 - Len(Bin_Number) Mod 4, "0") Bin_Number
End If
For i = 1 To Len(Bin_Number) Step 4
Select Case Mid(Bin_Number, i, 4)
Case "0000": H = H "0"
Case "0001": H = H "1"
Case "0010": H = H "2"
Case "0011": H = H "3"
Case "0100": H = H "4"
Case "0101": H = H "5"
Case "0110": H = H "6"
Case "0111": H = H "7"
Case "1000": H = H "8"
Case "1001": H = H "9"
Case "1010": H = H "A"
Case "1011": H = H "B"
Case "1100": H = H "C"
Case "1101": H = H "D"
Case "1110": H = H "E"
Case "1111": H = H "F"
End Select
Next i
While Left(H, 1) = "0"
H = Right(H, Len(H) - 1)
Wend
BINtoHEX = H
End Function