【问题标题】:how to Decrypt Application settings before binding?如何在绑定前解密应用程序设置?
【发布时间】:2025-11-26 10:35:02
【问题描述】:

我在我的 c# 项目中使用应用程序设置来保存 gmail 用户名和密码。在保存设置之前,我正在加密纯文本密码。当我重新启动应用程序时,检索它们当然没有任何问题,但检索到的密码是加密的。我想要的是在绑定回文本框之前解密它 设计器生成的代码如下:

this.tb_Gmail_Password.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::MyApp.Properties.Settings.Default, "GmailPassword", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

我不知道我能否正确地提出我的问题。如果我听起来像是在说外语,请随意踢我。我会根据你的建议给出更多的描述……

问候, N10i。

--编辑--

在保存配置时,我使用自定义函数来加密密码,即

Properties.Settings.Default.GmailPassword = MyApp.EncPass(tb_Gmail_Password.Text);

我可以使用类似的东西吗

this.tb_Gmail_Password.DataBindings.Add(new System.Windows.Forms.Binding("Text", global::MyApp.Properties.Settings.Default, MyApp.DecPass("GmailPassword"), true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

MyApp.DecPass("GmailPassword")

【问题讨论】:

    标签: c# .net visual-studio winforms


    【解决方案1】:

    您使用 DPAPI DpapiProtectedConfigurationProvider 加密配置,并在应用程序启动时使用相同的方式对其进行解密,请参阅 Encrypting Configuration Information Using Protected Configuration

    【讨论】:

    • 感谢您的回复,我将研究 DPAPI,但是否可以使用我在编辑后的帖子中提到的类似内容?谢谢和问候,N10I
    • 您可以将委托附加到 Setting.Default.OnSettingsLoaded 和 Settings.Default.OnSettingsSaving 以动态解密/加密您的密码,就像从配置文件加载/保存到配置文件一样。
    • 你可以请。使用示例代码进行解释。如果可以的话,这将是一个很大的帮助!
    最近更新 更多