|
@@ -7,6 +7,8 @@ using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using AutoMapper.Execution;
|
|
|
+using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
|
|
+using Org.BouncyCastle.Utilities;
|
|
|
|
|
|
|
|
|
namespace HengshanPaymentTerminal
|
|
@@ -139,10 +141,8 @@ namespace HengshanPaymentTerminal
|
|
|
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
|
|
|
};
|
|
|
|
|
|
- // CRC-16/CCITT-FALSE多项式
|
|
|
- private const ushort Polynomial = 0x1021;
|
|
|
- // 初始值
|
|
|
- private const ushort InitialValue = 0xFFFF;
|
|
|
+ private const ushort Polynomial = 0xA001; // CRC-16/IBM 多项式
|
|
|
+ private const ushort InitialValue = 0x0000;// CRC-16/IBM 初始值
|
|
|
|
|
|
public static ushort ComputeCrc(byte[] data)
|
|
|
{
|
|
@@ -163,24 +163,23 @@ namespace HengshanPaymentTerminal
|
|
|
return BitConverter.GetBytes(result).Reverse().ToArray();
|
|
|
}
|
|
|
|
|
|
- // 计算CRC-16/CCITT-FALSE校验值
|
|
|
+ // 计算CRC-16/IBM校验值
|
|
|
public static ushort ComputeChecksum(byte[] data)
|
|
|
{
|
|
|
ushort crc = InitialValue;
|
|
|
|
|
|
foreach (byte b in data)
|
|
|
{
|
|
|
- crc ^= (ushort)((b << 8) & 0xFF00); // 将字节数据左移8位并与0xFF00进行与操作
|
|
|
-
|
|
|
+ crc ^= b; // XOR 字节数据
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
{
|
|
|
- if ((crc & 0x8000) != 0) // 检查最高位是否为1
|
|
|
+ if ((crc & 0x0001) != 0) // 检查最低位是否为 1
|
|
|
{
|
|
|
- crc = (ushort)((crc << 1) ^ Polynomial); // 左移1位并与多项式进行异或
|
|
|
+ crc = (ushort)((crc >> 1) ^ Polynomial); // 右移并异或多项式
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- crc <<= 1; // 仅左移1位
|
|
|
+ crc >>= 1; // 直接右移
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -188,7 +187,7 @@ namespace HengshanPaymentTerminal
|
|
|
return crc;
|
|
|
}
|
|
|
|
|
|
- // 计算CRC-16/CCITT-FALSE校验值,返回数组
|
|
|
+ // 计算CRC-16/IBM校验值,返回数组
|
|
|
public static byte[] ComputeChecksumToBytes(byte[] data)
|
|
|
{
|
|
|
byte[] result = new byte[2];
|