/// <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}"); }
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); } }