public PortableData Create() { // 1. Decrypt stream ExportDataEncryptor decryptor = new ExportDataEncryptor(dataStream); using (MemoryStream decrypted = new MemoryStream()) { decryptor.Decrypt(decrypted); decrypted.Position = 0; // 2. Decompress stream ExportDataCompressor decompressor = new ExportDataCompressor(decrypted); using (MemoryStream decompressed = new MemoryStream()) { decompressor.Decompress(decompressed); decompressed.Position = 0; // 3. Deserialize stream PsaTracesSerializer deserializer = new PsaTracesSerializer(); PortableData result = deserializer.Deserialize(decompressed); return(result); } } }
public void Export() { try { string fileName = filePathDelegate.Invoke(); PsaTracesSerializer serializer = new PsaTracesSerializer(traces); using (MemoryStream serialized = new MemoryStream()) { serializer.Serialize(serialized); serialized.Position = 0; ExportDataCompressor compressor = new ExportDataCompressor(serialized); using (MemoryStream compressed = new MemoryStream()) { compressor.Compress(compressed); compressed.Position = 0; ExportDataEncryptor encryptor = new ExportDataEncryptor(compressed); using (MemoryStream encrypted = new MemoryStream()) { encryptor.Encrypt(encrypted); encrypted.Position = 0; if (!String.IsNullOrEmpty(fileName)) { using (FileStream output = new FileStream(fileName, FileMode.CreateNew)) { encrypted.CopyTo(output); } } } } } if (finishCallback != null) { finishCallback.Invoke(true, fileName); } } catch (Exception e) { if (finishCallback != null) { finishCallback.Invoke(false, e.Message); } } }
public void TestEncryptionDecryption() { FileStream inFile = new FileStream("Encryption/TextFile.txt", FileMode.Open); ExportDataEncryptor encryptor = new ExportDataEncryptor(inFile); MemoryStream encrypted = new MemoryStream(); encryptor.Encrypt(encrypted); encrypted.Flush(); encrypted.Position = 0; BinaryReader rdrEncr = new BinaryReader(encrypted); byte[] encrBytes = new byte[encrypted.Length]; for (int i = 0; i < encrypted.Length; i++) { encrBytes[i] = rdrEncr.ReadByte(); } //Assert.IsFalse( == "secret service"); encrypted.Position = 0; ExportDataEncryptor decryptor = new ExportDataEncryptor(encrypted); MemoryStream decrypted = new MemoryStream(); decryptor.Decrypt(decrypted); decrypted.Position = 0; StreamReader rdrDecr = new StreamReader(decrypted); string decrStr = rdrDecr.ReadToEnd(); Assert.IsTrue(decrStr.Contains("<tag1>vaue1</tag1>")); }
public static PortableData Import(string vtsFilePathname) { if (String.IsNullOrEmpty(vtsFilePathname)) { throw new ArgumentNullException("vtsFilePathname"); } if (!File.Exists(vtsFilePathname)) { throw new ArgumentException(".VTS File does not exist."); } PortableData result; using (FileStream file = new FileStream(vtsFilePathname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { file.Position = 0; ExportDataEncryptor decryptor = new ExportDataEncryptor(file); using (MemoryStream decryptedFile = new MemoryStream()) { decryptor.Decrypt(decryptedFile); //decryptedFile.Position = 0; ExportDataCompressor decompressor = new ExportDataCompressor(decryptedFile); using (MemoryStream decompressed = new MemoryStream()) { decompressor.Decompress(decompressed); decompressed.Position = 0; PsaTracesSerializer deserializer = new PsaTracesSerializer(); result = deserializer.Deserialize(decompressed); } } } return(result); }