Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest doing JSON or XML serialization and then encrypting the contents with some algorithm. I wouldn't go with binary serialization because it's not very friendly when you need to change the assembly version.</p> <p>I am using the following code together with Newtonsoft.Json (you can get it on NuGet) to accomplish this:</p> <pre><code>using System.IO; using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; class SecureJsonSerializer&lt;T&gt; where T : class { private readonly string filePath; private readonly ICryptoTransform encryptor; private readonly ICryptoTransform decryptor; private const string Password = "some password"; private static readonly byte[] passwordBytes = Encoding.ASCII.GetBytes(Password); public SecureJsonSerializer(string filePath) { this.filePath = filePath; var rmCrypto = GetAlgorithm(); this.encryptor = rmCrypto.CreateEncryptor(); this.decryptor = rmCrypto.CreateDecryptor(); } private static RijndaelManaged GetAlgorithm() { var algorithm = new RijndaelManaged(); int bytesForKey = algorithm.KeySize / 8; int bytesForIV = algorithm.BlockSize / 8; algorithm.Key = key.GetBytes(bytesForKey); algorithm.IV = key.GetBytes(bytesForIV); return algorithm; } public void Save(T obj) { using (var writer = new StreamWriter(new CryptoStream(File.Create(this.filePath), this.encryptor, CryptoStreamMode.Write))) { writer.Write(JsonConvert.SerializeObject(obj)); } } public T Load() { using (var reader = new StreamReader(new CryptoStream(File.OpenRead(this.filePath), this.decryptor, CryptoStreamMode.Read))) { return JsonConvert.DeserializeObject&lt;T&gt;(reader.ReadToEnd()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload