简体   繁体   中英

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

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. I have tried so:

code without 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() . That is not the case.

Use the methods of File that read and write byte arrays. Because that is what you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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