You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.3 KiB
68 lines
2.3 KiB
using System;
|
|
using System.Globalization;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Windows;
|
|
|
|
namespace IoTKeyGen
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Button_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if(string.IsNullOrEmpty(deviceNumber.Text))
|
|
{
|
|
MessageBox.Show("设备编号不能为空");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(this.days.Text))
|
|
{
|
|
MessageBox.Show("过期时间不能为空");
|
|
return;
|
|
}
|
|
var sn = deviceNumber.Text;
|
|
|
|
if(!int.TryParse(this.days.Text, out int days)||days<0)
|
|
{
|
|
MessageBox.Show("过期时间必须是非负整数");
|
|
return;
|
|
}
|
|
var endTime = days == 0 ? 0 : new DateTimeOffset(DateTime.UtcNow.Date.AddDays(days + 1).AddSeconds(-1)).ToUnixTimeSeconds();
|
|
keyCode.Text = DESEncrypt($"{sn}-{endTime}",sn);
|
|
}
|
|
|
|
public static string DESEncrypt(string value, string key)
|
|
{
|
|
using var des = new DESCryptoServiceProvider();
|
|
var inputByteArray = Encoding.Default.GetBytes(value);
|
|
var md5Key = Md5(key).Substring(0, 8);
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(md5Key);
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(md5Key);
|
|
using var ms = new System.IO.MemoryStream();
|
|
using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
var ret = new StringBuilder();
|
|
foreach (var item in ms.ToArray())
|
|
{
|
|
ret.AppendFormat("{0:X2}", item);
|
|
}
|
|
return ret.ToString();
|
|
}
|
|
public static string Md5(string input)
|
|
{
|
|
using MD5 md5 = MD5.Create();
|
|
byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
|
|
return BitConverter.ToString(hash).Replace("-", string.Empty, StringComparison.CurrentCulture).ToLower(CultureInfo.CurrentCulture);
|
|
}
|
|
}
|
|
}
|