2using System.Runtime.Serialization;
3using System.Security.Cryptography;
19 using (var stream = File.OpenRead(path))
20 using (var algorithm = Aes.Create())
22 var iv =
new byte[algorithm.IV.Length];
23 stream.Read(iv, 0, iv.Length);
25 using (var encryptor = algorithm.CreateDecryptor(key, iv))
26 using (var crypto =
new CryptoStream(stream, encryptor, CryptoStreamMode.Read))
27 using (var reader =
new StreamReader(crypto))
29 return reader.ReadToEnd();
41 public static void EncryptToStream<T>(Stream stream, XmlObjectSerializer serializer, T graph,
byte[] key)
43 using (var algorithm = Aes.Create())
44 using (var encryptor = algorithm.CreateEncryptor(key, algorithm.IV))
45 using (var crypto =
new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
47 stream.Write(algorithm.IV, 0, algorithm.IV.Length);
48 serializer.WriteObject(crypto, graph);
60 using (var algorithm = Aes.Create())
62 var iv =
new byte[algorithm.IV.Length];
63 stream.Read(iv, 0, iv.Length);
65 using (var decryptor = algorithm.CreateDecryptor(key, iv))
66 using (var crypto =
new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
68 return (T)serializer.ReadObject(crypto);
Contains methods related to encryption and decryption.
static void EncryptToStream< T >(Stream stream, XmlObjectSerializer serializer, T graph, byte[] key)
Encrypts the object to the stream.
static T DecryptFromStream< T >(Stream stream, XmlObjectSerializer serializer, byte[] key)
Decrypts the object from the stream.
static string DecryptTextFile(string path, byte[] key)
Returns the decrypted text for the file at the specified path.