private void Encrypt(object obj) { using (Stream targetStream = new MemoryStream()) { DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings(); encryptionSettings.Password = this.EncryptionPassword ?? string.Empty; using (CompressedStream compressedStream = new CompressedStream(targetStream, StreamOperationMode.Write, new DeflateSettings(), false, encryptionSettings)) { StreamWriter writer = new StreamWriter(compressedStream); writer.Write(this.Input); writer.Flush(); } targetStream.Seek(0, SeekOrigin.Begin); this.bytes = new byte[targetStream.Length]; targetStream.Read(this.bytes, 0, this.bytes.Length); targetStream.Seek(0, SeekOrigin.Begin); using (StreamReader reader = new StreamReader(targetStream)) { this.Output = reader.ReadToEnd(); } } }
private void ReadProtectedArchive() { #region radziplibrary-protect-ziparchive_1 using (Stream stream = File.Open("test.zip", FileMode.Open)) { DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings(); encryptionSettings.Password = "******"; using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, false, null, null, encryptionSettings)) { { // Display the list of the files in the selected zip file using the ZipArchive.Entries property. } } #endregion } }
private void CreateProtectedArchive() { #region radziplibrary-protect-ziparchive_0 using (Stream stream = File.Open("test.zip", FileMode.Create)) { DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings(); encryptionSettings.Password = "******"; using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null, null, encryptionSettings)) { using (ZipArchiveEntry entry = archive.CreateEntry("text.txt")) { StreamWriter writer = new StreamWriter(entry.Open()); writer.WriteLine("Hello world!"); writer.Flush(); } } } #endregion }
private void Decrypt(object obj) { try { using (Stream sourceStream = new MemoryStream()) { sourceStream.Write(this.bytes, 0, this.bytes.Length); sourceStream.Seek(0, SeekOrigin.Begin); DefaultEncryptionSettings encryptionSettings = new DefaultEncryptionSettings(); encryptionSettings.Password = this.DecryptionPassword ?? string.Empty; using (CompressedStream compressedStream = new CompressedStream(sourceStream, StreamOperationMode.Read, new DeflateSettings(), false, encryptionSettings)) { StreamReader reader = new StreamReader(compressedStream); this.Output = reader.ReadToEnd(); } } } catch { this.Output = "Can not decompress with this password"; } }