KeyGenWizard.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:21k
源码类别:

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: KeyGenWizard.cs,v 1.2 2005/04/20 08:45:45 okajima Exp $
  4. */
  5. using System;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Drawing;
  9. using System.Collections;
  10. using System.ComponentModel;
  11. using System.Windows.Forms;
  12. using System.Diagnostics;
  13. using Granados.PKI;
  14. using Granados.SSHCV2;
  15. namespace Poderosa.Forms
  16. {
  17. /// <summary>
  18. /// KeyGenWizard 偺奣梫偺愢柧偱偡丅
  19. /// </summary>
  20. public class KeyGenWizard : System.Windows.Forms.Form
  21. {
  22. //尰嵼偺儁乕僕
  23. private enum Page {
  24. Parameter,
  25. Generation,
  26. Store
  27. }
  28. private Page _page;
  29. private KeyGenThread _keyGenThread;
  30. private SSH2UserAuthKey _resultKey;
  31. private System.Windows.Forms.Panel _parameterPanel;
  32. private System.Windows.Forms.Button _nextButton;
  33. public System.Windows.Forms.Button _cancelButton;
  34. private System.Windows.Forms.Label _promptLabel1;
  35. private System.Windows.Forms.Label _algorithmLabel;
  36. private System.Windows.Forms.Label _bitCountLabel;
  37. private ComboBox _algorithmBox;
  38. private ComboBox _bitCountBox;
  39. private System.Windows.Forms.Panel _generationPanel;
  40. private System.Windows.Forms.Label _keygenLabel;
  41. private ProgressBar _generationBar;
  42. private System.Windows.Forms.Panel _storePanel;
  43. private System.Windows.Forms.Label _completeLabel;
  44. private System.Windows.Forms.Button _storePrivateKey;
  45. private System.Windows.Forms.Button _storeSECSHPublicKeyButton;
  46. private System.Windows.Forms.Button _storeOpenSSHPublicKeyButton;
  47. private System.Windows.Forms.Label _passphraseLabel;
  48. private TextBox _passphraseBox;
  49. private TextBox _confirmBox;
  50. private System.Windows.Forms.Label _confirmLabel;
  51. /// <summary>
  52. /// 昁梫側僨僓僀僫曄悢偱偡丅
  53. /// </summary>
  54. private System.ComponentModel.Container components = null;
  55. public KeyGenWizard()
  56. {
  57. //
  58. // Windows 僼僅乕儉 僨僓僀僫 僒億乕僩偵昁梫偱偡丅
  59. //
  60. InitializeComponent();
  61. //
  62. // TODO: InitializeComponent 屇傃弌偟偺屻偵丄僐儞僗僩儔僋僞 僐乕僪傪捛壛偟偰偔偩偝偄丅
  63. //
  64. if(!this.DesignMode)
  65. this.Width = PanelPitch;
  66. this._confirmLabel.Text = GApp.Strings.GetString("Form.KeyGenWizard._confirmLabel");
  67. this._passphraseLabel.Text = GApp.Strings.GetString("Form.KeyGenWizard._passphraseLabel");
  68. this._bitCountLabel.Text = GApp.Strings.GetString("Form.KeyGenWizard._bitCountLabel");
  69. this._algorithmLabel.Text = GApp.Strings.GetString("Form.KeyGenWizard._algorithmLabel");
  70. this._promptLabel1.Text = GApp.Strings.GetString("Form.KeyGenWizard._promptLabel1");
  71. this._nextButton.Text = GApp.Strings.GetString("Form.KeyGenWizard._nextButton");
  72. this._cancelButton.Text = GApp.Strings.GetString("Common.Cancel");
  73. this._keygenLabel.Text = GApp.Strings.GetString("Form.KeyGenWizard._keygenLabel");
  74. this._storePrivateKey.Text = GApp.Strings.GetString("Form.KeyGenWizard._storePrivateKey");
  75. this._storeSECSHPublicKeyButton.Text = GApp.Strings.GetString("Form.KeyGenWizard._storeSECSHPublicKeyButton");
  76. this._storeOpenSSHPublicKeyButton.Text = GApp.Strings.GetString("Form.KeyGenWizard._storeOpenSSHPublicKeyButton");
  77. this._completeLabel.Text = GApp.Strings.GetString("Form.KeyGenWizard._completeLabel");
  78. this.Text = GApp.Strings.GetString("Form.KeyGenWizard.Text");
  79. _page=Page.Parameter;
  80. }
  81. public void SetResultKey(SSH2UserAuthKey key) {
  82. _resultKey = key;
  83. }
  84. private int PanelPitch {
  85. get {
  86. return _parameterPanel.Width+8;
  87. }
  88. }
  89. /// <summary>
  90. /// 巊梡偝傟偰偄傞儕僜乕僗偵屻張棟傪幚峴偟傑偡丅
  91. /// </summary>
  92. protected override void Dispose( bool disposing )
  93. {
  94. if( disposing )
  95. {
  96. if(components != null)
  97. {
  98. components.Dispose();
  99. }
  100. }
  101. base.Dispose( disposing );
  102. }
  103. #region Windows Form Designer generated code
  104. /// <summary>
  105. /// 僨僓僀僫 僒億乕僩偵昁梫側儊僜僢僪偱偡丅偙偺儊僜僢僪偺撪梕傪
  106. /// 僐乕僪 僄僨傿僞偱曄峏偟側偄偱偔偩偝偄丅
  107. /// </summary>
  108. private void InitializeComponent()
  109. {
  110. this._parameterPanel = new System.Windows.Forms.Panel();
  111. this._confirmBox = new TextBox();
  112. this._confirmLabel = new System.Windows.Forms.Label();
  113. this._passphraseBox = new TextBox();
  114. this._passphraseLabel = new System.Windows.Forms.Label();
  115. this._bitCountBox = new ComboBox();
  116. this._bitCountLabel = new System.Windows.Forms.Label();
  117. this._algorithmLabel = new System.Windows.Forms.Label();
  118. this._promptLabel1 = new System.Windows.Forms.Label();
  119. this._algorithmBox = new ComboBox();
  120. this._nextButton = new System.Windows.Forms.Button();
  121. this._cancelButton = new System.Windows.Forms.Button();
  122. this._generationPanel = new System.Windows.Forms.Panel();
  123. this._generationBar = new ProgressBar();
  124. this._keygenLabel = new System.Windows.Forms.Label();
  125. this._storePanel = new System.Windows.Forms.Panel();
  126. this._storeSECSHPublicKeyButton = new System.Windows.Forms.Button();
  127. this._storeOpenSSHPublicKeyButton = new System.Windows.Forms.Button();
  128. this._storePrivateKey = new System.Windows.Forms.Button();
  129. this._completeLabel = new System.Windows.Forms.Label();
  130. this._parameterPanel.SuspendLayout();
  131. this._generationPanel.SuspendLayout();
  132. this._storePanel.SuspendLayout();
  133. this.SuspendLayout();
  134. // 
  135. // _parameterPanel
  136. // 
  137. this._parameterPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
  138.   this._confirmBox,
  139.   this._confirmLabel,
  140.   this._passphraseBox,
  141.   this._passphraseLabel,
  142.   this._bitCountBox,
  143.   this._bitCountLabel,
  144.   this._algorithmLabel,
  145.   this._promptLabel1,
  146.   this._algorithmBox});
  147. this._parameterPanel.Location = new System.Drawing.Point(0, 8);
  148. this._parameterPanel.Name = "_parameterPanel";
  149. this._parameterPanel.Size = new System.Drawing.Size(304, 184);
  150. this._parameterPanel.TabIndex = 0;
  151. // 
  152. // _confirmBox
  153. // 
  154. this._confirmBox.Location = new System.Drawing.Point(128, 128);
  155. this._confirmBox.Name = "_confirmBox";
  156. this._confirmBox.PasswordChar = '*';
  157. this._confirmBox.Size = new System.Drawing.Size(152, 19);
  158. this._confirmBox.TabIndex = 8;
  159. this._confirmBox.Text = "";
  160. // 
  161. // _confirmLabel
  162. // 
  163. this._confirmLabel.Location = new System.Drawing.Point(16, 128);
  164. this._confirmLabel.Name = "_confirmLabel";
  165. this._confirmLabel.TabIndex = 7;
  166. this._confirmLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  167. // 
  168. // _passphraseBox
  169. // 
  170. this._passphraseBox.Location = new System.Drawing.Point(128, 104);
  171. this._passphraseBox.Name = "_passphraseBox";
  172. this._passphraseBox.PasswordChar = '*';
  173. this._passphraseBox.Size = new System.Drawing.Size(152, 19);
  174. this._passphraseBox.TabIndex = 6;
  175. this._passphraseBox.Text = "";
  176. // 
  177. // _passphraseLabel
  178. // 
  179. this._passphraseLabel.Location = new System.Drawing.Point(16, 104);
  180. this._passphraseLabel.Name = "_passphraseLabel";
  181. this._passphraseLabel.TabIndex = 5;
  182. this._passphraseLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  183. // 
  184. // _bitCountBox
  185. // 
  186. this._bitCountBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  187. this._bitCountBox.Items.AddRange(new object[] {
  188.   "768",
  189.   "1024",
  190.   "2048"});
  191. this._bitCountBox.Location = new System.Drawing.Point(128, 80);
  192. this._bitCountBox.SelectedIndex = 0;
  193. this._bitCountBox.Name = "_bitCountBox";
  194. this._bitCountBox.Size = new System.Drawing.Size(121, 20);
  195. this._bitCountBox.TabIndex = 4;
  196. // 
  197. // _bitCountLabel
  198. // 
  199. this._bitCountLabel.Location = new System.Drawing.Point(16, 80);
  200. this._bitCountLabel.Name = "_bitCountLabel";
  201. this._bitCountLabel.TabIndex = 3;
  202. this._bitCountLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  203. // 
  204. // _algorithmLabel
  205. // 
  206. this._algorithmLabel.Location = new System.Drawing.Point(16, 56);
  207. this._algorithmLabel.Name = "_algorithmLabel";
  208. this._algorithmLabel.TabIndex = 1;
  209. this._algorithmLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  210. // 
  211. // _promptLabel1
  212. // 
  213. this._promptLabel1.Location = new System.Drawing.Point(8, 8);
  214. this._promptLabel1.Name = "_promptLabel1";
  215. this._promptLabel1.Size = new System.Drawing.Size(288, 40);
  216. this._promptLabel1.TabIndex = 0;
  217. // 
  218. // _algorithmBox
  219. // 
  220. this._algorithmBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  221. this._algorithmBox.Items.AddRange(new object[] {
  222.    "DSA",
  223.    "RSA"});
  224. this._algorithmBox.Location = new System.Drawing.Point(128, 56);
  225. this._algorithmBox.Name = "_algorithmBox";
  226. this._algorithmBox.SelectedIndex = 0;
  227. this._algorithmBox.Size = new System.Drawing.Size(121, 20);
  228. this._algorithmBox.TabIndex = 2;
  229. // 
  230. // _nextButton
  231. // 
  232. this._nextButton.Location = new System.Drawing.Point(224, 192);
  233. this._nextButton.Name = "_nextButton";
  234. this._nextButton.FlatStyle = FlatStyle.System;
  235. this._nextButton.TabIndex = 1;
  236. this._nextButton.Click += new System.EventHandler(this.OnNext);
  237. // 
  238. // _cancelButton
  239. // 
  240. this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  241. this._cancelButton.Location = new System.Drawing.Point(136, 192);
  242. this._cancelButton.Name = "_cancelButton";
  243. this._cancelButton.FlatStyle = FlatStyle.System;
  244. this._cancelButton.TabIndex = 2;
  245. // 
  246. // _generationPanel
  247. // 
  248. this._generationPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
  249.    this._generationBar,
  250.    this._keygenLabel});
  251. this._generationPanel.Location = new System.Drawing.Point(312, 8);
  252. this._generationPanel.Name = "_generationPanel";
  253. this._generationPanel.Size = new System.Drawing.Size(304, 184);
  254. this._generationPanel.TabIndex = 5;
  255. this._generationPanel.Visible = false;
  256. // 
  257. // _generationBar
  258. // 
  259. this._generationBar.Location = new System.Drawing.Point(8, 80);
  260. this._generationBar.Maximum = 200;
  261. this._generationBar.Minimum = 0;
  262. this._generationBar.Name = "_generationBar";
  263. this._generationBar.Size = new System.Drawing.Size(288, 24);
  264. this._generationBar.Step = 1;
  265. this._generationBar.TabIndex = 1;
  266. this._generationBar.Value = 0;
  267. // 
  268. // _keygenLabel
  269. // 
  270. this._keygenLabel.Location = new System.Drawing.Point(8, 8);
  271. this._keygenLabel.Name = "_keygenLabel";
  272. this._keygenLabel.Size = new System.Drawing.Size(288, 40);
  273. this._keygenLabel.TabIndex = 0;
  274. // 
  275. // _storePanel
  276. // 
  277. this._storePanel.Controls.AddRange(new System.Windows.Forms.Control[] {
  278.   this._storeSECSHPublicKeyButton,
  279.   this._storeOpenSSHPublicKeyButton,
  280.   this._storePrivateKey,
  281.   this._completeLabel});
  282. this._storePanel.Location = new System.Drawing.Point(624, 8);
  283. this._storePanel.Name = "_storePanel";
  284. this._storePanel.Size = new System.Drawing.Size(304, 184);
  285. this._storePanel.TabIndex = 6;
  286. this._storePanel.Visible = false;
  287. // 
  288. // _storePrivateKey
  289. // 
  290. this._storePrivateKey.Location = new System.Drawing.Point(24, 56);
  291. this._storePrivateKey.Name = "_storePrivateKey";
  292. this._storePrivateKey.FlatStyle = FlatStyle.System;
  293. this._storePrivateKey.Size = new System.Drawing.Size(256, 23);
  294. this._storePrivateKey.TabIndex = 2;
  295. this._storePrivateKey.Click += new System.EventHandler(this.OnSavePrivateKey);
  296. // 
  297. // _storeSECSHPublicKeyButton
  298. // 
  299. this._storeSECSHPublicKeyButton.Location = new System.Drawing.Point(24, 96);
  300. this._storeSECSHPublicKeyButton.Name = "_storeSECSHPublicKeyButton";
  301. this._storeSECSHPublicKeyButton.FlatStyle = FlatStyle.System;
  302. this._storeSECSHPublicKeyButton.Size = new System.Drawing.Size(256, 23);
  303. this._storeSECSHPublicKeyButton.TabIndex = 3;
  304. this._storeSECSHPublicKeyButton.Click += new System.EventHandler(this.OnSaveSECSHPublicKey);
  305. // 
  306. // _storeOpenSSHPublicKeyButton
  307. // 
  308. this._storeOpenSSHPublicKeyButton.Location = new System.Drawing.Point(24, 136);
  309. this._storeOpenSSHPublicKeyButton.Name = "_storeOpenSSHPublicKeyButton";
  310. this._storeOpenSSHPublicKeyButton.FlatStyle = FlatStyle.System;
  311. this._storeOpenSSHPublicKeyButton.Size = new System.Drawing.Size(256, 23);
  312. this._storeOpenSSHPublicKeyButton.TabIndex = 4;
  313. this._storeOpenSSHPublicKeyButton.Click += new System.EventHandler(this.OnSaveOpenSSHPublicKey);
  314. // 
  315. // _completeLabel
  316. // 
  317. this._completeLabel.Location = new System.Drawing.Point(8, 8);
  318. this._completeLabel.Name = "_completeLabel";
  319. this._completeLabel.Size = new System.Drawing.Size(288, 40);
  320. this._completeLabel.TabIndex = 1;
  321. // 
  322. // KeyGenWizard
  323. // 
  324. this.AcceptButton = this._nextButton;
  325. this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
  326. this.CancelButton = this._cancelButton;
  327. this.ClientSize = new System.Drawing.Size(930, 223);
  328. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  329.   this._storePanel,
  330.   this._cancelButton,
  331.   this._nextButton,
  332.   this._parameterPanel,
  333.   this._generationPanel});
  334. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  335. this.MaximizeBox = false;
  336. this.MinimizeBox = false;
  337. this.Name = "KeyGenWizard";
  338. this.ShowInTaskbar = false;
  339. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  340. this._parameterPanel.ResumeLayout(false);
  341. this._generationPanel.ResumeLayout(false);
  342. this._storePanel.ResumeLayout(false);
  343. this.ResumeLayout(false);
  344. }
  345. #endregion
  346. public ProgressBar GenerationBar {
  347. get {
  348. return _generationBar;
  349. }
  350. }
  351. private PublicKeyAlgorithm KeyAlgorithm {
  352. get {
  353. if(_algorithmBox.Text=="RSA")
  354. return PublicKeyAlgorithm.RSA;
  355. else
  356. return PublicKeyAlgorithm.DSA;
  357. }
  358. }
  359. private bool VerifyPassphrase() {
  360. if(_passphraseBox.Text!=_confirmBox.Text) {
  361. GUtil.Warning(this, GApp.Strings.GetString("Message.KeyGenWizard.NotMatch"));
  362. return false;
  363. }
  364. else if(_passphraseBox.Text.Length==0) {
  365. return DialogResult.Yes==GUtil.AskUserYesNo(this, GApp.Strings.GetString("Message.KeyGenWizard.ConfirmEmptyPassphrase"));
  366. }
  367. else
  368. return true;
  369. }
  370. private void OnNext(object sender, EventArgs args) {
  371. switch(_page) {
  372. case Page.Parameter:
  373. if(!VerifyPassphrase()) return;
  374. _parameterPanel.Visible = false;
  375. _generationPanel.Visible = true;
  376. _generationPanel.Left -= PanelPitch;
  377. _keyGenThread = new KeyGenThread(this, KeyAlgorithm, Int32.Parse(_bitCountBox.Text));
  378. this.MouseMove += new MouseEventHandler(_keyGenThread.OnMouseMove);
  379. _generationPanel.MouseMove += new MouseEventHandler(_keyGenThread.OnMouseMove);
  380. _nextButton.Enabled = false;
  381. _page = Page.Generation;
  382. _keyGenThread.Start();
  383. break;
  384. case Page.Generation:
  385. _generationPanel.Visible = false;
  386. _storePanel.Visible = true;
  387. _storePanel.Left -= PanelPitch*2;
  388. _page = Page.Store;
  389. _nextButton.Text = GApp.Strings.GetString("Message.KeyGenWizard.Finish");
  390. break;
  391. case Page.Store:
  392. Close();
  393. break;
  394. }
  395. }
  396. protected override void WndProc(ref Message msg) {
  397. base.WndProc(ref msg);
  398. if(msg.Msg==GConst.WMG_KEYGEN_PROGRESS) {
  399. _generationBar.Value = msg.LParam.ToInt32();
  400. }
  401. else if(msg.Msg==GConst.WMG_KEYGEN_FINISHED) {
  402. CheckGenerationComplete();
  403. }
  404. }
  405. protected override void OnClosed(EventArgs args) {
  406. if(_keyGenThread!=null) _keyGenThread.SetAbortFlag();
  407. base.OnClosed(args);
  408. }
  409. public void SetProgressValue(int v) {
  410. _generationBar.Value = v;
  411. if(v==_generationBar.Maximum) {
  412. _keygenLabel.Text = GApp.Strings.GetString("Message.KeyGenWizard.RandomNumberCompleted");
  413. this.Cursor = Cursors.WaitCursor;
  414. CheckGenerationComplete();
  415. }
  416. }
  417. private void CheckGenerationComplete() {
  418. //僾儘僌儗僗僶乕偑廔抂偵偄偔偺偲丄尞偺惗惉偑廔傢傞偺偼椉曽枮偨偝側偄偲偄偗側偄
  419. if(_generationBar.Value==_generationBar.Maximum && _resultKey!=null) {
  420. _nextButton.Enabled = true;
  421. _keygenLabel.Text = GApp.Strings.GetString("Message.KeyGenWizard.GenerationCompleted");
  422. this.Cursor = Cursors.Default;
  423. }
  424. }
  425. private void OnSavePrivateKey(object sender, EventArgs args) {
  426. SaveFileDialog dlg = new SaveFileDialog();
  427. dlg.InitialDirectory = GApp.Options.DefaultKeyDir;
  428. dlg.Title = GApp.Strings.GetString("Caption.KeyGenWizard.SavePrivateKey");
  429. if(GCUtil.ShowModalDialog(this, dlg)==DialogResult.OK) {
  430. GApp.Options.DefaultKeyDir = GUtil.FileToDir(dlg.FileName);
  431. try {
  432. string pp = _passphraseBox.Text;
  433. if(pp.Length==0) pp = null; //嬻僷僗僼儗乕僘偼null巜掕
  434. _resultKey.WritePrivatePartInSECSHStyleFile(new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write), "", pp);
  435. }
  436. catch(Exception ex) {
  437. GUtil.Warning(this, String.Format(GApp.Strings.GetString("Message.KeyGenWizard.KeySaveError"), ex.Message));
  438. }
  439. }
  440. }
  441. private void OnSaveSECSHPublicKey(object sender, EventArgs args) {
  442. SaveFileDialog dlg = new SaveFileDialog();
  443. dlg.InitialDirectory = GApp.Options.DefaultKeyDir;
  444. dlg.Title = GApp.Strings.GetString("Caption.KeyGenWizard.SavePublicInSECSH");
  445. dlg.DefaultExt = "pub";
  446. dlg.Filter = "SSH Public Key(*.pub)|*.pub|All Files(*.*)|*.*";
  447. if(GCUtil.ShowModalDialog(this, dlg)==DialogResult.OK) {
  448. GApp.Options.DefaultKeyDir = GUtil.FileToDir(dlg.FileName);
  449. try {
  450. _resultKey.WritePublicPartInSECSHStyle(new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write), "");
  451. }
  452. catch(Exception ex) {
  453. GUtil.Warning(this, String.Format(GApp.Strings.GetString("Message.KeyGenWizard.KeySaveError"), ex.Message));
  454. }
  455. }
  456. }
  457. private void OnSaveOpenSSHPublicKey(object sender, EventArgs args) {
  458. SaveFileDialog dlg = new SaveFileDialog();
  459. dlg.InitialDirectory = GApp.Options.DefaultKeyDir;
  460. dlg.Title = GApp.Strings.GetString("Caption.KeyGenWizard.SavePublicInOpenSSH");
  461. dlg.DefaultExt = "pub";
  462. dlg.Filter = "SSH Public Key(*.pub)|*.pub|All Files(*.*)|*.*";
  463. if(GCUtil.ShowModalDialog(this, dlg)==DialogResult.OK) {
  464. GApp.Options.DefaultKeyDir = GUtil.FileToDir(dlg.FileName);
  465. try {
  466. _resultKey.WritePublicPartInOpenSSHStyle(new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write));
  467. }
  468. catch(Exception ex) {
  469. GUtil.Warning(this, String.Format(GApp.Strings.GetString("Message.KeyGenWizard.KeySaveError"), ex.Message));
  470. }
  471. }
  472. }
  473. }
  474. /*
  475.  * 偄偔偮偐帋偟偨偲偙傠丄尞嶌惉偵偄偔偮偺棎悢偑昁梫偐偼偐側傝偽傜偮偒偑偁傞丅偦偙偱師偺傛偆偵偡傞丅
  476.  * 1. MouseMove100夞傪昁偢庴怣偡傞丅
  477.  * 2. 侾夞偺MouseMove偵偮偒100屄偺棎悢傪寁嶼偡傞丅100屄徚旓偟偨傜師偺MouseMove偑棃傞傑偱僽儘僢僋丅
  478.  * 4. 搑拞偱尞嶌惉偑廔椆偟偰傕丄100屄偺MouseMove偑棃傞傑偱偼UI忋偼惗惉傪偟偰偄傞傆傝傪偡傞丅
  479.  */
  480. internal class KeyGenThread {
  481. private KeyGenWizard _parent;
  482. private PublicKeyAlgorithm _algorithm;
  483. private int _bitCount;
  484. private KeyGenRandomGenerator _rnd;
  485. private int _mouseMoveCount;
  486. public KeyGenThread(KeyGenWizard p, PublicKeyAlgorithm a, int b) {
  487. _parent = p;
  488. _algorithm = a;
  489. _bitCount = b;
  490. _rnd = new KeyGenRandomGenerator();
  491. }
  492. public void Start() {
  493. GUtil.CreateThread(new ThreadStart(EntryPoint)).Start();
  494. }
  495. public void SetAbortFlag() {
  496. _rnd.SetAbortFlag();
  497. }
  498. private void EntryPoint() {
  499. try {
  500. _mouseMoveCount = 0;
  501. KeyPair kp;
  502. if(_algorithm==PublicKeyAlgorithm.DSA)
  503. kp = Granados.PKI.DSAKeyPair.GenerateNew(_bitCount, _rnd);
  504. else
  505. kp = Granados.PKI.RSAKeyPair.GenerateNew(_bitCount, _rnd);
  506. _parent.SetResultKey(new SSH2UserAuthKey(kp));
  507. Win32.PostMessage(_parent.Handle, GConst.WMG_KEYGEN_FINISHED, IntPtr.Zero, IntPtr.Zero);
  508. }
  509. catch(Exception ex) {
  510. Debug.WriteLine(ex.StackTrace);
  511. }
  512. }
  513. //偙傟偼僼僅乕儉偺僗儗僢僪偱幚峴偝傟傞丅拲堄両
  514. public void OnMouseMove(object sender, MouseEventArgs args) {
  515. if(_mouseMoveCount==_parent.GenerationBar.Maximum) return;
  516. int n = (int)System.DateTime.Now.Ticks;
  517. n ^= (args.X << 16);
  518. n ^= args.Y;
  519. n ^= (int)0x31031293; //偙傟偖傜偄傗傟偽廫暘偽傜偗傞偩傠偆
  520. if(++_mouseMoveCount==_parent.GenerationBar.Maximum)
  521. _rnd.RefreshFinal(n);
  522. else
  523. _rnd.Refresh(n);
  524. _parent.SetProgressValue(_mouseMoveCount);
  525. }
  526. private class KeyGenRandomGenerator : Random {
  527. private Random _internal;
  528. public int _doubleCount;
  529. private int _internalAvailableCount;
  530. private bool _abortFlag;
  531. public KeyGenRandomGenerator() {
  532. _internalAvailableCount = 0;
  533. _abortFlag = false;
  534. }
  535. public override double NextDouble() {
  536. while(_internalAvailableCount==0) {
  537. Thread.Sleep(100); //摨婜僆僽僕僃僋僩傪巊偆傑偱傕側偄偩傠偆
  538. if(_abortFlag) throw new Exception("key generation aborted");
  539. }
  540. _internalAvailableCount--;
  541. _doubleCount++;
  542. return _internal.NextDouble();
  543. }
  544. //懠偼override偟側偄
  545. public void Refresh(int seed) {
  546. _internal = new Random(seed);
  547. _internalAvailableCount = 50;
  548. }
  549. public void RefreshFinal(int seed) {
  550. _internal = new Random(seed);
  551. _internalAvailableCount = Int32.MaxValue;
  552. }
  553. public void SetAbortFlag() {
  554. _abortFlag = true;
  555. }
  556. }
  557. }
  558. }