添加CRC16校验
This commit is contained in:
commit
81c98ec936
4 files changed
+350
-18
No files matched your search
@@ -0,0 +1,193 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace formula_manage.UserClass
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// CRC校验
|
||||||
|
/// </summary>
|
||||||
|
public class CRCcheck16
|
||||||
|
{
|
||||||
|
|
||||||
|
#region CRC16
|
||||||
|
public static byte[] CRC16(byte[] data)
|
||||||
|
{
|
||||||
|
int len = data.Length;
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
ushort crc = 0xFFFF;
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
crc = (ushort)(crc ^ (data[i]));
|
||||||
|
for (int j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
|
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
|
||||||
|
byte lo = (byte)(crc & 0x00FF); //低位置
|
||||||
|
|
||||||
|
return new byte[] { hi, lo };
|
||||||
|
}
|
||||||
|
return new byte[] { 0, 0 };
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region ToCRC16
|
||||||
|
public static string ToCRC16(string content)
|
||||||
|
{
|
||||||
|
return ToCRC16(content, Encoding.UTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToCRC16(string content, bool isReverse)
|
||||||
|
{
|
||||||
|
return ToCRC16(content, Encoding.UTF8, isReverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToCRC16(string content, Encoding encoding)
|
||||||
|
{
|
||||||
|
return ByteToString(CRC16(encoding.GetBytes(content)), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToCRC16(string content, Encoding encoding, bool isReverse)
|
||||||
|
{
|
||||||
|
return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToCRC16(byte[] data)
|
||||||
|
{
|
||||||
|
return ByteToString(CRC16(data), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToCRC16(byte[] data, bool isReverse)
|
||||||
|
{
|
||||||
|
return ByteToString(CRC16(data), isReverse);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region ToModbusCRC16
|
||||||
|
public static string ToModbusCRC16(string s)
|
||||||
|
{
|
||||||
|
return ToModbusCRC16(s, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToModbusCRC16(string s, bool isReverse)
|
||||||
|
{
|
||||||
|
return ByteToString(CRC16(StringToHexByte(s)), isReverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToModbusCRC16(byte[] data)
|
||||||
|
{
|
||||||
|
return ToModbusCRC16(data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToModbusCRC16(byte[] data, bool isReverse)
|
||||||
|
{
|
||||||
|
return ByteToString(CRC16(data), isReverse);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region ByteToString
|
||||||
|
public static string ByteToString(byte[] arr, bool isReverse)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
byte hi = arr[0], lo = arr[1];
|
||||||
|
return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0');
|
||||||
|
}
|
||||||
|
catch (Exception ex) { throw (ex); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ByteToString(byte[] arr)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return ByteToString(arr, true);
|
||||||
|
}
|
||||||
|
catch (Exception ex) { throw (ex); }
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region StringToHexString
|
||||||
|
public static string StringToHexString(string str)
|
||||||
|
{
|
||||||
|
StringBuilder s = new StringBuilder();
|
||||||
|
foreach (short c in str.ToCharArray())
|
||||||
|
{
|
||||||
|
s.Append(c.ToString("X4"));
|
||||||
|
}
|
||||||
|
return s.ToString();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region StringToHexByte
|
||||||
|
private static string ConvertChinese(string str)
|
||||||
|
{
|
||||||
|
StringBuilder s = new StringBuilder();
|
||||||
|
foreach (short c in str.ToCharArray())
|
||||||
|
{
|
||||||
|
if (c <= 0 || c >= 127)
|
||||||
|
{
|
||||||
|
s.Append(c.ToString("X4"));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s.Append((char)c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FilterChinese(string str)
|
||||||
|
{
|
||||||
|
StringBuilder s = new StringBuilder();
|
||||||
|
foreach (short c in str.ToCharArray())
|
||||||
|
{
|
||||||
|
if (c > 0 && c < 127)
|
||||||
|
{
|
||||||
|
s.Append((char)c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 字符串转16进制字符数组
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hex"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static byte[] StringToHexByte(string str)
|
||||||
|
{
|
||||||
|
return StringToHexByte(str, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 字符串转16进制字符数组
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str"></param>
|
||||||
|
/// <param name="isFilterChinese">是否过滤掉中文字符</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static byte[] StringToHexByte(string str, bool isFilterChinese)
|
||||||
|
{
|
||||||
|
string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str);
|
||||||
|
|
||||||
|
//清除所有空格
|
||||||
|
hex = hex.Replace(" ", "");
|
||||||
|
//若字符个数为奇数,补一个0
|
||||||
|
hex += hex.Length % 2 != 0 ? "0" : "";
|
||||||
|
|
||||||
|
byte[] result = new byte[hex.Length / 2];
|
||||||
|
for (int i = 0, c = result.Length; i < c; i++)
|
||||||
|
{
|
||||||
|
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
<TextBox x:Name="sn_id" HorizontalAlignment="Left" Height="20" Margin="90,10,0,0" VerticalAlignment="Top" Width="360" Background="#FFE6E6E6" IsReadOnly="True" IsTabStop="False" Focusable="False"/>
|
<TextBox x:Name="sn_id" HorizontalAlignment="Left" Height="20" Margin="90,10,0,0" VerticalAlignment="Top" Width="360" Background="#FFE6E6E6" IsReadOnly="True" IsTabStop="False" Focusable="False"/>
|
||||||
<TextBox x:Name="cdk" HorizontalAlignment="Left" Height="20" Margin="90,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="360"/>
|
<TextBox x:Name="cdk" HorizontalAlignment="Left" Height="20" Margin="90,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="360"/>
|
||||||
<Button Content="OK" HorizontalAlignment="Right" Height="20" Margin="0,60,50,0" VerticalAlignment="Top" Width="85" Background="#FFE6E6E6" Click="Button_Click_2"/>
|
<Button Content="OK" HorizontalAlignment="Right" Height="20" Margin="0,60,50,0" VerticalAlignment="Top" Width="85" Background="#FFE6E6E6" Click="Button_Click_2"/>
|
||||||
|
<TextBox x:Name="sn_time" HorizontalAlignment="Left" Height="20" Margin="90,60,0,0" VerticalAlignment="Top" Width="250" Background="{x:Null}" IsReadOnly="True" IsTabStop="False" Focusable="False" BorderBrush="{x:Null}"/>
|
||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
+155
-18
@@ -1,7 +1,11 @@
|
|||||||
using System;
|
using formula_manage.UserClass;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Management;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
@@ -28,14 +32,8 @@ namespace formula_manage.Windows
|
|||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SQLTIME;
|
string KEY;
|
||||||
int Time;
|
string SN;
|
||||||
int i;
|
|
||||||
string SQLIP;
|
|
||||||
string SQLNAME;
|
|
||||||
bool SQLMOD;
|
|
||||||
string SQLUSER;
|
|
||||||
string SQLPASS;
|
|
||||||
|
|
||||||
public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini";
|
public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini";
|
||||||
|
|
||||||
@@ -43,20 +41,159 @@ namespace formula_manage.Windows
|
|||||||
{
|
{
|
||||||
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); //读配置文件
|
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath); //读配置文件
|
||||||
|
|
||||||
SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
// KEY = Configini.IniReadvalue("CDKEY", "KEY"); //读配置文件
|
||||||
SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
// SN = Configini.IniReadvalue("CDKEY", "SN");
|
||||||
int.TryParse(Configini.IniReadvalue("SQL_SERVER", "SQL3"), out i); //读数据库状态配置文件
|
// 获取CPU序列号
|
||||||
if (i == 0) SQLMOD = false;
|
string cpuSerialNumber = GetCPUSerialNumber();
|
||||||
else SQLMOD = true;
|
// 获取主板序列号
|
||||||
SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
|
string biosSerialNumber = GetBIOSSerialNumber();
|
||||||
SQLPASS = Configini.IniReadvalue("SQL_SERVER", "SQL5");
|
// 获取硬盘序列号
|
||||||
|
//string hardDiskSerialNumber = GetHardDiskSerialNumber();
|
||||||
|
// 获取网卡地址
|
||||||
|
//string netCardMACAddress = GetNetCardMACAddress();
|
||||||
|
|
||||||
|
sn_id.Text = MD5Encrypt16(cpuSerialNumber+ biosSerialNumber);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取cpuid
|
||||||
|
/// </summary>
|
||||||
|
public static string GetCPUSerialNumber()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
|
||||||
|
string cpuSerialNumber = "";
|
||||||
|
foreach (ManagementObject mo in searcher.Get())
|
||||||
|
{
|
||||||
|
cpuSerialNumber = mo["ProcessorId"].ToString().Trim();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return cpuSerialNumber;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取主板id
|
||||||
|
/// </summary>
|
||||||
|
public static string GetBIOSSerialNumber()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_BIOS");
|
||||||
|
string biosSerialNumber = "";
|
||||||
|
foreach (ManagementObject mo in searcher.Get())
|
||||||
|
{
|
||||||
|
biosSerialNumber = mo.GetPropertyValue("SerialNumber").ToString().Trim();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return biosSerialNumber;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取硬盘id
|
||||||
|
/// </summary>
|
||||||
|
public static string GetHardDiskSerialNumber()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
|
||||||
|
string hardDiskSerialNumber = "";
|
||||||
|
foreach (ManagementObject mo in searcher.Get())
|
||||||
|
{
|
||||||
|
hardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return hardDiskSerialNumber;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 获取网卡id
|
||||||
|
/// </summary>
|
||||||
|
public static string GetNetCardMACAddress()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
|
||||||
|
string netCardMACAddress = "";
|
||||||
|
foreach (ManagementObject mo in searcher.Get())
|
||||||
|
{
|
||||||
|
netCardMACAddress = mo["MACAddress"].ToString().Trim();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return netCardMACAddress;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 16位MD5加密
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="password"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string MD5Encrypt16(string password)
|
||||||
|
{
|
||||||
|
var md5 = new MD5CryptoServiceProvider();
|
||||||
|
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
|
||||||
|
t2 = t2.Replace("-", "");
|
||||||
|
return t2;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 32位MD5加密
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="password"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string MD5Encrypt32(string password)
|
||||||
|
{
|
||||||
|
string cl = password;
|
||||||
|
string pwd = "";
|
||||||
|
MD5 md5 = MD5.Create(); //实例化一个md5对像
|
||||||
|
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
|
||||||
|
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
|
||||||
|
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
|
||||||
|
for (int i = 0; i < s.Length; i++)
|
||||||
|
{
|
||||||
|
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
|
||||||
|
pwd = pwd + s[i].ToString("X");
|
||||||
|
}
|
||||||
|
return pwd;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 64位MD5加密
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="password"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string MD5Encrypt64(string password)
|
||||||
|
{
|
||||||
|
string cl = password;
|
||||||
|
//string pwd = "";
|
||||||
|
MD5 md5 = MD5.Create(); //实例化一个md5对像
|
||||||
|
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
|
||||||
|
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
|
||||||
|
return Convert.ToBase64String(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Button_Click_2(object sender, RoutedEventArgs e)
|
||||||
private async void Button_Click_2(object sender, RoutedEventArgs e)
|
|
||||||
{
|
{
|
||||||
|
string t;
|
||||||
|
t = sn_id.Text.Substring(0,6);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cdk.Text = t +"240102" + CRCcheck16.ToCRC16(CRCcheck16.StringToHexByte(t+ "240102"),true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -215,6 +215,7 @@
|
|||||||
<DependentUpon>Login.xaml</DependentUpon>
|
<DependentUpon>Login.xaml</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="SQLModels\STUFF_Product.cs" />
|
<Compile Include="SQLModels\STUFF_Product.cs" />
|
||||||
|
<Compile Include="UserClass\CRCcheck16.cs" />
|
||||||
<Compile Include="UserClass\DataGridHelper.cs" />
|
<Compile Include="UserClass\DataGridHelper.cs" />
|
||||||
<Compile Include="UserClass\IniFile.cs" />
|
<Compile Include="UserClass\IniFile.cs" />
|
||||||
<Compile Include="UserClass\PressKey.cs" />
|
<Compile Include="UserClass\PressKey.cs" />
|
||||||
|
|||||||
Reference in new issue
Block a user