我会使用我在这里找到的ProtectedData: https://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata%28v=vs.110%29.aspx
我的问题是我无法理解如何将字典调整为字节源,尽管它从byte []字符串转换,当我去做Unprotect时,无法解密它。我试过了:
没有DataProtection的代码:
public static void SetLoginUserDataForFile(ConnectionData data)
{
Dictionary<string, Dictionary<string, string>> retVal = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> dataValues = new Dictionary<string, string>();
if (File.Exists(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PROGRAMFILE", "PROGRAMFILE")))
{
string fileData = File.ReadAllText(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PROGRAMFILE", "PROGRAMFILE"));
try
{
retVal = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(Decrypt(fileData, "pa$$w0rd"));
}
catch (Exception ex)
{
retVal = new Dictionary<string, Dictionary<string, string>>();
}
dataValues.Add("HashedPwd", data.HashedPwd);
dataValues.Add("CanAccessOffline", data.LoggedUser.CanAccessOffline.ToString());
dataValues.Add("CanSavePassword", data.LoggedUser.CanSavePassword.ToString());
if (retVal.ContainsKey(data.LoggedUser.UserName))
retVal[data.LoggedUser.UserName] = dataValues;
else
retVal.Add(data.LoggedUser.UserName, dataValues);
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PROGRAMFILE", "PROGRAMFILE"), Encrypt(JsonConvert.SerializeObject(retVal), "pa$$w0rd"));
}
}
我试试这个:
public static void SetLoginUserDataForFile(ConnectionData data)
{
Assembly assembly = Assembly.GetExecutingAssembly();
String GuidID = assembly.GetType().GUID.ToString();
Dictionary<string, Dictionary<string, string>> retVal = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> dataValues = new Dictionary<string, string>();
if (File.Exists(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PROGRAMFILE", "PROGRAMFILE")))
{
string fileData = File.ReadAllText(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PROGRAMFILE", "PROGRAMFILE"));
try
{
retVal = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(Decrypt(fileData, DataProtection.Unprotect(GetBytes(GuidID)).ToString()));
}
catch (Exception ex)
{
retVal = new Dictionary<string, Dictionary<string, string>>();
}
dataValues.Add("HashedPwd", data.HashedPwd);
dataValues.Add("CanAccessOffline", data.LoggedUser.CanAccessOffline.ToString());
dataValues.Add("CanSavePassword", data.LoggedUser.CanSavePassword.ToString());
if (retVal.ContainsKey(data.LoggedUser.UserName))
retVal[data.LoggedUser.UserName] = dataValues;
else
retVal.Add(data.LoggedUser.UserName, dataValues);
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PROGRAMFILE", "PROGRAMFILE"), Encrypt(JsonConvert.SerializeObject(retVal), DataProtection.Protect(GetBytes(GuidID)).ToString()));
}
}
public static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
一旦我再次阅读,通过调用该文件,再也无法打开它...我该如何解决?
答案 0 :(得分:0)
出于某种原因,您假设您的文件是文本文件,并且可以通过简单调用ToString()
来序列化和反序列化字节数组。事实并非如此。
使用读取和写入字节数组的File
方法。因为这就是你拥有的。