StephaneLenclud@268: using System; StephaneLenclud@268: using System.Collections.Generic; StephaneLenclud@268: using System.Linq; StephaneLenclud@268: using System.Security; StephaneLenclud@268: using System.Text; StephaneLenclud@268: using System.Threading.Tasks; StephaneLenclud@268: StephaneLenclud@268: namespace SharpDisplayManager StephaneLenclud@268: { StephaneLenclud@268: /// StephaneLenclud@268: /// As per: http://weblogs.asp.net/jongalloway/encrypting-passwords-in-a-net-app-config-file StephaneLenclud@268: /// StephaneLenclud@268: class Secure StephaneLenclud@268: { StephaneLenclud@268: static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("kd566slqfjls234895DFG743fqdlskj345SDFDepozwxc,n*ù^$^é"); StephaneLenclud@268: StephaneLenclud@268: public static string EncryptString(System.Security.SecureString input) StephaneLenclud@268: { StephaneLenclud@268: byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect( StephaneLenclud@268: System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)), StephaneLenclud@268: entropy, StephaneLenclud@268: System.Security.Cryptography.DataProtectionScope.CurrentUser); StephaneLenclud@268: return Convert.ToBase64String(encryptedData); StephaneLenclud@268: } StephaneLenclud@268: StephaneLenclud@268: public static SecureString DecryptString(string encryptedData) StephaneLenclud@268: { StephaneLenclud@268: try StephaneLenclud@268: { StephaneLenclud@268: byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect( StephaneLenclud@268: Convert.FromBase64String(encryptedData), StephaneLenclud@268: entropy, StephaneLenclud@268: System.Security.Cryptography.DataProtectionScope.CurrentUser); StephaneLenclud@268: return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData)); StephaneLenclud@268: } StephaneLenclud@268: catch StephaneLenclud@268: { StephaneLenclud@268: return new SecureString(); StephaneLenclud@268: } StephaneLenclud@268: } StephaneLenclud@268: StephaneLenclud@268: public static SecureString ToSecureString(string input) StephaneLenclud@268: { StephaneLenclud@268: SecureString secure = new SecureString(); StephaneLenclud@268: foreach (char c in input) StephaneLenclud@268: { StephaneLenclud@268: secure.AppendChar(c); StephaneLenclud@268: } StephaneLenclud@268: secure.MakeReadOnly(); StephaneLenclud@268: return secure; StephaneLenclud@268: } StephaneLenclud@268: StephaneLenclud@268: public static string ToInsecureString(SecureString input) StephaneLenclud@268: { StephaneLenclud@268: string returnValue = string.Empty; StephaneLenclud@268: IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input); StephaneLenclud@268: try StephaneLenclud@268: { StephaneLenclud@268: returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr); StephaneLenclud@268: } StephaneLenclud@268: finally StephaneLenclud@268: { StephaneLenclud@268: System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr); StephaneLenclud@268: } StephaneLenclud@268: return returnValue; StephaneLenclud@268: } StephaneLenclud@268: } StephaneLenclud@268: }