Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cc75a4d51 | ||
|
|
dd8bedf452 | ||
|
|
4e9fb61ff0 | ||
|
|
1a7669f1bf | ||
|
|
4e40658493 | ||
|
|
608f121b5d | ||
|
|
6b06bb643e | ||
|
|
60bc8223b0 | ||
|
|
b8878d4223 | ||
|
|
eadb411c73 | ||
|
|
2fa0249314 | ||
|
|
481d85d04a | ||
|
|
8793b264d8 | ||
|
|
537c89677e | ||
|
|
cc3c806c97 | ||
|
|
7b04a24520 |
No files matched your search
+193
@@ -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
|
||||
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 4.2 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 231 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 59 KiB |
+3
-2
@@ -5,10 +5,11 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:SUNLIGHT_CDKEY"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="400" Width="600">
|
||||
Title="SUNLIGHT_CDKEY" Height="400" Width="600">
|
||||
<Grid>
|
||||
<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" FontSize="24"/>
|
||||
<TextBox x:Name="sn_id" HorizontalAlignment="Left" Height="40" Margin="170,10,0,0" VerticalAlignment="Top" Width="340" FontSize="24"
|
||||
PreviewTextInput="AlphanumericValidationTextBox" InputMethod.IsInputMethodEnabled="False"/>
|
||||
<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"/>
|
||||
<ComboBox x:Name="DURATION" HorizontalAlignment="Left" Height="40" Margin="170,55,0,0" VerticalAlignment="Top" Width="220" DropDownClosed="DURATION_DropDownClosed" FontSize="24">
|
||||
|
||||
+34
-3
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using formula_manage.UserClass;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
@@ -29,6 +31,15 @@ namespace SUNLIGHT_CDKEY
|
||||
T_M.Text = DateTime.Now.ToString("MM");
|
||||
T_D.Text = DateTime.Now.ToString("dd");
|
||||
}
|
||||
private void AlphanumericValidationTextBox(object sender, TextCompositionEventArgs e)
|
||||
{
|
||||
Regex regex = new Regex(@"[^a-eA-E0-9]+$");
|
||||
e.Handled = regex.IsMatch(e.Text.ToUpper());
|
||||
}
|
||||
private void validation_PreviewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key == Key.Space) e.Handled = true;
|
||||
}
|
||||
|
||||
private void Tb_KeyFloating_D(object sender, TextCompositionEventArgs e)//输入事件
|
||||
{
|
||||
@@ -104,9 +115,29 @@ namespace SUNLIGHT_CDKEY
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
string t;
|
||||
t = sn_id.Text.Substring(0, 6);
|
||||
string a;
|
||||
string b, my, mm, md;
|
||||
|
||||
sn_id.Text = sn_id.Text.ToUpper();
|
||||
|
||||
if (sn_id.Text.Length < 6)//id字节长度大于6有效
|
||||
{
|
||||
sn_id.BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 0, 0));
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
sn_id.BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 255, 255));
|
||||
}
|
||||
|
||||
a= sn_id.Text.Substring(5, 1)+ sn_id.Text.Substring(0, 1)+ sn_id.Text.Substring(4, 1) + sn_id.Text.Substring(1, 1) + sn_id.Text.Substring(3, 1)+ sn_id.Text.Substring(2, 1);
|
||||
|
||||
my = Convert.ToString(Convert.ToInt32(2255 - y), 16).ToUpper();
|
||||
mm = Convert.ToString(Convert.ToInt32(255 - m), 16).ToUpper();
|
||||
md = Convert.ToString(Convert.ToInt32(255-d), 16).ToUpper();
|
||||
b=my+mm+md;
|
||||
|
||||
cdk.Text = a + b + CRCcheck16.ToCRC16(CRCcheck16.StringToHexByte(b), true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ApplicationIcon>sunlight_logo.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
@@ -66,6 +69,7 @@
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="CRCcheck16.cs" />
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DesignTime>True</DesignTime>
|
||||
@@ -85,5 +89,8 @@
|
||||
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Resource Include="sunlight_logo.ico" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
Reference in new issue
Block a user