author | StephaneLenclud |
Wed, 05 Oct 2016 15:02:09 +0200 | |
changeset 270 | 5dcd226539d4 |
permissions | -rw-r--r-- |
StephaneLenclud@268 | 1 |
using System; |
StephaneLenclud@268 | 2 |
using System.Collections.Generic; |
StephaneLenclud@268 | 3 |
using System.Linq; |
StephaneLenclud@268 | 4 |
using System.Security; |
StephaneLenclud@268 | 5 |
using System.Text; |
StephaneLenclud@268 | 6 |
using System.Threading.Tasks; |
StephaneLenclud@268 | 7 |
|
StephaneLenclud@268 | 8 |
namespace SharpDisplayManager |
StephaneLenclud@268 | 9 |
{ |
StephaneLenclud@268 | 10 |
/// <summary> |
StephaneLenclud@268 | 11 |
/// As per: http://weblogs.asp.net/jongalloway/encrypting-passwords-in-a-net-app-config-file |
StephaneLenclud@268 | 12 |
/// </summary> |
StephaneLenclud@268 | 13 |
class Secure |
StephaneLenclud@268 | 14 |
{ |
StephaneLenclud@268 | 15 |
static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("kd566slqfjls234895DFG743fqdlskj345SDFDepozwxc,n*ù^$^é"); |
StephaneLenclud@268 | 16 |
|
StephaneLenclud@268 | 17 |
public static string EncryptString(System.Security.SecureString input) |
StephaneLenclud@268 | 18 |
{ |
StephaneLenclud@268 | 19 |
byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect( |
StephaneLenclud@268 | 20 |
System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)), |
StephaneLenclud@268 | 21 |
entropy, |
StephaneLenclud@268 | 22 |
System.Security.Cryptography.DataProtectionScope.CurrentUser); |
StephaneLenclud@268 | 23 |
return Convert.ToBase64String(encryptedData); |
StephaneLenclud@268 | 24 |
} |
StephaneLenclud@268 | 25 |
|
StephaneLenclud@268 | 26 |
public static SecureString DecryptString(string encryptedData) |
StephaneLenclud@268 | 27 |
{ |
StephaneLenclud@268 | 28 |
try |
StephaneLenclud@268 | 29 |
{ |
StephaneLenclud@268 | 30 |
byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect( |
StephaneLenclud@268 | 31 |
Convert.FromBase64String(encryptedData), |
StephaneLenclud@268 | 32 |
entropy, |
StephaneLenclud@268 | 33 |
System.Security.Cryptography.DataProtectionScope.CurrentUser); |
StephaneLenclud@268 | 34 |
return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData)); |
StephaneLenclud@268 | 35 |
} |
StephaneLenclud@268 | 36 |
catch |
StephaneLenclud@268 | 37 |
{ |
StephaneLenclud@268 | 38 |
return new SecureString(); |
StephaneLenclud@268 | 39 |
} |
StephaneLenclud@268 | 40 |
} |
StephaneLenclud@268 | 41 |
|
StephaneLenclud@268 | 42 |
public static SecureString ToSecureString(string input) |
StephaneLenclud@268 | 43 |
{ |
StephaneLenclud@268 | 44 |
SecureString secure = new SecureString(); |
StephaneLenclud@268 | 45 |
foreach (char c in input) |
StephaneLenclud@268 | 46 |
{ |
StephaneLenclud@268 | 47 |
secure.AppendChar(c); |
StephaneLenclud@268 | 48 |
} |
StephaneLenclud@268 | 49 |
secure.MakeReadOnly(); |
StephaneLenclud@268 | 50 |
return secure; |
StephaneLenclud@268 | 51 |
} |
StephaneLenclud@268 | 52 |
|
StephaneLenclud@268 | 53 |
public static string ToInsecureString(SecureString input) |
StephaneLenclud@268 | 54 |
{ |
StephaneLenclud@268 | 55 |
string returnValue = string.Empty; |
StephaneLenclud@268 | 56 |
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input); |
StephaneLenclud@268 | 57 |
try |
StephaneLenclud@268 | 58 |
{ |
StephaneLenclud@268 | 59 |
returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr); |
StephaneLenclud@268 | 60 |
} |
StephaneLenclud@268 | 61 |
finally |
StephaneLenclud@268 | 62 |
{ |
StephaneLenclud@268 | 63 |
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr); |
StephaneLenclud@268 | 64 |
} |
StephaneLenclud@268 | 65 |
return returnValue; |
StephaneLenclud@268 | 66 |
} |
StephaneLenclud@268 | 67 |
} |
StephaneLenclud@268 | 68 |
} |