菜的像徐坤
排名
7
文章
192
粉丝
15
评论
16
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

RSA解密实体

3670人阅读 2023/4/25 15:21 总访问:960929 评论:0 收藏:0 手机
分类: .net

前言

衔接上一篇文章,在配置好加解密之后,我从配置文件直接映射的实体到代码里,里面的属性含有加密数据,也可能传递一个List。我需要对传递过来的对象属性进行解密并返回。
下边来上代码


解密实体中所有属性,并重新赋值

  1.         /// <summary>
  2.         /// 遍历对象属性,尝试进行解密,失败则原样返回
  3.         /// </summary>
  4.         /// <typeparam name="T">List或者实体</typeparam>
  5.         /// <param name="model"></param>
  6.         /// <returns></returns>
  7.         public static T ForeachClassProperties<T>(T model) where T : classnew()
  8.         {
  9.             try
  10.             {
  11.                 string privateKey = RSAHelper.GetPrivateKey();
  12.                 Type t = model.GetType();
  13.                 PropertyInfo[] propertyList = t.GetProperties();
  14.                 //除开list
  15.                 if (t.Name.IndexOf("List`", StringComparison.Ordinal) < 0)
  16.                 {
  17.                     foreach (PropertyInfo item in propertyList)
  18.                     {
  19.                         if (item.PropertyType.Name == "String")
  20.                         {
  21.                             if (item.CanWrite == true && item.PropertyType == typeof(string))
  22.                             {
  23.                                 var value = item.GetValue(model) ?? "";
  24.                                 if (value.IsNullOrEmpty())
  25.                                 {
  26.                                     continue;
  27.                                 }
  28.                                 try
  29.                                 {
  30.                                     item.SetValue(model, RSAHelper.Decrypt(privateKey, value.ToString()));
  31.                                 }
  32.                                 catch (Exception e)
  33.                                 {
  34.                                     continue;
  35.                                 }
  36.                             }
  37.                         }
  38.                     }
  39.                 }
  40.                 else
  41.                 {
  42.                     //获取
  43.                     var instanceName = propertyList[2].PropertyType.Name;
  44.                     var namespaceName = propertyList[2].PropertyType.Namespace;
  45.                     Assembly assembly = propertyList[2].PropertyType.Assembly;
  46.                     //Assembly.Load($"{namespaceName.Split('.')[0]}.{namespaceName.Split('.')[1]}");//加载程序集
  47.                     Type type = propertyList[2].PropertyType;
  48.                     //assembly.GetType(namespaceName + "." + instanceName);
  49.                     Type listType = typeof(List<>);
  50.                     //指定泛型的具体类型
  51.                     Type newType = listType.MakeGenericType(new Type[] { type });
  52.                     dynamic list = Activator.CreateInstance(newType, model);
  53.                     foreach (var item in list)
  54.                     {
  55.                         propertyList = item.GetType().GetProperties();
  56.                         foreach (PropertyInfo item1 in propertyList)
  57.                         {
  58.                             if (item1.CanWrite == true && item1.PropertyType == typeof(string))
  59.                             {
  60.                                 object value = item1.GetValue(item) ?? "";
  61.                                 if (value.IsNullOrEmpty())
  62.                                 {
  63.                                     continue;
  64.                                 }
  65.                                 try
  66.                                 {
  67.                                     item1.SetValue(item, RSAHelper.Decrypt(privateKey, value.ToString()));
  68.                                 }
  69.                                 catch (Exception e)
  70.                                 {
  71.                                     continue;
  72.                                 }
  73.                             }
  74.                         }
  75.                     }
  76.                     model = list;
  77.                 }
  78.                 return model;
  79.             }
  80.             catch (Exception e)
  81.             {
  82.                 return model;
  83.             }
  84.         }


评价