-
客户端
-
界面
<Window x:Class="RegistrationClient.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RegistrationClient"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="300">
<Grid>
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="9,32,0,0" TextWrapping="Wrap" Text="账号 :" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="58,32,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="165"/>
<TextBlock x:Name="textBlock1_Copy" HorizontalAlignment="Left" Margin="6,83,0,0" TextWrapping="Wrap" Text="机器码 :" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1_jiqima" HorizontalAlignment="Left" Height="23" Margin="58,83,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="165"/>
<TextBlock x:Name="textBlock1_Copy1" HorizontalAlignment="Left" Margin="4,127,0,0" TextWrapping="Wrap" Text="GUID码 :" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1_GUID" HorizontalAlignment="Left" Height="23" Margin="58,127,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="165"/>
<Button x:Name="button" Content="生成GUID" HorizontalAlignment="Left" Margin="90,176,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="24,238,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="8,212,0,0" TextWrapping="Wrap" Text="注册码" VerticalAlignment="Top" RenderTransformOrigin="0.5,-0.591"/>
<RichTextBox x:Name="richTextBox" HorizontalAlignment="Left" Height="107" Margin="58,214,0,0" VerticalAlignment="Top" Width="209">
<FlowDocument>
<Paragraph>
<Run x:Name="txtzhucema" Text=""/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button x:Name="button1" Content="注册码验证" HorizontalAlignment="Left" Margin="90,340,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>
<Button x:Name="button2" Content="机器码" HorizontalAlignment="Left" Margin="237,87,0,0" VerticalAlignment="Top" Width="45" Click="Button2_Click"/>
</Grid>
</Window>
-
后台
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 机器码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button2_Click(object sender, RoutedEventArgs e)
{
string strMachineCode = string.Empty;
string strCpu = string.Empty;
string strDiskC = string.Empty;
ManagementClass myCpu = new ManagementClass("win32_Processor");
ManagementObjectCollection myCpuCollections = myCpu.GetInstances();
foreach (ManagementObject obj in myCpuCollections)
{
strCpu = obj.Properties["Processorid"].Value.ToString();
}
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
strDiskC = disk.GetPropertyValue("VolumeSerialNumber").ToString();
strMachineCode = strCpu + strDiskC;
textBox1_jiqima.Text = strMachineCode;
}
/// <summary>
/// 生成注册码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入帐号名!");
return;
}
else
{
if (textBox1_jiqima.Text == "")
{
MessageBox.Show("请先获取机器码!");
return;
}
else
{
textBox1_GUID.Text = textBox1.Text.Trim() + textBox1_jiqima.Text.Trim();
writeGUID();
}
}
}
/// <summary>
/// 验证注册码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, RoutedEventArgs e)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024))
{
rsa.FromXmlString(getPubkey());
//获取并处理唯一特征值
byte[] source = ASCIIEncoding.ASCII.GetBytes(getGUID());
RSAPKCS1SignatureDeformatter decry = new RSAPKCS1SignatureDeformatter(rsa);
decry.SetHashAlgorithm("SHA1");
//格式化唯一特征值
SHA1Managed sha = new SHA1Managed();
byte[] arr = sha.ComputeHash(source);
TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
byte[] signed = Convert.FromBase64String(tr.Text);
if (decry.VerifySignature(arr, signed))
{
MessageBox.Show("注册成功!");
}
else
{
MessageBox.Show("注册失败!");
}
}
}
/// <summary>
/// 写入GUID文本流
/// </summary>
private void writeGUID()
{
string fileName = "guid.txt";
if (!File.Exists(fileName))
{
using (StreamWriter sw = File.CreateText(fileName))
{
sw.WriteLine(textBox1_GUID.Text.Trim());
sw.Close();
}
}
else
{
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(textBox1_GUID.Text.Trim());
sw.Close();
}
}
}
/// <summary>
/// 获取公钥
/// </summary>
/// <returns></returns>
private string getPubkey()
{
string pubkey = string.Empty;
try
{
string path = string.Empty;
path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
path += "pubkey.xml";
using (StreamReader sr = new StreamReader(path))
{
string line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
pubkey += line;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return pubkey.Replace(" ", "");
}
/// <summary>
/// 获取GUID码
/// </summary>
/// <returns></returns>
private string getGUID()
{
string guid = string.Empty;
try
{
string fileName = "guid.txt";
using (StreamReader sr = new StreamReader(fileName))
{
string line;
while ((line = sr.ReadLine()) != null)
{
guid += line;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return guid.Replace(" ", "");
}
}
-
xml文件
-
<RSAKeyValue> <Modulus>uh9KusRbX08YT7bn1K8Wp5KmKAAnxCUAeeGqCem36EtBlkIHl1Cs+p4RPgrRt2m9uJOMf8NjFpM0f+9GcK+DJgGl1X91XK615qiIIfa0zjvzdNNOBKx0j8Kn4+HWi0466k1QH6ELAe9RsY53X4r1PZXFDbHc42Mom032zfz5on0=</Modulus> <Exponent>AQAB</Exponent> </RSAKeyValue>
-
服务度端
-
界面
<Window x:Class="OpenEyeRegistrationService.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:OpenEyeRegistrationService"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="344*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="24,118,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top"/>
<TextBlock x:Name="textBlock2" HorizontalAlignment="Left" Margin="8,92,0,0" TextWrapping="Wrap" Text="注册码" VerticalAlignment="Top" RenderTransformOrigin="0.5,-0.591"/>
<RichTextBox x:Name="richTextBox" HorizontalAlignment="Left" Height="107" Margin="58,94,0,0" VerticalAlignment="Top" Width="303">
<FlowDocument>
<Paragraph>
<Run x:Name="txtzhucema" Text=""/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button x:Name="button1" Content="生成注册码" HorizontalAlignment="Left" Margin="189,220,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/>
<TextBlock x:Name="textBlock1_Copy1" HorizontalAlignment="Left" Margin="4,32,0,0" TextWrapping="Wrap" Text="GUID码 :" VerticalAlignment="Top"/>
<TextBox x:Name="textBox1_GUID" HorizontalAlignment="Left" Height="23" Margin="58,32,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="303"/>
<Button x:Name="button1_Copy" Content="导出公钥" HorizontalAlignment="Left" Margin="286,220,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Copy_Click" RenderTransformOrigin="1.905,0.712"/>
</Grid>
</Window>
-
后台
-
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 生成注册码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender, RoutedEventArgs e) { if (textBox1_GUID.Text == "") { MessageBox.Show("请输入GUID码!"); return; } else { CspParameters cspPara = new CspParameters(); cspPara.KeyContainerName = "TestTT"; cspPara.KeyNumber = 2; cspPara.Flags = CspProviderFlags.UseMachineKeyStore; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspPara)) { //获取GUID体征值 byte[] source = ASCIIEncoding.ASCII.GetBytes(textBox1_GUID.Text.Trim()); //定义加密对象 RSAPKCS1SignatureFormatter encry = new RSAPKCS1SignatureFormatter(rsa); //设置HASH签名算法 encry.SetHashAlgorithm("SHA1"); //格式化唯一特征值 SHA1Managed sha = new SHA1Managed(); byte[] arr = sha.ComputeHash(source); byte[] signed = encry.CreateSignature(arr); txtzhucema.Text = Convert.ToBase64String(signed); } } } /// <summary> /// 导出公钥 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Copy_Click(object sender, RoutedEventArgs e) { try { CspParameters cspPara = new CspParameters(); cspPara.KeyContainerName = "TestTT"; cspPara.KeyNumber = 2; cspPara.Flags = CspProviderFlags.UseMachineKeyStore; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspPara)) { string pubkey = rsa.ToXmlString(false); XmlDocument doc = new XmlDocument(); doc.LoadXml(pubkey); doc.Save("pubkey.xml"); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }