Issue deserializing encrypted data using BinaryFormatter

后端 未结 1 1175
情书的邮戳
情书的邮戳 2021-01-21 22:21

Here is my code:

    public static void Save(T toSerialize, string fileSpec) {
        BinaryFormatter formatter = new BinaryFormatter();
        DESCry         


        
1条回答
  •  Happy的楠姐
    2021-01-21 22:55

    One small typo: your Load method should use des.CreateDecryptor, like this:

    public static T Load(string fileSpec)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
        using (FileStream stream = File.OpenRead(fileSpec))
        {
            using (CryptoStream cryptoStream = 
                   new CryptoStream(stream, des.CreateDecryptor(key, iv),
                                    CryptoStreamMode.Read))
            {
                return (T)formatter.Deserialize(cryptoStream);
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题