Ami*_*shk 2 c# security password-protection winforms
我有一个带有远程接口的桌面应用程序.通过用户名和密码保护对远程接口的访问.
什么是安全保存这些密码的最佳方法,最好是在注册表中?
如果确实需要存储未散列的密码,请查看使用ProtectedData类.这使用了Data Protection API(DPAPI),这是在Windows上保护数据的最佳方式.
这是一个包装ProtectedData的小类,它在String上提供了两种扩展方法来加密和解密数据:
public static class DataProtectionApiWrapper
{
/// <summary>
/// Specifies the data protection scope of the DPAPI.
/// </summary>
private const DataProtectionScope Scope = DataProtectionScope.CurrentUser;
public static string Encrypt(this string text)
{
if (text == null)
{
throw new ArgumentNullException("text");
}
//encrypt data
var data = Encoding.Unicode.GetBytes(text);
byte[] encrypted = ProtectedData.Protect(data, null, Scope);
//return as base64 string
return Convert.ToBase64String(encrypted);
}
public static string Decrypt(this string cipher)
{
if (cipher == null)
{
throw new ArgumentNullException("cipher");
}
//parse base64 string
byte[] data = Convert.FromBase64String(cipher);
//decrypt data
byte[] decrypted = ProtectedData.Unprotect(data, null, Scope);
return Encoding.Unicode.GetString(decrypted);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4715 次 |
| 最近记录: |