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

Telnet服务器

开发平台:

C#

  1. /*
  2. * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3. * $Id: ChangePassphrase.cs,v 1.2 2005/04/20 08:45:44 okajima Exp $
  4. */
  5. using System;
  6. using System.Drawing;
  7. using System.Collections;
  8. using System.ComponentModel;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using Granados.SSHCV2;
  12. namespace Poderosa.Forms
  13. {
  14. /// <summary>
  15. /// ChangePassphrase 偺奣梫偺愢柧偱偡丅
  16. /// </summary>
  17. internal class ChangePassphrase : System.Windows.Forms.Form
  18. {
  19. private System.Windows.Forms.Label _lKeyFile;
  20. private TextBox _tKeyFile;
  21. private System.Windows.Forms.Button _selectKeyFile;
  22. private System.Windows.Forms.Label _lCurrentPassphrase;
  23. private TextBox _tCurrentPassphrase;
  24. private System.Windows.Forms.Label _lNewPassphrase;
  25. private TextBox _tNewPassphrase;
  26. private System.Windows.Forms.Label _lNewPassphraseAgain;
  27. private TextBox _tNewPassphraseAgain;
  28. private System.Windows.Forms.Button _okButton;
  29. private System.Windows.Forms.Button _cancelButton;
  30. /// <summary>
  31. /// 昁梫側僨僓僀僫曄悢偱偡丅
  32. /// </summary>
  33. private System.ComponentModel.Container components = null;
  34. public ChangePassphrase()
  35. {
  36. //
  37. // Windows 僼僅乕儉 僨僓僀僫 僒億乕僩偵昁梫偱偡丅
  38. //
  39. InitializeComponent();
  40. //
  41. // TODO: InitializeComponent 屇傃弌偟偺屻偵丄僐儞僗僩儔僋僞 僐乕僪傪捛壛偟偰偔偩偝偄丅
  42. //
  43. this._lKeyFile.Text = GApp.Strings.GetString("Form.ChangePassphrase._lKeyFile");
  44. this._lCurrentPassphrase.Text = GApp.Strings.GetString("Form.ChangePassphrase._lCurrentPassphrase");
  45. this._lNewPassphrase.Text = GApp.Strings.GetString("Form.ChangePassphrase._lNewPassphrase");
  46. this._lNewPassphraseAgain.Text = GApp.Strings.GetString("Form.ChangePassphrase._lNewPassphraseAgain");
  47. this._okButton.Text = GApp.Strings.GetString("Common.OK");
  48. this._cancelButton.Text = GApp.Strings.GetString("Common.Cancel");
  49. this.Text = GApp.Strings.GetString("Form.ChangePassphrase.Text");
  50. }
  51. /// <summary>
  52. /// 巊梡偝傟偰偄傞儕僜乕僗偵屻張棟傪幚峴偟傑偡丅
  53. /// </summary>
  54. protected override void Dispose( bool disposing )
  55. {
  56. if( disposing )
  57. {
  58. if(components != null)
  59. {
  60. components.Dispose();
  61. }
  62. }
  63. base.Dispose( disposing );
  64. }
  65. #region Windows Form Designer generated code
  66. /// <summary>
  67. /// 僨僓僀僫 僒億乕僩偵昁梫側儊僜僢僪偱偡丅偙偺儊僜僢僪偺撪梕傪
  68. /// 僐乕僪 僄僨傿僞偱曄峏偟側偄偱偔偩偝偄丅
  69. /// </summary>
  70. private void InitializeComponent()
  71. {
  72. this._lKeyFile = new System.Windows.Forms.Label();
  73. this._tKeyFile = new TextBox();
  74. this._selectKeyFile = new System.Windows.Forms.Button();
  75. this._tCurrentPassphrase = new TextBox();
  76. this._lCurrentPassphrase = new System.Windows.Forms.Label();
  77. this._tNewPassphrase = new TextBox();
  78. this._lNewPassphrase = new System.Windows.Forms.Label();
  79. this._tNewPassphraseAgain = new TextBox();
  80. this._lNewPassphraseAgain = new System.Windows.Forms.Label();
  81. this._okButton = new System.Windows.Forms.Button();
  82. this._cancelButton = new System.Windows.Forms.Button();
  83. this.SuspendLayout();
  84. // 
  85. // _lKeyFile
  86. // 
  87. this._lKeyFile.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
  88. this._lKeyFile.Location = new System.Drawing.Point(8, 8);
  89. this._lKeyFile.Name = "_lKeyFile";
  90. this._lKeyFile.Size = new System.Drawing.Size(112, 16);
  91. this._lKeyFile.TabIndex = 0;
  92. // 
  93. // _tKeyFile
  94. // 
  95. this._tKeyFile.Location = new System.Drawing.Point(136, 8);
  96. this._tKeyFile.Name = "_tKeyFile";
  97. this._tKeyFile.Size = new System.Drawing.Size(120, 19);
  98. this._tKeyFile.TabIndex = 1;
  99. // 
  100. // _selectKeyFile
  101. // 
  102. this._selectKeyFile.Location = new System.Drawing.Point(264, 8);
  103. this._selectKeyFile.Name = "_selectKeyFile";
  104. this._selectKeyFile.FlatStyle = FlatStyle.System;
  105. this._selectKeyFile.Size = new System.Drawing.Size(19, 19);
  106. this._selectKeyFile.TabIndex = 2;
  107. this._selectKeyFile.Text = "...";
  108. this._selectKeyFile.Click += new EventHandler(OpenKeyFile);
  109. // 
  110. // _lCurrentPassphrase
  111. // 
  112. this._lCurrentPassphrase.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
  113. this._lCurrentPassphrase.Location = new System.Drawing.Point(8, 32);
  114. this._lCurrentPassphrase.Name = "_lCurrentPassphrase";
  115. this._lCurrentPassphrase.Size = new System.Drawing.Size(120, 16);
  116. this._lCurrentPassphrase.TabIndex = 3;
  117. // 
  118. // _tCurrentPassphrase
  119. // 
  120. this._tCurrentPassphrase.Location = new System.Drawing.Point(136, 32);
  121. this._tCurrentPassphrase.Name = "_tCurrentPassphrase";
  122. this._tCurrentPassphrase.PasswordChar = '*';
  123. this._tCurrentPassphrase.Size = new System.Drawing.Size(152, 19);
  124. this._tCurrentPassphrase.TabIndex = 4;
  125. // 
  126. // _lNewPassphrase
  127. // 
  128. this._lNewPassphrase.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
  129. this._lNewPassphrase.Location = new System.Drawing.Point(8, 56);
  130. this._lNewPassphrase.Name = "_lNewPassphrase";
  131. this._lNewPassphrase.Size = new System.Drawing.Size(120, 16);
  132. this._lNewPassphrase.TabIndex = 5;
  133. // 
  134. // _tNewPassphrase
  135. // 
  136. this._tNewPassphrase.Location = new System.Drawing.Point(136, 56);
  137. this._tNewPassphrase.Name = "_tNewPassphrase";
  138. this._tNewPassphrase.PasswordChar = '*';
  139. this._tNewPassphrase.Size = new System.Drawing.Size(152, 19);
  140. this._tNewPassphrase.TabIndex = 6;
  141. // 
  142. // _lNewPassphraseAgain
  143. // 
  144. this._lNewPassphraseAgain.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
  145. this._lNewPassphraseAgain.Location = new System.Drawing.Point(8, 80);
  146. this._lNewPassphraseAgain.Name = "_lNewPassphraseAgain";
  147. this._lNewPassphraseAgain.Size = new System.Drawing.Size(120, 16);
  148. this._lNewPassphraseAgain.TabIndex = 7;
  149. // 
  150. // _tNewPassphraseAgain
  151. // 
  152. this._tNewPassphraseAgain.Location = new System.Drawing.Point(136, 80);
  153. this._tNewPassphraseAgain.Name = "_tNewPassphraseAgain";
  154. this._tNewPassphraseAgain.PasswordChar = '*';
  155. this._tNewPassphraseAgain.Size = new System.Drawing.Size(152, 19);
  156. this._tNewPassphraseAgain.TabIndex = 8;
  157. // 
  158. // _okButton
  159. // 
  160. this._okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
  161. this._okButton.Location = new System.Drawing.Point(152, 112);
  162. this._okButton.Name = "_okButton";
  163. this._okButton.FlatStyle = FlatStyle.System;
  164. this._okButton.Size = new System.Drawing.Size(64, 24);
  165. this._okButton.TabIndex = 9;
  166. this._okButton.Click += new EventHandler(OnOK);
  167. // 
  168. // _cancelButton
  169. // 
  170. this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  171. this._cancelButton.Location = new System.Drawing.Point(224, 112);
  172. this._cancelButton.Name = "_cancelButton";
  173. this._cancelButton.FlatStyle = FlatStyle.System;
  174. this._cancelButton.Size = new System.Drawing.Size(64, 23);
  175. this._cancelButton.TabIndex = 10;
  176. // 
  177. // ChangePassphrase
  178. // 
  179. this.AcceptButton = this._okButton;
  180. this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
  181. this.CancelButton = this._cancelButton;
  182. this.ClientSize = new System.Drawing.Size(292, 141);
  183. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  184.   this._cancelButton,
  185.   this._okButton,
  186.   this._tNewPassphraseAgain,
  187.   this._lNewPassphraseAgain,
  188.   this._tNewPassphrase,
  189.   this._lNewPassphrase,
  190.   this._tCurrentPassphrase,
  191.   this._lCurrentPassphrase,
  192.   this._selectKeyFile,
  193.   this._tKeyFile,
  194.   this._lKeyFile});
  195. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  196. this.MaximizeBox = false;
  197. this.MinimizeBox = false;
  198. this.Name = "ChangePassphrase";
  199. this.ShowInTaskbar = false;
  200. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  201. this.ResumeLayout(false);
  202. }
  203. #endregion
  204. private void OpenKeyFile(object sender, EventArgs args) {
  205. string fn = GCUtil.SelectPrivateKeyFileByDialog(this);
  206. if(fn!=null) _tKeyFile.Text = fn;
  207. }
  208. private void OnOK(object sender, EventArgs args) {
  209. this.DialogResult = DialogResult.None;
  210. try {
  211. SSH2UserAuthKey key = SSH2UserAuthKey.FromSECSHStyleFile(_tKeyFile.Text, _tCurrentPassphrase.Text);
  212. if(_tNewPassphrase.Text!=_tNewPassphraseAgain.Text)
  213. GUtil.Warning(this, GApp.Strings.GetString("Message.ChangePassphrase.PassphraseMismatch"));
  214. else {
  215. if(_tNewPassphrase.Text.Length>0 || GUtil.AskUserYesNo(this, GApp.Strings.GetString("Message.ChangePassphrase.AskEmptyPassphrase"))==DialogResult.Yes) {
  216. FileStream s = new FileStream(_tKeyFile.Text, FileMode.Create);
  217. key.WritePrivatePartInSECSHStyleFile(s, "", _tNewPassphrase.Text);
  218. s.Close();
  219. GUtil.Warning(this, GApp.Strings.GetString("Message.ChangePassphrase.NotifyChanged"), MessageBoxIcon.Information);
  220. this.DialogResult = DialogResult.OK;
  221. this.Close();
  222. }
  223. }
  224. }
  225. catch(Exception ex) {
  226. GUtil.Warning(this, ex.Message);
  227. }
  228. }
  229. }
  230. }