扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
你对LRC的理解有问题啊……
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、雅安服务器托管、营销软件、网站建设、昔阳网站维护、网站推广。
图二中的有效ASCII字符串为“010300001388”,因此01+03+00+00+13+88 = 9F,9F的补码为61,因此校验码为“61”,即0x36、0x31。
LRC是纵向冗余校验。
纵向冗余校验(LRC,Longitudinal Redundancy Check)是通信中常用的一种校验形式。纵向冗余校验(LRC)是一种从纵向通道上的特定比特串产生校验比特的错误检测方法。在行列格式中(例如,在磁带中),LRC经常是与VRC一起使用,这样就会为每个字符校验码。
优势:
纵向冗余校验的异或校验和可以简单快速的计算出来,将一个数据块的所有数据字节递归,经过异或选通后即可产生异或校验和。由于算法简单,可以快速简单地计算纵向冗余校验。然而,LRC并不很可靠,多个错误可能相互抵消,在一个数据块内字节顺序的互换根本识别不出来。
因此LRC主要用于快速校验很小的数据块儿(如32B)。在射频识别系统中,由于标签的容量一般较小,每次交易的数据量也不大,所以这种算法还是比较适合的。
分二步:
一、建立一个模块,复制下面代码
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
二、函数调用
msgbox
HashString(Text1.Text)
当选用ASCII模式作字符帧,错误检测域包含两个ASCII字符。这是使用LRC(纵向冗长检测)方法对消息内容计算得出的,不包括开始的冒号符及回车换行符。LRC字符附加在回车换行符前面。我理解的是各位ASCII码对应的字符求和,然后位反,最后加1,转化成对应的字符即为lrc校验码。
信息“:010300010001”中“010300010001”
的LRC校验码为“FA”,
01+03+00+01+00+01=06 06位取反码是F9 再加1是FA
很明显结果就是FA啊 求和:0000 0110 取反 +1 = 1111 1010
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流