Browse Source

添加crc

master
sc 2 years ago
parent
commit
7b04a24520
  1. 22
      MainWindow.xaml
  2. 79
      MainWindow.xaml.cs
  3. 1
      SUNLIGHT_CDKEY.csproj
  4. 193
      UserClass/CRCcheck16.cs

22
MainWindow.xaml

@ -5,12 +5,10 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SUNLIGHT_CDKEY" xmlns:local="clr-namespace:SUNLIGHT_CDKEY"
mc:Ignorable="d" mc:Ignorable="d"
<<<<<<< HEAD
<<<<<<< HEAD
Title="MainWindow" Height="400" Width="600"> Title="MainWindow" Height="400" Width="600">
<Grid> <Grid>
<TextBlock HorizontalAlignment="Left" Height="40" Margin="20,10,0,0" TextWrapping="Wrap" Text="SN:" VerticalAlignment="Top" Width="170" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="20,10,0,0" TextWrapping="Wrap" Text="SN:" VerticalAlignment="Top" Width="170" FontSize="24"/>
<TextBox x:Name="sn_id" HorizontalAlignment="Left" Height="40" Margin="170,10,0,0" VerticalAlignment="Top" Width="340" Focusable="False" FontSize="24"/> <TextBox x:Name="sn_id" HorizontalAlignment="Left" Height="40" Margin="170,10,0,0" VerticalAlignment="Top" Width="340" FontSize="24"/>
<TextBlock HorizontalAlignment="Left" Height="40" Margin="20,55,0,0" TextWrapping="Wrap" Text="DURATION:" VerticalAlignment="Top" Width="170" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="20,55,0,0" TextWrapping="Wrap" Text="DURATION:" VerticalAlignment="Top" Width="170" FontSize="24"/>
<TextBlock HorizontalAlignment="Left" Height="40" Margin="400,55,0,0" TextWrapping="Wrap" Text="MONTHS" VerticalAlignment="Top" Width="170" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="400,55,0,0" TextWrapping="Wrap" Text="MONTHS" VerticalAlignment="Top" Width="170" FontSize="24"/>
<ComboBox x:Name="DURATION" HorizontalAlignment="Left" Height="40" Margin="170,55,0,0" VerticalAlignment="Top" Width="220" DropDownClosed="DURATION_DropDownClosed" FontSize="24"> <ComboBox x:Name="DURATION" HorizontalAlignment="Left" Height="40" Margin="170,55,0,0" VerticalAlignment="Top" Width="220" DropDownClosed="DURATION_DropDownClosed" FontSize="24">
@ -23,25 +21,17 @@
<ComboBoxItem Content="FOREVER"></ComboBoxItem> <ComboBoxItem Content="FOREVER"></ComboBoxItem>
</ComboBox> </ComboBox>
<TextBlock HorizontalAlignment="Left" Height="40" Margin="20,100,0,0" TextWrapping="Wrap" Text="TIME:" VerticalAlignment="Top" Width="88" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="20,100,0,0" TextWrapping="Wrap" Text="TIME:" VerticalAlignment="Top" Width="88" FontSize="24"/>
<TextBox x:Name="T_Y" Text="0" HorizontalAlignment="Left" Height="40" Margin="170,100,0,0" VerticalAlignment="Top" Width="220" FontSize="24"/> <TextBox x:Name="T_Y" Text="0" HorizontalAlignment="Left" Height="40" Margin="170,100,0,0" VerticalAlignment="Top" Width="220" FontSize="24" InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Tb_KeyFloating_Y" />
<TextBlock HorizontalAlignment="Left" Height="40" Margin="400,100,0,0" TextWrapping="Wrap" Text="YEAR" VerticalAlignment="Top" Width="170" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="400,100,0,0" TextWrapping="Wrap" Text="YEAR" VerticalAlignment="Top" Width="170" FontSize="24"/>
<TextBox x:Name="T_M" Text="0" HorizontalAlignment="Left" Height="40" Margin="170,145,0,0" VerticalAlignment="Top" Width="220" FontSize="24"/> <TextBox x:Name="T_M" Text="0" HorizontalAlignment="Left" Height="40" Margin="170,145,0,0" VerticalAlignment="Top" Width="220" FontSize="24" InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Tb_KeyFloating_M" />
<TextBlock HorizontalAlignment="Left" Height="40" Margin="400,145,0,0" TextWrapping="Wrap" Text="MONTHS" VerticalAlignment="Top" Width="170" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="400,145,0,0" TextWrapping="Wrap" Text="MONTHS" VerticalAlignment="Top" Width="170" FontSize="24"/>
<TextBox x:Name="T_D" Text="0" HorizontalAlignment="Left" Height="40" Margin="170,190,0,0" VerticalAlignment="Top" Width="220" FontSize="24"/> <TextBox x:Name="T_D" Text="0" HorizontalAlignment="Left" Height="40" Margin="170,190,0,0" VerticalAlignment="Top" Width="220" FontSize="24" InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Tb_KeyFloating_D" />
<TextBlock HorizontalAlignment="Left" Height="40" Margin="400,190,0,0" TextWrapping="Wrap" Text="DAY" VerticalAlignment="Top" Width="170" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="400,190,0,0" TextWrapping="Wrap" Text="DAY" VerticalAlignment="Top" Width="170" FontSize="24"/>
<TextBlock HorizontalAlignment="Left" Height="40" Margin="20,235,0,0" TextWrapping="Wrap" Text="CDKEY:" VerticalAlignment="Top" Width="88" FontSize="24"/> <TextBlock HorizontalAlignment="Left" Height="40" Margin="20,235,0,0" TextWrapping="Wrap" Text="CDKEY:" VerticalAlignment="Top" Width="88" FontSize="24"/>
<TextBox x:Name="cdk" HorizontalAlignment="Left" Height="40" Margin="170,235,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="342" FontSize="24"/> <TextBox x:Name="cdk" HorizontalAlignment="Left" Height="40" Margin="170,235,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="342" FontSize="24"/>
<Button Content="OK" HorizontalAlignment="Left" Height="40" Margin="170,280,0,0" VerticalAlignment="Top" Width="85" Background="#FFE6E6E6" Click="Button_Click"/> <Button Content="OK" HorizontalAlignment="Left" Height="40" Margin="170,280,0,0" VerticalAlignment="Top" Width="85" Background="#FFE6E6E6" Click="Button_Click"/>
=======
Title="MainWindow" Height="450" Width="800">
<Grid>
>>>>>>> 768827d5435e8f9e34d3db0c7dbb91737195a206
=======
Title="MainWindow" Height="450" Width="800">
<Grid>
>>>>>>> 768827d5435e8f9e34d3db0c7dbb91737195a206
</Grid> </Grid>
</Window> </Window>

