Fay*_*aik 2 c# encryption dpapi
我尝试过使用UTF8算法和SHA256进行密码加密,但建议不要使用它们.相反,我被建议使用DPAPI.我浏览了一些谷歌的示例代码并不清楚.你能帮我解决DPAPI算法吗?
Tho*_*mas 13
您可以使用ProtectedData类访问DPAPI .有两种加密模式:
对字符串进行编码并返回可以保存在数据库中的Base64字符串:
public static string Protect(string stringToEncrypt, string optionalEntropy, DataProtectionScope scope)
{
return Convert.ToBase64String(
ProtectedData.Protect(
Encoding.UTF8.GetBytes(stringToEncrypt)
, optionalEntropy != null ? Encoding.UTF8.GetBytes(optionalEntropy) : null
, scope));
}
Run Code Online (Sandbox Code Playgroud)
解码Base64字符串(您之前保存在数据库中):
public static string Unprotect(string encryptedString, string optionalEntropy, DataProtectionScope scope)
{
return Encoding.UTF8.GetString(
ProtectedData.Unprotect(
Convert.FromBase64String(encryptedString)
, optionalEntropy != null ? Encoding.UTF8.GetBytes(optionalEntropy) : null
, scope));
}
Run Code Online (Sandbox Code Playgroud)
您需要记住加密仅对机器(以及用户,如果您选择CurrentUser加密模式)有效,因此需要在同一服务器上执行加密/解密.
如果您计划在负载平衡环境下使用DPAPI,请参阅此文章.
如果您需要更多信息,请告诉我.
| 归档时间: |
|
| 查看次数: |
6166 次 |
| 最近记录: |