Now storing encrypted version of Logitech password.
authorStephaneLenclud
Mon, 05 Sep 2016 18:11:21 +0200
changeset 26831d4905ad28a
parent 267 a601b377a3eb
child 269 8e32e51a2edd
Now storing encrypted version of Logitech password.
Server/App.config
Server/FormMain.Designer.cs
Server/FormMain.cs
Server/Properties/Settings.Designer.cs
Server/Properties/Settings.settings
Server/Secure.cs
Server/SharpDisplayManager.csproj
     1.1 --- a/Server/App.config	Fri Sep 02 01:38:08 2016 +0200
     1.2 +++ b/Server/App.config	Mon Sep 05 18:11:21 2016 +0200
     1.3 @@ -46,6 +46,9 @@
     1.4              <setting name="LogitechAuthToken" serializeAs="String">
     1.5                  <value />
     1.6              </setting>
     1.7 +            <setting name="LogitechPassword" serializeAs="String">
     1.8 +                <value />
     1.9 +            </setting>
    1.10          </SharpDisplayManager.Properties.Settings>
    1.11      </userSettings>
    1.12  </configuration>
     2.1 --- a/Server/FormMain.Designer.cs	Fri Sep 02 01:38:08 2016 +0200
     2.2 +++ b/Server/FormMain.Designer.cs	Mon Sep 05 18:11:21 2016 +0200
     2.3 @@ -991,6 +991,7 @@
     2.4              this.iTextBoxLogitechPassword.PasswordChar = '*';
     2.5              this.iTextBoxLogitechPassword.Size = new System.Drawing.Size(134, 20);
     2.6              this.iTextBoxLogitechPassword.TabIndex = 12;
     2.7 +            this.iTextBoxLogitechPassword.TextChanged += new System.EventHandler(this.iTextBoxLogitechPassword_TextChanged);
     2.8              // 
     2.9              // labelLogitechUserName
    2.10              // 
     3.1 --- a/Server/FormMain.cs	Fri Sep 02 01:38:08 2016 +0200
     3.2 +++ b/Server/FormMain.cs	Mon Sep 05 18:11:21 2016 +0200
     3.3 @@ -38,6 +38,7 @@
     3.4  using NAudio.CoreAudioApi;
     3.5  using NAudio.CoreAudioApi.Interfaces;
     3.6  using System.Runtime.InteropServices;
     3.7 +using System.Security;
     3.8  using CecSharp;
     3.9  //Network
    3.10  using NETWORKLIST;
    3.11 @@ -1240,8 +1241,9 @@
    3.12              checkBoxAutoStart.Checked = iStartupManager.Startup;
    3.13  
    3.14              //Harmony settings
    3.15 -            iButtonHarmonyConnect.Enabled = Properties.Settings.Default.HarmonyEnabled;
    3.16 -
    3.17 +            //Decrypt our password first
    3.18 +            iTextBoxLogitechPassword.Text = Secure.ToInsecureString(Secure.DecryptString(Properties.Settings.Default.LogitechPassword));
    3.19 +            
    3.20              //CEC settings
    3.21              comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
    3.22  
    3.23 @@ -3071,6 +3073,9 @@
    3.24                  await Program.HarmonyClient.CloseAsync();
    3.25              }
    3.26  
    3.27 +
    3.28 +            SecureString password;
    3.29 +
    3.30              //Reset Harmony client & config
    3.31              Program.HarmonyClient = null;
    3.32              Program.HarmonyConfig = null;
    3.33 @@ -3161,5 +3166,11 @@
    3.34              }
    3.35          }
    3.36  
    3.37 +        private void iTextBoxLogitechPassword_TextChanged(object sender, EventArgs e)
    3.38 +        {
    3.39 +            //Save our password after encryption
    3.40 +            Properties.Settings.Default.LogitechPassword = Secure.EncryptString(Secure.ToSecureString(iTextBoxLogitechPassword.Text));
    3.41 +            Properties.Settings.Default.Save();
    3.42 +        }
    3.43      }
    3.44  }
     4.1 --- a/Server/Properties/Settings.Designer.cs	Fri Sep 02 01:38:08 2016 +0200
     4.2 +++ b/Server/Properties/Settings.Designer.cs	Mon Sep 05 18:11:21 2016 +0200
     4.3 @@ -188,5 +188,17 @@
     4.4                  this["LogitechAuthToken"] = value;
     4.5              }
     4.6          }
     4.7 +        
     4.8 +        [global::System.Configuration.UserScopedSettingAttribute()]
     4.9 +        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    4.10 +        [global::System.Configuration.DefaultSettingValueAttribute("")]
    4.11 +        public string LogitechPassword {
    4.12 +            get {
    4.13 +                return ((string)(this["LogitechPassword"]));
    4.14 +            }
    4.15 +            set {
    4.16 +                this["LogitechPassword"] = value;
    4.17 +            }
    4.18 +        }
    4.19      }
    4.20  }
     5.1 --- a/Server/Properties/Settings.settings	Fri Sep 02 01:38:08 2016 +0200
     5.2 +++ b/Server/Properties/Settings.settings	Mon Sep 05 18:11:21 2016 +0200
     5.3 @@ -44,5 +44,8 @@
     5.4      <Setting Name="LogitechAuthToken" Type="System.String" Scope="User">
     5.5        <Value Profile="(Default)" />
     5.6      </Setting>
     5.7 +    <Setting Name="LogitechPassword" Type="System.String" Scope="User">
     5.8 +      <Value Profile="(Default)" />
     5.9 +    </Setting>
    5.10    </Settings>
    5.11  </SettingsFile>
    5.12 \ No newline at end of file
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/Server/Secure.cs	Mon Sep 05 18:11:21 2016 +0200
     6.3 @@ -0,0 +1,68 @@
     6.4 +using System;
     6.5 +using System.Collections.Generic;
     6.6 +using System.Linq;
     6.7 +using System.Security;
     6.8 +using System.Text;
     6.9 +using System.Threading.Tasks;
    6.10 +
    6.11 +namespace SharpDisplayManager
    6.12 +{
    6.13 +    /// <summary>
    6.14 +    /// As per: http://weblogs.asp.net/jongalloway/encrypting-passwords-in-a-net-app-config-file
    6.15 +    /// </summary>
    6.16 +    class Secure
    6.17 +    {
    6.18 +        static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("kd566slqfjls234895DFG743fqdlskj345SDFDepozwxc,n*ù^$^é");
    6.19 +
    6.20 +        public static string EncryptString(System.Security.SecureString input)
    6.21 +        {
    6.22 +            byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
    6.23 +                System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
    6.24 +                entropy,
    6.25 +                System.Security.Cryptography.DataProtectionScope.CurrentUser);
    6.26 +            return Convert.ToBase64String(encryptedData);
    6.27 +        }
    6.28 +
    6.29 +        public static SecureString DecryptString(string encryptedData)
    6.30 +        {
    6.31 +            try
    6.32 +            {
    6.33 +                byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
    6.34 +                    Convert.FromBase64String(encryptedData),
    6.35 +                    entropy,
    6.36 +                    System.Security.Cryptography.DataProtectionScope.CurrentUser);
    6.37 +                return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
    6.38 +            }
    6.39 +            catch
    6.40 +            {
    6.41 +                return new SecureString();
    6.42 +            }
    6.43 +        }
    6.44 +
    6.45 +        public static SecureString ToSecureString(string input)
    6.46 +        {
    6.47 +            SecureString secure = new SecureString();
    6.48 +            foreach (char c in input)
    6.49 +            {
    6.50 +                secure.AppendChar(c);
    6.51 +            }
    6.52 +            secure.MakeReadOnly();
    6.53 +            return secure;
    6.54 +        }
    6.55 +
    6.56 +        public static string ToInsecureString(SecureString input)
    6.57 +        {
    6.58 +            string returnValue = string.Empty;
    6.59 +            IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
    6.60 +            try
    6.61 +            {
    6.62 +                returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
    6.63 +            }
    6.64 +            finally
    6.65 +            {
    6.66 +                System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
    6.67 +            }
    6.68 +            return returnValue;
    6.69 +        }
    6.70 +    }
    6.71 +}
     7.1 --- a/Server/SharpDisplayManager.csproj	Fri Sep 02 01:38:08 2016 +0200
     7.2 +++ b/Server/SharpDisplayManager.csproj	Mon Sep 05 18:11:21 2016 +0200
     7.3 @@ -179,6 +179,7 @@
     7.4      <Compile Include="ConsumerElectronicControl.cs" />
     7.5      <Compile Include="ClientData.cs" />
     7.6      <Compile Include="EarManager.cs" />
     7.7 +    <Compile Include="Secure.cs" />
     7.8      <Compile Include="Events\EventHid.cs" />
     7.9      <Compile Include="FormEditObject.cs">
    7.10        <SubType>Form</SubType>