void BuildFile(string keyFileLoc, string mapFileLoc, FileHider caller) { try { Stream mapStream = File.Open(mapFileLoc, FileMode.Open); byte[] mapBytes = new byte[mapStream.Length]; mapStream.Read(mapBytes, 0, mapBytes.Length); mapStream.Close(); Stream keyStream = File.Open(keyFileLoc, FileMode.Open); byte[] keyBytes = new byte[keyStream.Length]; keyStream.Read(keyBytes, 0, keyBytes.Length); keyStream.Close(); int extSize = keyBytes[keyBytes.Length - 1]; List<byte> extNums = new List<byte>(); for (int i = keyBytes.Length - extSize - 1; i < keyBytes.Length - 1; i++) extNums.Add(keyBytes[i]); string extention = String.Join("",Encoding.ASCII.GetChars(extNums.ToArray())); int locNumSize = keyBytes[keyBytes.Length - extSize - 2]; List<int> locNums = new List<int>(); for (int i = keyBytes.Length - locNumSize - extSize - 2; i < keyBytes.Length - extSize - 2; i++) locNums.Add(keyBytes[i]); int startNum = ConvertByte(locNums); int startCount = startNum; List<int> locations = new List<int>(); for (int i = 0; i < startNum; i++) { List<int> newInt = new List<int>(); for (int h = startCount; h < keyBytes[i] + startCount; h++) { newInt.Add(keyBytes[h]); } startCount += keyBytes[i]; locations.Add(ConvertByte(newInt)); } byte[] final = new byte[locations.Count]; totalSize = final.Length; completedSize = 0; for (int i = 0; i < final.Length; i++) { final[i] = mapBytes[locations[i]]; completedSize = i; } string fileName = keyFileLoc.Split('.')[0] + "." + extention; File.WriteAllBytes(fileName, final); MessageBox.Show("File Created."); } catch { MessageBox.Show("File failed to be created."); } state = 0; }
void HideFile(string hideFileLoc, string mapFileLoc, FileHider caller) { Stream mapStream = File.Open(mapFileLoc, FileMode.Open); byte[] mapBytes = new byte[mapStream.Length]; mapStream.Read(mapBytes, 0, mapBytes.Length); mapStream.Close(); Stream fileStream = File.Open(hideFileLoc, FileMode.Open); byte[] fileBytes = new byte[fileStream.Length]; fileStream.Read(fileBytes, 0, fileBytes.Length); fileStream.Close(); List<List<int>> byteLocations = new List<List<int>>(); for (int i = 0; i < 256; i++) { byteLocations.Add(new List<int>()); for (int h = 0; h < mapBytes.Length; h++) { if (mapBytes[h] == i) { byteLocations[i].Add(h); } } } List<int> missingBytes = new List<int>(); for (int i = 0; i < byteLocations.Count; i++) { if (byteLocations[i].Count == 0) { missingBytes.Add(i); } } bool validMap = true; if (missingBytes.Count != 0) { for (int i = 0; i < missingBytes.Count && validMap; i++) { for (int h = 0; h < fileBytes.Length && validMap; h++) { if (missingBytes[i] == fileBytes[h]) { validMap = false; } } } } if (validMap) { List<int> locations = new List<int>(); List<int> rawData = new List<int>(); completedSize = 0; totalSize = fileBytes.Length; foreach (byte b in fileBytes) { completedSize++; List<int> newBytes = ConvertDec(FindLocation(byteLocations, b)); locations.Add(newBytes.Count); foreach (int i in newBytes) rawData.Add(i); } List<int> locSize = ConvertDec(locations.Count); foreach (int i in locSize) rawData.Add(i); rawData.Add(locSize.Count); string fileName = ""; string extention = ""; if (hideFileLoc.Split('.').Length > 1) { fileName = hideFileLoc.Split('.')[0] + ".fkey"; extention = hideFileLoc.Split('.')[1]; byte[] extBytes = ASCIIEncoding.ASCII.GetBytes(extention); foreach (byte b in extBytes) rawData.Add(b); rawData.Add(extBytes.Length); } else { fileName = hideFileLoc + ".fkey"; rawData.Add(0); } foreach (int i in rawData) locations.Add(i); byte[] final = new byte[locations.Count]; for (int i = 0; i < locations.Count; i++) final[i] = (byte)locations[i]; File.WriteAllBytes(fileName, final); MessageBox.Show("Key Created."); } else { MessageBox.Show("Map is not sufficant."); } state = 0; }