Added backward compatibility support to encrypted string value provider

This commit is contained in:
Jaex 2020-05-30 13:08:05 +03:00
parent 4371884843
commit dfe387a113

View file

@ -30,6 +30,8 @@ namespace ShareX.HelpersLib
{
public class DPAPIEncryptedStringValueProvider : IValueProvider
{
private const string encryptedTag = "$DPAPIEncrypted$";
private PropertyInfo targetProperty;
public DPAPIEncryptedStringValueProvider(PropertyInfo targetProperty)
@ -40,13 +42,12 @@ public DPAPIEncryptedStringValueProvider(PropertyInfo targetProperty)
public object GetValue(object target)
{
string value = (string)targetProperty.GetValue(target);
string encryptedValue = null;
if (!string.IsNullOrEmpty(value))
{
try
{
encryptedValue = DPAPI.Encrypt(value);
value = encryptedTag + DPAPI.Encrypt(value);
}
catch
{
@ -54,22 +55,26 @@ public object GetValue(object target)
}
return encryptedValue;
return value;
}
public void SetValue(object target, object value)
{
string decryptedValue = null;
string text = (string)value;
try
{
decryptedValue = DPAPI.Decrypt((string)value);
}
catch
if (!string.IsNullOrEmpty(text) && text.StartsWith(encryptedTag))
{
try
{
string encryptedString = text.Substring(encryptedTag.Length);
text = DPAPI.Decrypt(encryptedString);
}
catch
{
}
}
targetProperty.SetValue(target, decryptedValue);
targetProperty.SetValue(target, text);
}
}
}