[英]How can I encrypt user settings (such as passwords) in my application?
我想为用户提供加密保存个人数据的能力。 这可能是微不足道的,或者可能已经被问到,但我找不到加密/解密密码的易于使用的方法的示例。
我真的不需要任何超级魔法无法破解的密码。 我只需要密码很难破解。
我看过一些 MSDN 和 SO 问题,但没有找到可用的东西。
大卫,我认为你的回答很漂亮,但我认为这些作为扩展方法会更漂亮。 这将允许这样的语法:
string cypherText;
string clearText;
using (var secureString = "Some string to encrypt".ToSecureString())
{
cypherText = secureString.EncryptString();
}
using (var secureString = cypherText.DecryptString())
{
clearText = secureString.ToInsecureString();
}
这是更新后的代码:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
public static class SecureIt
{
private static readonly byte[] entropy = Encoding.Unicode.GetBytes("Salt Is Not A Password");
public static string EncryptString(this SecureString input)
{
if (input == null)
{
return null;
}
var encryptedData = ProtectedData.Protect(
Encoding.Unicode.GetBytes(input.ToInsecureString()),
entropy,
DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
public static SecureString DecryptString(this string encryptedData)
{
if (encryptedData == null)
{
return null;
}
try
{
var decryptedData = ProtectedData.Unprotect(
Convert.FromBase64String(encryptedData),
entropy,
DataProtectionScope.CurrentUser);
return Encoding.Unicode.GetString(decryptedData).ToSecureString();
}
catch
{
return new SecureString();
}
}
public static SecureString ToSecureString(this IEnumerable<char> input)
{
if (input == null)
{
return null;
}
var secure = new SecureString();
foreach (var c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
public static string ToInsecureString(this SecureString input)
{
if (input == null)
{
return null;
}
var ptr = Marshal.SecureStringToBSTR(input);
try
{
return Marshal.PtrToStringBSTR(ptr);
}
finally
{
Marshal.ZeroFreeBSTR(ptr);
}
}
}
以下内容非常简单,假设您真的只想加密/解密字符串并将其存储到磁盘。 请注意,这不使用密码,它使用登录用户System.Security.Cryptography.DataProtectionScope.CurrentUser的安全上下文来保护数据。
public class SecureIt
{
static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("Salt Is Not A Password");
public static string EncryptString(System.Security.SecureString input)
{
byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
entropy,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
public static SecureString DecryptString(string encryptedData)
{
try
{
byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
Convert.FromBase64String(encryptedData),
entropy,
System.Security.Cryptography.DataProtectionScope.CurrentUser);
return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
}
catch
{
return new SecureString();
}
}
public static SecureString ToSecureString(string input)
{
SecureString secure = new SecureString();
foreach (char c in input)
{
secure.AppendChar(c);
}
secure.MakeReadOnly();
return secure;
}
public static string ToInsecureString(SecureString input)
{
string returnValue = string.Empty;
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
try
{
returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
}
finally
{
System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
}
return returnValue;
}
}
然后加密一个字符串:
var clearText = "Some string to encrypt";
var cypherText = SecureIt.EncryptString( SecureIt.ToSecureString( clearText));
并随后解密:
var clearText = SecureIt.ToInsecureString( SecureIt.DecryptString(cypherText));
出于我的目的,我修改Jesse C. Slicer解决方案以不使用SecureString
因为保护内存对我来说并不重要。 我只需要序列化加密的字符串。 为了使这个工作项目需要引用System.Security
(不仅仅是 using 语句)。
public static class StringSecurityHelper
{
private static readonly byte[] entropy = Encoding.Unicode.GetBytes("5ID'&mc %sJo@lGtbi%n!G^ fiVn8 *tNh3eB %rDaVijn!.c b");
public static string EncryptString(this string input)
{
if (input == null)
{
return null;
}
byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(input), entropy, DataProtectionScope.CurrentUser);
return Convert.ToBase64String(encryptedData);
}
public static string DecryptString(this string encryptedData)
{
if (encryptedData == null)
{
return null;
}
try
{
byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(encryptedData), entropy, DataProtectionScope.CurrentUser);
return Encoding.Unicode.GetString(decryptedData);
}
catch
{
return null;
}
}
}
并使用它:
string cypherText = "My string".EncryptString();
string clearText = cypherText.DecryptString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.