C# Encrypt serialized file before writing to disk

前端 未结 4 1732
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 08:08

Let\'s say my program has a class called "customer" and the customer class is serializable so I can read and write it to disk. The customer class holds sensitive i

4条回答
  •  执笔经年
    2020-12-05 08:52

    You can use a CryptoStream to do the encryption at the same time as you serialize the class to a file:

    byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Where to store these keys is the tricky part, 
        // you may need to obfuscate them or get the user to input a password each time
    byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
    string path = @"C:\path\to.file";
    
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
    // Encryption
    using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write))
    using (var cryptoStream = new CryptoStream(fs, des.CreateEncryptor(key, iv), CryptoStreamMode.Write))
    {
        BinaryFormatter formatter = new BinaryFormatter();
    
        // This is where you serialize the class
        formatter.Serialize(cryptoStream, customClass);
    }
    
    // Decryption
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    using (var cryptoStream = new CryptoStream(fs, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))
    {
        BinaryFormatter formatter = new BinaryFormatter();
    
        // This is where you deserialize the class
        CustomClass deserialized = (CustomClass)formatter.Deserialize(cryptoStream);
    }
    

提交回复
热议问题