你對(duì)LRC的理解有問(wèn)題啊……
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、雅安服務(wù)器托管、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、昔陽(yáng)網(wǎng)站維護(hù)、網(wǎng)站推廣。
圖二中的有效ASCII字符串為“010300001388”,因此01+03+00+00+13+88 = 9F,9F的補(bǔ)碼為61,因此校驗(yàn)碼為“61”,即0x36、0x31。
LRC是縱向冗余校驗(yàn)。
縱向冗余校驗(yàn)(LRC,Longitudinal Redundancy Check)是通信中常用的一種校驗(yàn)形式??v向冗余校驗(yàn)(LRC)是一種從縱向通道上的特定比特串產(chǎn)生校驗(yàn)比特的錯(cuò)誤檢測(cè)方法。在行列格式中(例如,在磁帶中),LRC經(jīng)常是與VRC一起使用,這樣就會(huì)為每個(gè)字符校驗(yàn)碼。
優(yōu)勢(shì):
縱向冗余校驗(yàn)的異或校驗(yàn)和可以簡(jiǎn)單快速的計(jì)算出來(lái),將一個(gè)數(shù)據(jù)塊的所有數(shù)據(jù)字節(jié)遞歸,經(jīng)過(guò)異或選通后即可產(chǎn)生異或校驗(yàn)和。由于算法簡(jiǎn)單,可以快速簡(jiǎn)單地計(jì)算縱向冗余校驗(yàn)。然而,LRC并不很可靠,多個(gè)錯(cuò)誤可能相互抵消,在一個(gè)數(shù)據(jù)塊內(nèi)字節(jié)順序的互換根本識(shí)別不出來(lái)。
因此LRC主要用于快速校驗(yàn)很小的數(shù)據(jù)塊兒(如32B)。在射頻識(shí)別系統(tǒng)中,由于標(biāo)簽的容量一般較小,每次交易的數(shù)據(jù)量也不大,所以這種算法還是比較適合的。
分二步:
一、建立一個(gè)模塊,復(fù)制下面代碼
Option
Explicit
Private
Declare
Function
CryptAcquireContext
Lib
"advapi32.dll"
_
Alias
"CryptAcquireContextA"
(
_
ByRef
phProv
As
Long,
_
ByVal
pszContainer
As
String,
_
ByVal
pszProvider
As
String,
_
ByVal
dwProvType
As
Long,
_
ByVal
dwFlags
As
Long)
As
Long
Private
Declare
Function
CryptReleaseContext
Lib
"advapi32.dll"
(
_
ByVal
hProv
As
Long,
_
ByVal
dwFlags
As
Long)
As
Long
Private
Declare
Function
CryptCreateHash
Lib
"advapi32.dll"
(
_
ByVal
hProv
As
Long,
_
ByVal
Algid
As
Long,
_
ByVal
hKey
As
Long,
_
ByVal
dwFlags
As
Long,
_
ByRef
phHash
As
Long)
As
Long
Private
Declare
Function
CryptDestroyHash
Lib
"advapi32.dll"
(
_
ByVal
hHash
As
Long)
As
Long
Private
Declare
Function
CryptHashData
Lib
"advapi32.dll"
(
_
ByVal
hHash
As
Long,
_
pbData
As
Any,
_
ByVal
dwDataLen
As
Long,
_
ByVal
dwFlags
As
Long)
As
Long
Private
Declare
Function
CryptGetHashParam
Lib
"advapi32.dll"
(
_
ByVal
hHash
As
Long,
_
ByVal
dwParam
As
Long,
_
pbData
As
Any,
_
pdwDataLen
As
Long,
_
ByVal
dwFlags
As
Long)
As
Long
Private
Const
PROV_RSA_FULL
=
1
Private
Const
CRYPT_NEWKEYSET
=
H8
Private
Const
ALG_CLASS_HASH
=
32768
Private
Const
ALG_TYPE_ANY
=
Private
Const
ALG_SID_MD2
=
1
Private
Const
ALG_SID_MD4
=
2
Private
Const
ALG_SID_MD5
=
3
Private
Const
ALG_SID_SHA1
=
4
Enum
HashAlgorithm
MD2
=
ALG_CLASS_HASH
Or
ALG_TYPE_ANY
Or
ALG_SID_MD2
MD4
=
ALG_CLASS_HASH
Or
ALG_TYPE_ANY
Or
ALG_SID_MD4
MD5
=
ALG_CLASS_HASH
Or
ALG_TYPE_ANY
Or
ALG_SID_MD5
SHA1
=
ALG_CLASS_HASH
Or
ALG_TYPE_ANY
Or
ALG_SID_SHA1
End
Enum
Private
Const
HP_HASHVAL
=
2
Private
Const
HP_HASHSIZE
=
4
Function
HashString(
_
ByVal
Str
As
String,
_
Optional
ByVal
Algorithm
As
HashAlgorithm
=
MD5)
As
String
Dim
hCtx
As
Long
Dim
hHash
As
Long
Dim
lRes
As
Long
Dim
lLen
As
Long
Dim
lIdx
As
Long
Dim
abData()
As
Byte
'
Get
default
provider
context
handle
lRes
=
CryptAcquireContext(hCtx,
vbNullString,
_
vbNullString,
PROV_RSA_FULL,
0)
If
lRes
Then
'
Create
the
hash
lRes
=
CryptCreateHash(hCtx,
Algorithm,
0,
0,
hHash)
If
lRes
Then
'
Hash
the
string
lRes
=
CryptHashData(hHash,
ByVal
Str,
Len(Str),
0)
If
lRes
Then
'
Get
the
hash
lenght
lRes
=
CryptGetHashParam(hHash,
HP_HASHSIZE,
lLen,
4,
0)
If
lRes
Then
'
Initialize
the
buffer
ReDim
abData(0
To
lLen
-
1)
'
Get
the
hash
value
lRes
=
CryptGetHashParam(hHash,
HP_HASHVAL,
abData(0),
lLen,
0)
If
lRes
Then
'
Convert
value
to
hex
string
For
lIdx
=
To
UBound(abData)
HashString
=
HashString
_
Right$("0"
Hex$(abData(lIdx)),
2)
Next
End
If
End
If
End
If
'
Release
the
hash
handle
CryptDestroyHash
hHash
End
If
End
If
'
Release
the
provider
context
CryptReleaseContext
hCtx,
'
Raise
an
error
if
lRes
=
If
lRes
=
Then
Err.Raise
Err.LastDllError
End
Function
二、函數(shù)調(diào)用
msgbox
HashString(Text1.Text)
當(dāng)選用ASCII模式作字符幀,錯(cuò)誤檢測(cè)域包含兩個(gè)ASCII字符。這是使用LRC(縱向冗長(zhǎng)檢測(cè))方法對(duì)消息內(nèi)容計(jì)算得出的,不包括開(kāi)始的冒號(hào)符及回車(chē)換行符。LRC字符附加在回車(chē)換行符前面。我理解的是各位ASCII碼對(duì)應(yīng)的字符求和,然后位反,最后加1,轉(zhuǎn)化成對(duì)應(yīng)的字符即為lrc校驗(yàn)碼。
信息“:010300010001”中“010300010001”
的LRC校驗(yàn)碼為“FA”,
01+03+00+01+00+01=06 06位取反碼是F9 再加1是FA
很明顯結(jié)果就是FA啊 求和:0000 0110 取反 +1 = 1111 1010