12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using OASystem.Domain.Attributes;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace OASystem.Domain.AesEncryption
- {
-
-
-
- public static class EncryptionProcessor
- {
-
-
-
-
-
- public static void EncryptProperties(object obj)
- {
- if (obj == null) return;
- var properties = obj.GetType()
- .GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.IsDefined(typeof(EncryptedAttribute), false) && p.CanWrite && p.CanRead);
- foreach (var property in properties)
- {
- var value = property.GetValue(obj) as string;
- if (!string.IsNullOrEmpty(value))
- {
- var encryptedValue = AesEncryptionHelper.Encrypt(value);
- property.SetValue(obj, encryptedValue);
- }
- }
- }
-
-
-
-
- public static void DecryptProperties(object obj)
- {
- if (obj == null) return;
- var properties = obj.GetType()
- .GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(p => p.IsDefined(typeof(EncryptedAttribute), false) && p.CanWrite && p.CanRead);
- foreach (var property in properties)
- {
- var value = property.GetValue(obj) as string;
- if (!string.IsNullOrEmpty(value))
- {
- var decryptedValue = AesEncryptionHelper.Decrypt(value);
- property.SetValue(obj, decryptedValue);
- }
- }
- }
-
-
-
-
-
- public static bool IsEncrypted(string value)
- {
- try
- {
-
- var bytes = Convert.FromBase64String(value);
-
- return bytes.Length % 16 == 0;
- }
- catch
- {
- return false;
- }
- }
- }
- }
|