79
MainWindow.xaml.cs

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@ -11,6 +12,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using SUNLIGHT_CDKEY.UserClass;
namespace SUNLIGHT_CDKEY namespace SUNLIGHT_CDKEY
{ {
@ -19,51 +21,98 @@ namespace SUNLIGHT_CDKEY
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
int d, y, m;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
T_Y.Text = DateTime.Now.ToString("yyyy");
T_M.Text = DateTime.Now.ToString("MM");
T_D.Text = DateTime.Now.ToString("dd");
}
private void Tb_KeyFloating_D(object sender, TextCompositionEventArgs e)//输入事件
{
d = int.Parse(T_D.Text);
//Regex re = new Regex("[^0-9.-]+");
Regex re = new Regex(@"^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");// 非负浮点数
e.Handled = !re.IsMatch(e.Text);
if(d>30)T_D.Text = "30";
}
private void Tb_KeyFloating_Y(object sender, TextCompositionEventArgs e)//输入事件
{
y = int.Parse(T_Y.Text);
//Regex re = new Regex("[^0-9.-]+");
Regex re = new Regex(@"^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");// 非负浮点数
e.Handled = !re.IsMatch(e.Text);
if (y > 2099) T_Y.Text = "2099";
}
private void Tb_KeyFloating_M(object sender, TextCompositionEventArgs e)//输入事件
{
m = int.Parse(T_M.Text);
//Regex re = new Regex("[^0-9.-]+");
Regex re = new Regex(@"^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");// 非负浮点数
e.Handled = !re.IsMatch(e.Text);
if (m > 12) T_M.Text = "12";
} }
<<<<<<< HEAD
<<<<<<< HEAD
private void DURATION_DropDownClosed(object sender, EventArgs e) private void DURATION_DropDownClosed(object sender, EventArgs e)
{ {
T_M.Text = "0"; T_Y.Text = DateTime.Now.ToString("yyyy");
T_Y.Text = "0"; T_M.Text = DateTime.Now.ToString("MM");
T_D.Text = "0"; T_D.Text = DateTime.Now.ToString("dd");
d = int.Parse(T_D.Text);
y = int.Parse(T_Y.Text);
m = int.Parse(T_M.Text);
if (DURATION.Text == "1") if (DURATION.Text == "1")
{ {
T_M.Text = "1"; m += 1;
} }
else if (DURATION.Text == "2") else if (DURATION.Text == "2")
{ {
T_M.Text = "2"; m += 2;
} }
else if (DURATION.Text == "3") else if (DURATION.Text == "3")
{ {
T_M.Text = "3"; m += 3;
} }
else if (DURATION.Text == "6") else if (DURATION.Text == "6")
{ {
T_M.Text = "6"; m += 6;
} }
else if (DURATION.Text == "12") else if (DURATION.Text == "12")
{ {
T_Y.Text = "1"; y += 1;
} }
else if (DURATION.Text == "FOREVER") else if (DURATION.Text == "FOREVER")
{ {
T_Y.Text = "99"; y = 2099;
}
if (m > 12)
{
m = m-12;
y += 1;
} }
T_Y.Text=y.ToString();
T_M.Text=m.ToString();
T_D.Text=d.ToString();
} }
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
{ {
string a,b;
a = sn_id.Text.Substring(0, 6);
b = (255-y).ToString()+ (255 - m).ToString()+(255 - d).ToString();
cdk.Text = a + b + CRCcheck16.ToCRC16(CRCcheck16.StringToHexByte(b), true);
} }
=======
>>>>>>> 768827d5435e8f9e34d3db0c7dbb91737195a206
=======
>>>>>>> 768827d5435e8f9e34d3db0c7dbb91737195a206
} }
} }

1
SUNLIGHT_CDKEY.csproj

@ -66,6 +66,7 @@
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="UserClass\CRCcheck16.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>

193
UserClass/CRCcheck16.cs

@ -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
}
}
Loading…
Cancel
Save