同系列文章:
- 国家商用密码(一)SM2椭圆曲线公钥密码算法
- 国家商用密码(二)C#:基于BouncyCastle实现SM3密码杂凑算法
- 国家商用密码(三)C#:从HashAlgorithm类派生实现SM3密码杂凑算法
- 国家商用密码(四)开放动态库及演示程序
- 国家商用密码(五)基于SM2的软件授权码生成及校验
- 国家商用密码(六)椭圆曲线加密算法密钥生成器
- 国家商用密码(七)在Apache MINA上实现二进制流的SM4编解码器
- 国家商用密码(八)在Apache MINA上实现文本流的SM4编解码器
- 国家商用密码(九)在Apache MINA上实现二进制流数据报文的SM4编解码器
- 国家商用密码(十)在Apache MINA上实现文本流数据报文的SM4编解码器
- 国家商用密码(11)在Netty 5.0.X上实现文本流的SM4编解码器
- 国家商用密码(12)在Netty 4.1.12.Final上实现文本流的SM4编解码器
- 国家商用密码(13)在DotNetty 0.4.5上实现文本流的SM4编解码器
程序下载
软件界面
开发环境:
Visual Studio V2015
.NET Framework 4 Client Profile
源代码:
MainWindow.xaml.cs
using System.Windows; using Com.FirstSolver.Security; namespace Splash { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); rbRecommend.IsChecked = true; rbCustom.IsChecked = false; } private void buttonKeyMaker_Click(object sender, RoutedEventArgs e) { // 清除原有数据 textBoxPrivateKey.Clear(); textBoxPublicKeyX.Clear(); textBoxPublicKeyY.Clear(); // 生成新的密钥 BigInteger d; // 私密密钥 ECPoint Q; // 公开密钥 if (rbRecommend.IsChecked == true) { // 官方推荐曲线 int SelectedIndex = cbRecommend.SelectedIndex; switch (SelectedIndex) { case 0: using (SM2 sm2 = new SM2()) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } break; case 1: using (SM2 sm2 = SM2.CreateInstance(192)) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } break; case 2: using (SM2 sm2 = SM2.CreateInstance(224)) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } break; case 3: using (SM2 sm2 = SM2.CreateInstance(256)) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } break; case 4: using (SM2 sm2 = SM2.CreateInstance(384)) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } break; case 5: using (SM2 sm2 = SM2.CreateInstance(521)) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } break; default: return; } } else { // 自定义曲线 try { using (SM2 sm2 = new SM2(textBoxECCurveP.Text, textBoxECCurveA.Text, textBoxECCurveB.Text, textBoxECCurveN.Text, textBoxECCurveGx.Text, textBoxECCurveGy.Text)) { d = sm2.GetKeyPair(out Q); textBoxPrivateKey.Text = Utils.ToString(sm2.GetEncoded(d)); textBoxPublicKeyX.Text = Utils.ToString(sm2.GetEncoded(Q.mX)); textBoxPublicKeyY.Text = Utils.ToString(sm2.GetEncoded(Q.mY)); } } catch(System.Exception) { MessageBoxPlus.Show(this, "椭圆曲线参数错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void buttonCopyPrivateKey_Click(object sender, RoutedEventArgs e) { textBoxPrivateKey.SelectAll(); textBoxPrivateKey.Copy(); } private void buttonCopyPublicKeyX_Click(object sender, RoutedEventArgs e) { textBoxPublicKeyX.SelectAll(); textBoxPublicKeyX.Copy(); } private void buttonCopyPublicKeyY_Click(object sender, RoutedEventArgs e) { textBoxPublicKeyY.SelectAll(); textBoxPublicKeyY.Copy(); } private void rbRecommend_Checked(object sender, RoutedEventArgs e) { rbCustom.IsChecked = false; expanderECCurve.IsExpanded = false; } private void rbCustom_Checked(object sender, RoutedEventArgs e) { rbRecommend.IsChecked = false; expanderECCurve.IsExpanded = true; } } }
MainWindow.xaml
<Window 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:Splash" xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="Splash.MainWindow" mc:Ignorable="d" Title="椭圆曲线加密算法密钥生成器" Height="480" Width="640" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" SizeToContent="Height" Icon="FireEyes.ico" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <GroupBox Grid.Row="0" Margin="8"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <RadioButton x:Name="rbRecommend" Content="官方推荐曲线" Margin="8" VerticalContentAlignment="Center" Checked="rbRecommend_Checked" /> <ComboBox x:Name="cbRecommend" Margin="8" Padding="8" SelectedIndex="0" VerticalContentAlignment="Center" > <System:String>国家密码管理局 P-256</System:String> <System:String>美国国家标准与技术研究院 P-192</System:String> <System:String>美国国家标准与技术研究院 P-224</System:String> <System:String>美国国家标准与技术研究院 P-256</System:String> <System:String>美国国家标准与技术研究院 P-384</System:String> <System:String>美国国家标准与技术研究院 P-521</System:String> </ComboBox> </StackPanel> <StackPanel Grid.Row="1" Orientation="Vertical"> <RadioButton Name="rbCustom" Content="自定义曲线" Margin="8" VerticalContentAlignment="Center" Checked="rbCustom_Checked" /> <Expander Name="expanderECCurve" Margin="8" Header="椭圆曲线参数设置(16进制字符串)"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="4,8" Text="P" /> <TextBlock Grid.Row="1" Grid.Column="0" Margin="4,8" Text="A" /> <TextBlock Grid.Row="2" Grid.Column="0" Margin="4,8" Text="B" /> <TextBlock Grid.Row="3" Grid.Column="0" Margin="4,8" Text="N" /> <TextBlock Grid.Row="4" Grid.Column="0" Margin="4,8" Text="Gx" /> <TextBlock Grid.Row="5" Grid.Column="0" Margin="4,8" Text="Gy" /> <TextBox Name="textBoxECCurveP" Grid.Row="0" Grid.Column="1" Margin="4,8" TextWrapping="Wrap" /> <TextBox Name="textBoxECCurveA" Grid.Row="1" Grid.Column="1" Margin="4,8" TextWrapping="Wrap" /> <TextBox Name="textBoxECCurveB" Grid.Row="2" Grid.Column="1" Margin="4,8" TextWrapping="Wrap" /> <TextBox Name="textBoxECCurveN" Grid.Row="3" Grid.Column="1" Margin="4,8" TextWrapping="Wrap" /> <TextBox Name="textBoxECCurveGx" Grid.Row="4" Grid.Column="1" Margin="4,8" TextWrapping="Wrap" /> <TextBox Name="textBoxECCurveGy" Grid.Row="5" Grid.Column="1" Margin="4,8" TextWrapping="Wrap" /> </Grid> </Expander> </StackPanel> </Grid> </GroupBox> <Button x:Name="buttonKeyMaker" Grid.Row="1" Margin="8" Width="100" Height="40" Content="生成密钥" Click="buttonKeyMaker_Click" /> <GroupBox Grid.Row="2" Margin="8"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Margin="4,8" Text="私钥d:" /> <TextBlock Grid.Row="1" Grid.Column="0" Margin="4,8" Text="公钥P.X:" /> <TextBlock Grid.Row="2" Grid.Column="0" Margin="4,8" Text="公钥P.Y:" /> <TextBox x:Name="textBoxPrivateKey" Grid.Row="0" Grid.Column="1" Margin="4,8" IsReadOnly="True" TextWrapping="Wrap" /> <TextBox x:Name="textBoxPublicKeyX" Grid.Row="1" Grid.Column="1" Margin="4,8" IsReadOnly="True" TextWrapping="Wrap" /> <TextBox x:Name="textBoxPublicKeyY" Grid.Row="2" Grid.Column="1" Margin="4,8" IsReadOnly="True" TextWrapping="Wrap" /> <Button x:Name="buttonCopyPrivateKey" Grid.Row="0" Grid.Column="2" Margin="4,8" Padding="4,0" Content="复制" Click="buttonCopyPrivateKey_Click" /> <Button x:Name="buttonCopyPublicKeyX" Grid.Row="1" Grid.Column="2" Margin="4,8" Padding="4,0" Content="复制" Click="buttonCopyPublicKeyX_Click" /> <Button x:Name="buttonCopyPublicKeyY" Grid.Row="2" Grid.Column="2" Margin="4,8" Padding="4,0" Content="复制" Click="buttonCopyPublicKeyY_Click" /> </Grid> </GroupBox> </Grid> </Window>