// Archiwizuje wskazany po ścieżce plik public void ArchiveFile(string path) { if (!IsOpen) { throw new Exception("Archiwum nieotwarte"); } if (!ArchiveKey.IsDerived) { throw new Exception("Klucz niewyprowadzony"); } if (!File.Exists(path)) { throw new Exception("Plik pod wskazaną lokalizacją nie istnieje"); } FileData file = FileData.Create(path); string plaintextName = file.NameStr; if (!FileNameUnique(plaintextName)) { throw new FileNamingException(plaintextName); } if (IsCompressed) { FileCompressor.CompressFile(file); } FileEncryptor.EncryptFile(file, ArchiveKey); SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "INSERT INTO FileData(name_l, name, content_l, content, iv) VALUES" + "(" + "'" + Convert.ToString(file.NameLength) + "', " + "@Name, " + "'" + Convert.ToString(file.ContentLength) + "', " + "@Content, " + "@IV" + ")"; command.Parameters.Add(new SQLiteParameter("Name", file.Name)); command.Parameters.Add(new SQLiteParameter("Content", file.Content)); command.Parameters.Add(new SQLiteParameter("IV", file.IV)); command.ExecuteNonQuery(); command.CommandText = "SELECT last_insert_rowid();"; long lastRowId = (long)command.ExecuteScalar(); int fileId = (int)lastRowId; // Przechowanie pary id_pliku-nazwa_pliku IdFileNamePairs.Add(fileId, plaintextName); }
// Usuwa wszystkie pliki z archiwum public void DeleteAll() { SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "DELETE FROM FileData;"; command.ExecuteNonQuery(); IdFileNamePairs.Clear(); }
/* * Metody publiczne realizujące operacje na otwartym archiwum */ // Zamyka otwarte archiwum public void Close() { if (IsOpen) { Connection.Close(); ArchiveKey.Reset(); FilePath = ""; IdFileNamePairs.Clear(); } }
// Wczytuje z bazy zaszyfrowane nazwy plików (razem z id) i odszyfrowuje je private void SelectPlaintextFileNames() { if (!IsOpen) { throw new Exception("Archiwum nieotwarte"); } if (!ArchiveKey.IsDerived) { throw new Exception("Klucz niewyprowadzony"); } SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "SELECT id, name_l, length(name), name, iv FROM FileData;"; SQLiteDataReader reader = command.ExecuteReader(); if (reader.HasRows) { IdFileNamePairs.Clear(); } while (reader.Read()) { int id = reader.GetInt32(0); int nameLength = reader.GetInt32(1); int nameBlobLength = reader.GetInt32(2); byte[] name_bytes = new byte[nameBlobLength]; reader.GetBytes(3, 0, name_bytes, 0, nameBlobLength); byte[] iv = new byte[EncryptionKey.BlockLength]; reader.GetBytes(4, 0, iv, 0, EncryptionKey.BlockLength); name_bytes = Encryptor.DecryptBytes(name_bytes, ArchiveKey.Value, iv); byte[] n = new byte[nameLength]; for (int i = 0; i < nameLength; i++) { n[i] = name_bytes[i]; } string name = Encoding.ASCII.GetString(n); IdFileNamePairs.Add(id, name); } }
// Usuwa z archiwum wskazany po identyfikatorze plik public void Delete(int id) { if (!IsOpen) { throw new Exception("Archiwum nieotwarte"); } if (!ArchiveKey.IsDerived) { throw new Exception("Klucz niewyprowadzony"); } SQLiteCommand command = new SQLiteCommand(Connection); command.CommandText = "DELETE FROM FileData WHERE id='" + Convert.ToString(id) + "';"; command.ExecuteNonQuery(); IdFileNamePairs.Remove(id); }