Commits

Christian Specht committed 22a53ba

encrypt password in settings file

  • Participants
  • Parent commits 9cf2e9b

Comments (0)

Files changed (3)

src/BitbucketBackup/BitbucketBackup.csproj

     <Reference Include="System" />
     <Reference Include="System.configuration" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Security" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="Microsoft.CSharp" />
       <DesignTime>True</DesignTime>
       <DependentUpon>Resources.resx</DependentUpon>
     </Compile>
+    <Compile Include="StringEncryptor.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />

src/BitbucketBackup/Config.cs

         {
             get
             {
-                return Settings.Default.PassWord;
+                return StringEncryptor.Decrypt(Settings.Default.PassWord);
             }
             private set
             {
-                Settings.Default.PassWord = value;
+                Settings.Default.PassWord = StringEncryptor.Encrypt(value);
                 Settings.Default.Save();
             }
-
         }
 
         /// <summary>

src/BitbucketBackup/StringEncryptor.cs

+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace BitbucketBackup
+{
+    /// <summary>
+    /// Encrypts and decrypts strings.
+    /// </summary>
+    internal static class StringEncryptor
+    {
+        /// <summary>
+        /// entropy for password encryption
+        /// </summary>
+        private readonly static byte[] entropy = { 1, 2, 3, 4 };
+
+        /// <summary>
+        /// Encrypts the given string.
+        /// </summary>
+        /// <param name="input">The string to encrypt</param>
+        /// <returns>The encrypted string</returns>
+        public static string Encrypt(string input)
+        {
+            byte[] unencrypted = Encoding.Unicode.GetBytes(input);
+            byte[] encrypted = ProtectedData.Protect(unencrypted, entropy, DataProtectionScope.CurrentUser);
+            return Convert.ToBase64String(encrypted);
+        }
+
+        /// <summary>
+        /// Decrypts the given encrypted string.
+        /// </summary>
+        /// <param name="input">The encrypted string to decrypt</param>
+        /// <returns>The decrypted string</returns>
+        public static string Decrypt(string input)
+        {
+            byte[] encrypted = Convert.FromBase64String(input);
+            byte[] decrypted = ProtectedData.Unprotect(encrypted, entropy, DataProtectionScope.CurrentUser);
+            return Encoding.Unicode.GetString(decrypted);
+        }
+    }
+}