Esempio n. 1
0
        public void EncryptEntity <TEntity>(TEntity entity, string key, string iv) where TEntity : class, IEncryptedEntity
        {
            var encryptedProperties = entity.GetType().GetProperties()
                                      .Where(p => p.GetCustomAttributes(typeof(PropertyEncrypted), true).Any(a => p.PropertyType == typeof(string)));

            foreach (var property in encryptedProperties)
            {
                var value = property.GetValue(entity) as string;
                if (string.IsNullOrEmpty(value))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(key))
                {
                    key = _cryptoProvider.GetByteToken(32);
                }
                var encryptedValue = _cryptoProvider.EncryptString(value, key, iv);
                property.SetValue(entity, encryptedValue);
                entity.DecryptionPrivateKey = key;
            }
        }