{"id":122,"date":"2016-07-15T09:52:34","date_gmt":"2016-07-15T07:52:34","guid":{"rendered":"http:\/\/www.summonte.com\/home\/?p=122"},"modified":"2016-07-21T09:04:52","modified_gmt":"2016-07-21T07:04:52","slug":"c-passwordtextbox","status":"publish","type":"post","link":"https:\/\/www.summonte.com\/home\/2016_07_15-c-passwordtextbox\/","title":{"rendered":"C# Password TextBox"},"content":{"rendered":"<p>Ciao,<\/p>\n<p>di seguito il codice C# per l&#8217;estensione del componente TextBox al fine di mitigare il problema dei &#8220;keyLogger&#8221;.<\/p>\n<p>Il codice \u00e8 funzionante anche se sicuramente prototipale.<\/p>\n<p>Spero possa esservi utile.<\/p>\n<p>(Codice presso GitHub: <a href=\"https:\/\/github.com\/waltzie\/CSharp-PasswordTextBox\" target=\"_blank\">https:\/\/github.com\/waltzie\/CSharp-PasswordTextBox<\/a>)<\/p>\n<p>using System;<br \/>\nusing System.ComponentModel;<br \/>\nusing System.Linq;<br \/>\nusing System.Windows.Forms;<br \/>\nnamespace WaltZie.Windows.Forms<br \/>\n{<\/p>\n<p>\/\/\/<br \/>\n\/\/\/ Estensione della casella TextBox per gestire le password in modo che non possano essere intercettate dai keylkoggers<br \/>\n\/\/\/<\/p>\n<p>\/\/ Problema con i caratteri [] gestiti da sendkey<br \/>\n\/\/ Problema con digitazione caratteri in contemporanea. Occorre reindirizzare OnKeyPress a thread sequenziali.<\/p>\n<p>public class PasswordTextBox : System.Windows.Forms.TextBox<br \/>\n{<br \/>\npublic static PasswordTextBox Instance = new PasswordTextBox();<\/p>\n<p>private string junk = &#8220;&#8221;;<br \/>\nprivate uint _junkMax = 4;<br \/>\nprivate uint _junkMin = 2;<br \/>\nprivate byte _minchar = 32;<br \/>\nprivate byte _maxchar = 126;<br \/>\nprivate string _allowedchars=&#8221;&#8221;;<br \/>\nprivate string _scramble;<br \/>\nprivate bool _obfuscate = false;<\/p>\n<p>private static readonly Random random = new Random();<br \/>\nprivate static readonly object syncLock = new object();<br \/>\nprivate delegate string del(string s);<\/p>\n<p>public PasswordTextBox() {<br \/>\n}<\/p>\n<p>protected override void OnMouseDown(MouseEventArgs e)<br \/>\n{<br \/>\nif (String.IsNullOrEmpty(this.Text))<br \/>\n{<br \/>\nsendJunkChars();<br \/>\n}<br \/>\nbase.OnMouseDown(e);<br \/>\n}<\/p>\n<p>\/\/\/<br \/>\n\/\/\/<br \/>\nThe AllowedChars represents characters allowed.<\/p>\n<p>\/\/\/ The AllowedChars gets\/sets the value of the string field, _allowedchars.<br \/>\n\/\/\/<br \/>\n[Category(&#8220;Extensions&#8221;), Browsable(true), Description(&#8220;String containig allowed chars&#8221;)]<br \/>\npublic string AllowedChars<br \/>\n{<br \/>\nset<br \/>\n{<br \/>\nif ((value.Length) &lt; 256) { this._allowedchars = value; this._scramble = new string(_allowedchars.ToCharArray().OrderBy(x =&gt; Guid.NewGuid()).ToArray());<br \/>\n}<br \/>\n}<br \/>\nget<br \/>\n{<br \/>\nreturn this._allowedchars;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/\/<br \/>\n\/\/\/<br \/>\nThe Obfuscate define if typed text must be obfuscate with shuffled allowd chars.<\/p>\n<p>\/\/\/ The Obfuscate gets\/sets the value of the bool field, _obfuscate.<br \/>\n\/\/\/<br \/>\n[Category(&#8220;Extensions&#8221;), Browsable(true), Description(&#8220;Obfuscate input data&#8221;)]<br \/>\npublic bool Obfuscate<br \/>\n{<br \/>\nset<br \/>\n{<br \/>\nthis._obfuscate = value;<br \/>\nif (this._obfuscate &amp;&amp; _allowedchars == &#8220;&#8221;) this.AllowedChars = &#8221; !\\&#8221;#$%&amp;'()*+-.\/0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~&#8221;;<br \/>\n}<br \/>\nget<br \/>\n{<br \/>\nreturn this._obfuscate;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/\/<br \/>\n\/\/\/<br \/>\nThe ClearText represents real typed test.<\/p>\n<p>\/\/\/ The ClearText gets the value of the de-obfuscated string field, Text.<br \/>\n\/\/\/ Returns original de-obfuscated typed text.<br \/>\n\/\/\/<br \/>\n[Category(&#8220;Extensions&#8221;), Browsable(true), Description(&#8220;Clear text&#8221;)]<br \/>\npublic string ClearText<br \/>\n{<br \/>\nget<br \/>\n{<br \/>\nreturn deobfuscate(this.Text);<br \/>\n}<br \/>\n}<\/p>\n<p>private bool IsAllowedChar(char c)<br \/>\n{<br \/>\nif (char.IsControl(c)) return false;<br \/>\nif (this._allowedchars.Length &gt; 0)<br \/>\n{<br \/>\nif (this._allowedchars.Contains(c.ToString())) return true;<br \/>\nelse return false;<br \/>\n}<br \/>\nelse return true;<br \/>\n}<\/p>\n<p>private string Randomchar()<br \/>\n{<br \/>\nreturn Convert.ToChar(random.Next(this._minchar, this._maxchar)).ToString();<br \/>\n}<\/p>\n<p>private string junkGenerator()<br \/>\n{<br \/>\nstring randomString = &#8220;&#8221;;<br \/>\nfor (int i = 0; i &lt; random.Next((int)this._junkMin, (int)this._junkMax); i++)<br \/>\nrandomString += Randomchar();<br \/>\nreturn randomString;<br \/>\n}<br \/>\nprivate void sendJunkChars()<br \/>\n{<br \/>\nstring outchar = &#8220;&#8221;;<br \/>\nlock (syncLock)<br \/>\n{<br \/>\nthis.junk += junkGenerator();<br \/>\nfor (int i = 0; i &lt; this.junk.Length; i++)<br \/>\nif (&#8220;+^%~(){}&#8221;.Contains(this.junk[i])) outchar += &#8220;{&#8221; + this.junk[i] + &#8220;}&#8221;;<br \/>\nelse outchar += this.junk[i].ToString();<br \/>\nthis.Focus();<br \/>\nSendKeys.Send(outchar);<br \/>\n}<br \/>\n}<\/p>\n<p>private char obfuscate(char s) {<br \/>\nlock (syncLock)<br \/>\n{<br \/>\nif (this._obfuscate) return this._scramble[this._allowedchars.IndexOf(s)];<br \/>\nelse return s;<br \/>\n}<br \/>\n}<br \/>\nprivate string deobfuscate(string s) {<br \/>\nif (!this._obfuscate) return s;<br \/>\nstring outstr = &#8220;&#8221;;<br \/>\nfor (int i=0; i&lt;this.Text.Length;i++) { outstr += this._allowedchars[this._scramble.IndexOf(this.Text[i])].ToString(); } return outstr; } protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (this.junk.Length == 0) { if (keyData == Keys.Delete) OnKeyPress(new KeyPressEventArgs((Char)Keys.Back)); } return base.ProcessCmdKey(ref msg, keyData); } protected override void OnKeyPress(KeyPressEventArgs e) { \/\/ Controllare this.SelectedText base.OnKeyPress(e); string keyInput = e.KeyChar.ToString(); if (this.junk.Length &gt; 0)<br \/>\n{<br \/>\n\/\/this char is junk<br \/>\ne.Handled = true;<br \/>\nlock(syncLock) this.junk = this.junk.Remove(this.junk.Length &#8211; 1);<br \/>\n}<br \/>\nelse if (IsAllowedChar(e.KeyChar))<br \/>\n{<br \/>\ne.KeyChar = obfuscate(e.KeyChar);<br \/>\nsendJunkChars();<br \/>\n}<br \/>\nelse if (e.KeyChar == &#8216;\\b&#8217;)<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\n\/\/ Backspace key is OK<br \/>\nsendJunkChars();<br \/>\n}<br \/>\ncatch { e.Handled = true; }<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ Consume this invalid key and beep<br \/>\ne.Handled = true;<br \/>\nSystem.Media.SystemSounds.Exclamation.Play();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ciao, di seguito il codice C# per l&#8217;estensione del componente TextBox al fine di mitigare il problema dei &#8220;keyLogger&#8221;. Il codice \u00e8 funzionante anche se sicuramente prototipale. Spero possa esservi utile. (Codice presso GitHub: https:\/\/github.com\/waltzie\/CSharp-PasswordTextBox) using System; using System.ComponentModel; using System.Linq; using System.Windows.Forms; namespace WaltZie.Windows.Forms { \/\/\/ \/\/\/ Estensione della casella TextBox per gestire le &hellip; <a href=\"https:\/\/www.summonte.com\/home\/2016_07_15-c-passwordtextbox\/\" class=\"more-link\">Continua la lettura di <span class=\"screen-reader-text\">C# Password TextBox<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[16,10,14,15],"class_list":["post-122","post","type-post","status-publish","format-standard","hentry","category-programmazione","tag-net","tag-c","tag-code","tag-programming"],"_links":{"self":[{"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":15,"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/posts\/122\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.summonte.com\/home\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}