/// <summary> /// Encrypts the Properties of an Object /// </summary> /// <param name="Target"></param> private static void EncryptProperties(object Target) { if (Target == null) { return; } if (Target is System.Data.DataTable) { return; } object oValue = null; try { Type type = Target.GetType(); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type propType = EncryptableAttribute.GetPropertyType(prop.PropertyType); if (prop.PropertyType.IsClass && propType.FullName != "System.String" && propType.FullName != "System.String[]" && propType.FullName != "System.Object" && propType.FullName != "System.Object[]" && propType.FullName.Contains("List") == false) { oValue = prop.GetValue(Target, null); EncryptProperties(oValue); continue; } if (!prop.CanWrite) { continue; } object[] attribs = prop.GetCustomAttributes(typeof(EncryptableAttribute), true); if (attribs.Length == 0) { continue; } EncryptableAttribute attrib = (EncryptableAttribute)attribs[0]; oValue = prop.GetValue(Target, null); if (oValue != null && !string.IsNullOrEmpty(oValue.ToString())) { oValue = Cryptographer.Encrypt(oValue.ToString(), attrib.Key, attrib.Salt, attrib.Algorithm, true); SetPropertyValue(prop, Target, oValue, ""); // Set the Value } } } catch (Exception ex) { throw ex; } }
/// <summary> /// Decrypts the Fields of an Object /// </summary> /// <param name="Target"></param> private static void DecryptFields(object Target) { if (Target == null) { return; } if (Target is System.Data.DataTable || Target is System.Data.DataRow) { return; } object oValue = null; try { Type type = Target.GetType(); FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo field in fields) { Type fieldType = EncryptableAttribute.GetPropertyType(field.FieldType); if (fieldType.IsClass && fieldType.FullName != "System.String" && fieldType.FullName != "System.String[]" && fieldType.FullName != "System.Object" && fieldType.FullName != "System.Object[]" && fieldType.FullName.Contains("List") == false) { oValue = field.GetValue(Target); DecryptFields(oValue); continue; } object[] attribs = field.GetCustomAttributes(typeof(EncryptableAttribute), true); if (attribs.Length == 0) { continue; } EncryptableAttribute attrib = (EncryptableAttribute)attribs[0]; oValue = field.GetValue(Target); if (oValue != null && !string.IsNullOrEmpty(oValue.ToString())) { oValue = Cryptographer.Decrypt(oValue.ToString(), attrib.Key, attrib.Salt, attrib.Algorithm, true); SetFieldValue(field, Target, oValue); // Set the Value } } } catch (Exception ex) { throw ex; } }