• 项目中的常用方法


     public class CommonTools
        {
            private static String EncodeKey="Simon";
            public static Object Copy(Object source, Object item)
            {
                Type t = source.GetType();
                foreach (PropertyInfo property in t.GetProperties())
                {
                    PropertyInfo pro = t.GetProperty(property.Name);
                    if (pro != null && property.PropertyType == pro.PropertyType && pro.CanWrite && property.CanRead)
                    {
                        pro.SetValue(item, property.GetValue(source, null), null);
                    }
                }
                return item;
            }
            public static String Encode(String str)
            {
                String result = String.Empty;
                CspParameters param = new CspParameters();
                param.KeyContainerName =EncodeKey;//密匙容器的名称,保持加密解密一致才能解密成功
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
                {
                    byte[] plaindata = Encoding.Default.GetBytes(str);//将要加密的字符串转换为字节数组
                    byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组
                    result = Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串
                }
                return result;
            }
            public static String Decode(String str)
            {
                String result = String.Empty;
                CspParameters param = new CspParameters();
                param.KeyContainerName = EncodeKey;
                using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
                {
                    byte[] encryptdata = Convert.FromBase64String(str);
                    byte[] decryptdata = rsa.Decrypt(encryptdata, false);
                    result = Encoding.Default.GetString(decryptdata);
                }
                return result;
            }
        }

  • 相关阅读:
    c#中文字符串与byte数组互相转化
    c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)
    c#的中英文混合字符串截取指定长度,startidx从0开始
    //字符是否为汉字
    //获得字节长度
    C# 截取中英文混合字符串分行显示宽度相同
    C#截取中英文混合字符串分行显示
    C#截取指定长度中英文字符串方法 (修改)
    截取字符串的长度(中英文)
    canvas贪吃蛇游戏
  • 原文地址:https://www.cnblogs.com/zhangsongshan/p/3056142.html
Copyright © 2020-2023  润新知