简体   繁体   English

ProtectedData使用字符串字典

[英]ProtectedData use a dictionary of strings

I would use the ProtectedData I found here: https://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata%28v=vs.110%29.aspx 我将使用在这里找到的ProtectedData: https//msdn.microsoft.com/zh-cn/library/system.security.cryptography.protecteddata%28v=vs.110%29.aspx

My problem is that I can not understand how I can adapt a dictionary to a source of bytes, although it converts from a byte [] string, when I go to do the Unprotect, can not decrypt it. 我的问题是我无法理解如何使字典适应字节源,尽管它是从byte []字符串转换而来的,但是当我去做Unprotect时,无法解密它。 I have tried so: 我已经尝试过了:

code without DataProtection: 没有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"));
    }
}

i try this: 我试试这个:

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;
}    

Once I read it again, by calling the file, no longer able to open it ... how do I fix? 再次读取该文件后,通过调用该文件,它不再能够打开它...我该如何解决?

For some reason you assume that your file is a text file and that byte arrays can be serialized and deserialized with a simple call to ToString() . 出于某种原因,您假定您的文件是文本文件,并且可以通过简单调用ToString()来对字节数组进行序列化和反序列化。 That is not the case. 事实并非如此。

Use the methods of File that read and write byte arrays. 使用读写字节数组的File方法。 Because that is what you have. 因为那就是你所拥有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM