# HG changeset patch # User StephaneLenclud # Date 1473091881 -7200 # Node ID 31d4905ad28ae65e39967b12ef63bc0cfffd7a2c # Parent a601b377a3ebe66b0164cd3f5b0ad705d2b6aa02 Now storing encrypted version of Logitech password. diff -r a601b377a3eb -r 31d4905ad28a Server/App.config --- a/Server/App.config Fri Sep 02 01:38:08 2016 +0200 +++ b/Server/App.config Mon Sep 05 18:11:21 2016 +0200 @@ -46,6 +46,9 @@ + + + diff -r a601b377a3eb -r 31d4905ad28a Server/FormMain.Designer.cs --- a/Server/FormMain.Designer.cs Fri Sep 02 01:38:08 2016 +0200 +++ b/Server/FormMain.Designer.cs Mon Sep 05 18:11:21 2016 +0200 @@ -991,6 +991,7 @@ this.iTextBoxLogitechPassword.PasswordChar = '*'; this.iTextBoxLogitechPassword.Size = new System.Drawing.Size(134, 20); this.iTextBoxLogitechPassword.TabIndex = 12; + this.iTextBoxLogitechPassword.TextChanged += new System.EventHandler(this.iTextBoxLogitechPassword_TextChanged); // // labelLogitechUserName // diff -r a601b377a3eb -r 31d4905ad28a Server/FormMain.cs --- a/Server/FormMain.cs Fri Sep 02 01:38:08 2016 +0200 +++ b/Server/FormMain.cs Mon Sep 05 18:11:21 2016 +0200 @@ -38,6 +38,7 @@ using NAudio.CoreAudioApi; using NAudio.CoreAudioApi.Interfaces; using System.Runtime.InteropServices; +using System.Security; using CecSharp; //Network using NETWORKLIST; @@ -1240,8 +1241,9 @@ checkBoxAutoStart.Checked = iStartupManager.Startup; //Harmony settings - iButtonHarmonyConnect.Enabled = Properties.Settings.Default.HarmonyEnabled; - + //Decrypt our password first + iTextBoxLogitechPassword.Text = Secure.ToInsecureString(Secure.DecryptString(Properties.Settings.Default.LogitechPassword)); + //CEC settings comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1; @@ -3071,6 +3073,9 @@ await Program.HarmonyClient.CloseAsync(); } + + SecureString password; + //Reset Harmony client & config Program.HarmonyClient = null; Program.HarmonyConfig = null; @@ -3161,5 +3166,11 @@ } } + private void iTextBoxLogitechPassword_TextChanged(object sender, EventArgs e) + { + //Save our password after encryption + Properties.Settings.Default.LogitechPassword = Secure.EncryptString(Secure.ToSecureString(iTextBoxLogitechPassword.Text)); + Properties.Settings.Default.Save(); + } } } diff -r a601b377a3eb -r 31d4905ad28a Server/Properties/Settings.Designer.cs --- a/Server/Properties/Settings.Designer.cs Fri Sep 02 01:38:08 2016 +0200 +++ b/Server/Properties/Settings.Designer.cs Mon Sep 05 18:11:21 2016 +0200 @@ -188,5 +188,17 @@ this["LogitechAuthToken"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string LogitechPassword { + get { + return ((string)(this["LogitechPassword"])); + } + set { + this["LogitechPassword"] = value; + } + } } } diff -r a601b377a3eb -r 31d4905ad28a Server/Properties/Settings.settings --- a/Server/Properties/Settings.settings Fri Sep 02 01:38:08 2016 +0200 +++ b/Server/Properties/Settings.settings Mon Sep 05 18:11:21 2016 +0200 @@ -44,5 +44,8 @@ + + + \ No newline at end of file diff -r a601b377a3eb -r 31d4905ad28a Server/Secure.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Server/Secure.cs Mon Sep 05 18:11:21 2016 +0200 @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security; +using System.Text; +using System.Threading.Tasks; + +namespace SharpDisplayManager +{ + /// + /// As per: http://weblogs.asp.net/jongalloway/encrypting-passwords-in-a-net-app-config-file + /// + class Secure + { + static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("kd566slqfjls234895DFG743fqdlskj345SDFDepozwxc,n*ù^$^é"); + + public static string EncryptString(System.Security.SecureString input) + { + byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect( + System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)), + entropy, + System.Security.Cryptography.DataProtectionScope.CurrentUser); + return Convert.ToBase64String(encryptedData); + } + + public static SecureString DecryptString(string encryptedData) + { + try + { + byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect( + Convert.FromBase64String(encryptedData), + entropy, + System.Security.Cryptography.DataProtectionScope.CurrentUser); + return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData)); + } + catch + { + return new SecureString(); + } + } + + public static SecureString ToSecureString(string input) + { + SecureString secure = new SecureString(); + foreach (char c in input) + { + secure.AppendChar(c); + } + secure.MakeReadOnly(); + return secure; + } + + public static string ToInsecureString(SecureString input) + { + string returnValue = string.Empty; + IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input); + try + { + returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr); + } + finally + { + System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr); + } + return returnValue; + } + } +} diff -r a601b377a3eb -r 31d4905ad28a Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Fri Sep 02 01:38:08 2016 +0200 +++ b/Server/SharpDisplayManager.csproj Mon Sep 05 18:11:21 2016 +0200 @@ -179,6 +179,7 @@ + Form