Skip to content

Instantly share code, notes, and snippets.

@jkuemerle
Created August 30, 2012 03:12
Show Gist options
  • Select an option

  • Save jkuemerle/3521954 to your computer and use it in GitHub Desktop.

Select an option

Save jkuemerle/3521954 to your computer and use it in GitHub Desktop.
Attribute to decorate the type declaration for a class that has auto encrypted properties
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Aspects.Dependencies;
namespace DataAccess.Aspects {
[Serializable]
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(EncryptedAttribute))]
[IntroduceInterface(typeof(IEncrypted), OverrideAction = InterfaceOverrideAction.Ignore)]
[AttributeUsage(AttributeTargets.Class)]
public class EncryptedTypeAttribute : InstanceLevelAspect, IEncrypted {
[IntroduceMember(IsVirtual=false,OverrideAction=MemberOverrideAction.OverrideOrFail, Visibility=PostSharp.Reflection.Visibility.Public)]
public Dictionary<string, string> EncryptedValues { get; set; }
public override void RuntimeInitialize(Type type) {
EncryptedValues = new Dictionary<string, string>();
}
public object AsClear(string Name) {
if (null != EncryptedValues) {
if (EncryptedValues.ContainsKey(Name))
return System.Text.UnicodeEncoding.Unicode.GetString(ProtectedData.Unprotect(
Convert.FromBase64String(EncryptedValues[Name].Substring(136)),
Convert.FromBase64String(EncryptedValues[Name].Substring(0, 136)),
DataProtectionScope.CurrentUser));
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment