2011-02-01 6 views
0

WPF는 SecureString에 암호 입력을받는 PasswordBox를 가지고 있지만 WinForm 응용 프로그램이 있습니다. SecurePasswordTextBox을 사용할 수는 있지만, DevExpress 컨트롤과 DevExpress 모양 사용자 정의가 혼합 된 것을 원합니다. DevExpress의의 텍스트 편집기 제어에 당신은 UseSystemPasswordChar이 = true로 설정 할 수 있지만 SecureString을 사용하지 (그리고하지 않습니다 않습니다. cf. 10/22/2010, cf. alsoDevExpress에서 SecureString으로 암호를 얻는 방법 WinForms TextEdit

를 내가 쉽게 DevExpress의 윈폼 텍스트 편집기 컨트롤에 지원을 SecureString 얻을 수있는 방법

를?

은 내가 내 자신의 답변으로 아래에 게시 뭔가 해낸 사람이 다른 해결책이 있습니까

편집 :.?. 나는 내가 DevExpress의 모양을 필요로하기 때문에, 작품 내 자신의 대답을 수락있어

답변

1

여기에 내 자신의 대답이다. 가짜 문자열에 고유 한 토큰을 유지하므로 각 EditValueChanging 이벤트에서 SecureString에서 무엇을 변경해야하는지 파악할 수 있습니다. 나는 DevExpress의 외관 동작을 잃고 있지만

public partial class PasswordForm : DevExpress.XtraEditors.XtraForm 
{ 
    private Random random = new Random(); 
    private HashSet<char> pool = new HashSet<char>(); 
    private char secret; 
    private char token; 
    private List<char> fake = new List<char>(); 

    public PasswordForm() 
    { 
     InitializeComponent(); 
     this.Password = new SecureString(); 
     for (int i = 0; i < 128; i++) 
     { 
      this.pool.Add((char)(' ' + i)); 
     } 
    } 

    public SecureString Password { get; private set; } 

    private void textEditPassword_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) 
    { 
     string value = e.NewValue as string; 

     // If any characters have been deleted... 
     foreach (char c in this.fake.ToArray()) 
     { 
      if (value.IndexOf(c) == -1) 
      { 
       this.Password.RemoveAt(this.fake.IndexOf(c)); 
       this.fake.Remove(c); 
       this.pool.Add(c); 
      } 
     } 

     // If a character is being added... 
     if (this.token != '\0') 
     { 
      int i = value.IndexOf(this.token); 
      this.Password.InsertAt(i, this.secret); 
      this.secret = '\0'; 
      fake.Insert(i, this.token); 
     } 
    } 

    private void textEditPassword_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (Char.IsControl(e.KeyChar)) 
     { 
      this.token = '\0'; 
     } 
     else 
     { 
      this.token = this.pool.ElementAt(random.Next(this.pool.Count)); // throws ArgumentOutOfRangeException when pool is empty 
      this.pool.Remove(this.token); 
      this.secret = e.KeyChar; 
      e.KeyChar = this.token; 
     } 
    } 
} 
1

가장 간단한 방법은 WPF 암호 상자를 사용하는 것입니다. ElementHost.

당신은 도구 상자에서 ElementHost 컨트롤을 드래그하거나 코드에서 모든 일을 수행 할 수 있습니다 물론

public Form1() 
{ 
    InitializeComponent(); 
    System.Windows.Forms.Integration.ElementHost host = new System.Windows.Forms.Integration.ElementHost(); 
    System.Windows.Controls.PasswordBox pb=new System.Windows.Controls.PasswordBox(); 
    host.Child = pb; 
    this.Controls.Add(host); 
} 

이것을 DevExpress의 컨트롤을 사용하지 않는,하지만 난을 사용하는 어떤 이유를 볼 수 없습니다 단순한 대안이있는 경우 서식있는 텍스트 편집기를 암호 상자로 사용하십시오.

가 대신 너무 복잡 보인다 서브 클래 싱 텍스트 편집기,의, 난 그냥 키 누르기 이벤트를 잡을 멀리 EditValueChanging 이벤트 때까지 각 눌러 문자를 숨기기 :

+0

이 매우 쉽습니다 : 텍스트 편집기 제어 및 두 개의 이벤트 핸들러 간단한 양식 - 여기

은 개념 증명입니다. 나는 그것을 시험해보고 그것이 어떻게 보이는지 볼 것이다. 나는 DevExpress.XtraEditors.TextEdit을 사용하고 있는데, 이것은 간단한 텍스트 편집기가 아닌 단 한 줄의 편집기이지만, out-of-the-box SecureString 기능을 갖는 것이 더 좋을 것이다. –

+0

죄송합니다. DevExpress 스타일을 계속 사용하고 싶지 않았습니다. –

관련 문제