/// <summary> /// Removes a file from the container at the specified index. /// </summary> /// <param name="index"></param> public void RemoveFile(int index) { this.Files.RemoveAt(index); for (int i = 1; i < this.Files.Count; i++) { this.Files[i].Offset = this.Files[i - 1].Offset + this.Files[i - 1].Length; } if (this.Files.Count == 0) { CFile cfile = new CFile { Name = "The container is empty", Length = 0, Offset = 0, data = AESThenHMAC.SimpleEncryptWithPassword(GetBytes("empty"), this.Key), Dummy = false }; cfile.Length = 0; this.Files.Add(cfile); } this.Save(); if (this.addRandomBuffers) { RemoveDummy(index); } }
/// <summary> /// Adds a new file to the container /// </summary> /// <param name="path"></param> public void AddFile(string path) { if (this.Files.ElementAt(0).Name == "The container is empty") { this.Files.RemoveAt(0); } FileInfo info = new FileInfo(path); CFile cfile = new CFile { Name = info.Name, Length = (int)info.Length, Offset = this.nextOffset, data = AESThenHMAC.SimpleEncryptWithPassword(ReadFileBytes(path), this.Key), Dummy = false }; cfile.Length = cfile.data.Length; this.Files.Add(cfile); this.Save(); nextOffset += (int)cfile.Length; if (this.addRandomBuffers) { int randSize = (int)(cfile.Length * (0.05f + new Random().NextDouble() / 10f)); var data = AESThenHMAC.SimpleEncryptWithPassword(CreateArrayWithRandomContent(randSize), this.Key); var dummy = new CFile { Name = "", Length = data.Length, Offset = this.nextOffset, data = data, Dummy = true }; this.Files.Add(dummy); this.Save(); nextOffset += (int)dummy.Length; } }
/// <summary> /// Saves the current state of the container. /// </summary> public void Save() { byte[] header = GetBytes(FileListToString()); header = AESThenHMAC.SimpleEncryptWithPassword(header, this.Key); if (hiddenContainerLoaded) { header2 = header; } else { header1 = header; } int j = 0; using (FileStream fileStream = new FileStream(this.Path, FileMode.Create)) { var size1 = System.BitConverter.GetBytes(header1.Length); fileStream.Write(size1, 0, size1.Length); var size2 = System.BitConverter.GetBytes(header2.Length); fileStream.Write(size2, 0, size2.Length); Random rand = new Random(); j = 0; // Write the data to the file, byte by byte. for (int i = 0; i < header1.Length; i++) { fileStream.WriteByte(header1[i]); } j += header1.Length; for (int i = j; i < 25000; i++) { fileStream.WriteByte((byte)rand.Next()); } j = 0; for (int i = 0; i < header2.Length; i++) { fileStream.WriteByte(header2[i]); } j += header2.Length; for (int i = j; i < 25000; i++) { fileStream.WriteByte((byte)rand.Next()); } foreach (var cfile in this.Files) { for (int i = 0; i < cfile.data.Length; i++) { fileStream.WriteByte(cfile.data[i]); } } } }
/// <summary> /// Creates a new container /// </summary> /// <param name="filename"></param> public void Create(string filename, bool createHidden = false, string hiddenKey = null) { using (FileStream fs = File.Create(filename)){ } this.Path = filename; if (hiddenKey == null) { hiddenKey = RandomString(32); } this.header2 = AESThenHMAC.SimpleEncryptWithPassword(GetBytes("empty"), hiddenKey); CFile cfile = new CFile { Name = "The container is empty", Length = 0, Offset = 0, data = AESThenHMAC.SimpleEncryptWithPassword(GetBytes("empty"), this.Key), Dummy = false }; cfile.Length = 0; this.Files.Add(cfile); this.Save(); }