Ejemplo n.º 1
0
        /// <summary>
        /// 加密Json字符串后序列化成dat文件
        /// </summary>
        static void TestEncryptDat()
        {
            //加密
            User user = new User
            {
                Email        = "*****@*****.**",
                Password     = "******",
                LoginIM      = "0",
                LoginByEmail = "1",
                PKcity       = "197e6c8f-6d32-4a27-867a-0ac6e77ee729",
                PKCompany    = "3478b0f5-ca53-4c07-a3b7-4977222c1619",
                PKUser       = "******",
                Role         = "系统管理员"
            };

            using (FileStream fileStream = new FileStream("user.dat", FileMode.Create))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                var             value           = JsonConvert.SerializeObject(user);

                Stopwatch watch = new Stopwatch();
                watch.Start();
                var secureValue = DpapiHelper.EncryptString(DpapiHelper.ToSecureString(value));
                watch.Stop();
                Console.WriteLine($"加密花费时间:{watch.ElapsedMilliseconds}毫秒");

                binaryFormatter.Serialize(fileStream, secureValue);
            }
        }
Ejemplo n.º 2
0
        static void TestConfig()
        {
            DpapiHelper.EncryptConfigSection("appSettings");
            //DPAPIHelper.DecryptConfigSection("appSettings");

            //Configuration config = ConfigurationManager.OpenExeConfiguration("Fooww.Soft.DataModel.dll.config");
            //var connectionStrings = config.AppSettings.Settings["connectionStrings"];
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 针对XML的value加密
        /// </summary>
        static void TestXML()
        {
            //加密密码
            XElement xElement    = XElement.Load("UserSetting.xml");
            var      userTag     = xElement.Elements().FirstOrDefault(item => item.Name == "User");
            var      passwordTag = userTag.Elements().FirstOrDefault(item => item.Name == "Password");
            //var securePassword = EncryptString(ToSecureString(passwordTag.Value));
            //passwordTag.Value = securePassword;
            //xElement.Save("UserSetting.xml");

            //取出原密码
            var password = DpapiHelper.ToInsecureString(DpapiHelper.DecryptString(passwordTag.Value));

            Console.WriteLine($"原密码:{password}");
        }
Ejemplo n.º 4
0
        static void TestDecryptDat()
        {
            using (FileStream fileStream = new FileStream("user.dat", FileMode.Open))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                var             secureValue     = binaryFormatter.Deserialize(fileStream);

                Stopwatch watch = new Stopwatch();
                watch.Start();
                var value = DpapiHelper.ToInsecureString(DpapiHelper.DecryptString(secureValue.ToString()));
                watch.Stop();
                Console.WriteLine($"解密花费时间:{watch.ElapsedMilliseconds}毫秒");

                var userValue = JsonConvert.DeserializeObject <User>(value);
            }
        }