From 4371884843f775439ae2032da165147f9efc8da9 Mon Sep 17 00:00:00 2001 From: Jaex Date: Fri, 29 May 2020 19:13:37 +0300 Subject: [PATCH] Added DPAPI json contract resolver --- .../DPAPIEncryptedStringPropertyResolver.cs | 54 +++++++++++++ .../DPAPIEncryptedStringValueProvider.cs | 75 +++++++++++++++++++ .../Settings/JsonEncryptAttribute.cs | 34 +++++++++ .../WritablePropertiesOnlyResolver.cs | 2 +- ShareX.HelpersLib/ShareX.HelpersLib.csproj | 5 ++ 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 ShareX.HelpersLib/Settings/DPAPIEncryptedStringPropertyResolver.cs create mode 100644 ShareX.HelpersLib/Settings/DPAPIEncryptedStringValueProvider.cs create mode 100644 ShareX.HelpersLib/Settings/JsonEncryptAttribute.cs diff --git a/ShareX.HelpersLib/Settings/DPAPIEncryptedStringPropertyResolver.cs b/ShareX.HelpersLib/Settings/DPAPIEncryptedStringPropertyResolver.cs new file mode 100644 index 000000000..288e128bb --- /dev/null +++ b/ShareX.HelpersLib/Settings/DPAPIEncryptedStringPropertyResolver.cs @@ -0,0 +1,54 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2020 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace ShareX.HelpersLib +{ + public class DPAPIEncryptedStringPropertyResolver : WritablePropertiesOnlyResolver + { + protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) + { + IList props = base.CreateProperties(type, memberSerialization); + + foreach (JsonProperty prop in props.Where(p => p.PropertyType == typeof(string))) + { + PropertyInfo pi = type.GetProperty(prop.UnderlyingName); + + if (pi != null && pi.GetCustomAttribute(typeof(JsonEncryptAttribute), true) != null) + { + prop.ValueProvider = new DPAPIEncryptedStringValueProvider(pi); + } + } + + return props; + } + } +} \ No newline at end of file diff --git a/ShareX.HelpersLib/Settings/DPAPIEncryptedStringValueProvider.cs b/ShareX.HelpersLib/Settings/DPAPIEncryptedStringValueProvider.cs new file mode 100644 index 000000000..04dc87117 --- /dev/null +++ b/ShareX.HelpersLib/Settings/DPAPIEncryptedStringValueProvider.cs @@ -0,0 +1,75 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2020 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using Newtonsoft.Json.Serialization; +using System.Reflection; + +namespace ShareX.HelpersLib +{ + public class DPAPIEncryptedStringValueProvider : IValueProvider + { + private PropertyInfo targetProperty; + + public DPAPIEncryptedStringValueProvider(PropertyInfo targetProperty) + { + this.targetProperty = targetProperty; + } + + public object GetValue(object target) + { + string value = (string)targetProperty.GetValue(target); + string encryptedValue = null; + + if (!string.IsNullOrEmpty(value)) + { + try + { + encryptedValue = DPAPI.Encrypt(value); + } + catch + { + } + + } + + return encryptedValue; + } + + public void SetValue(object target, object value) + { + string decryptedValue = null; + + try + { + decryptedValue = DPAPI.Decrypt((string)value); + } + catch + { + } + + targetProperty.SetValue(target, decryptedValue); + } + } +} \ No newline at end of file diff --git a/ShareX.HelpersLib/Settings/JsonEncryptAttribute.cs b/ShareX.HelpersLib/Settings/JsonEncryptAttribute.cs new file mode 100644 index 000000000..d9beb4b0d --- /dev/null +++ b/ShareX.HelpersLib/Settings/JsonEncryptAttribute.cs @@ -0,0 +1,34 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2020 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using System; + +namespace ShareX.HelpersLib +{ + [AttributeUsage(AttributeTargets.Property)] + public class JsonEncryptAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/ShareX.HelpersLib/Settings/WritablePropertiesOnlyResolver.cs b/ShareX.HelpersLib/Settings/WritablePropertiesOnlyResolver.cs index d833137c6..c567ed708 100644 --- a/ShareX.HelpersLib/Settings/WritablePropertiesOnlyResolver.cs +++ b/ShareX.HelpersLib/Settings/WritablePropertiesOnlyResolver.cs @@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License namespace ShareX.HelpersLib { - internal class WritablePropertiesOnlyResolver : DefaultContractResolver + public class WritablePropertiesOnlyResolver : DefaultContractResolver { protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) { diff --git a/ShareX.HelpersLib/ShareX.HelpersLib.csproj b/ShareX.HelpersLib/ShareX.HelpersLib.csproj index aceaf4d08..d36e1b73c 100644 --- a/ShareX.HelpersLib/ShareX.HelpersLib.csproj +++ b/ShareX.HelpersLib/ShareX.HelpersLib.csproj @@ -91,6 +91,7 @@ + 3.5 @@ -139,6 +140,7 @@ Component + Form @@ -151,6 +153,9 @@ + + + Form