public static bool SaveHashKey(byte[] hash, byte[] key) { byte[] allBytes = hash.Concat(key).ToArray(); using (FileStream newSource = new FileStream(ConstantKeeper.PathToKeys, FileMode.Create, FileAccess.Write)) { try { var bytes = NotebookCryptography.ProtectHashKey(allBytes, ConstantKeeper.Entropy); newSource.Write(bytes, 0, bytes.Length); } catch (CryptographicException) { return(false); } return(true); } }
public static byte[] ReadHashKey(bool getHash) { byte[] bytes; using (FileStream fsSource = new FileStream(ConstantKeeper.PathToKeys, FileMode.Open, FileAccess.Read)) { bytes = new byte[fsSource.Length]; int numBytesToRead = (int)fsSource.Length; int numBytesRead = 0; while (numBytesToRead > 0) { int n = fsSource.Read(bytes, numBytesRead, numBytesToRead); if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } } bytes = NotebookCryptography.UnprotectHashKey(bytes, ConstantKeeper.Entropy); if (getHash) { Array.Resize(ref bytes, 32); return(bytes); } else { byte[] key = new byte[bytes.Length - 32]; for (int i = 32, j = 0; i < bytes.Length; i++, j++) { key[j] = bytes[i]; } return(key); } }
public void SerializeNotes() { var binFormatter = new BinaryFormatter(); byte[] serialized; using (var memory = new MemoryStream()) { binFormatter.Serialize(memory, _notes); serialized = memory.ToArray(); } byte[] toFile = NotebookCryptography.Encode( serialized, ConstantKeeper.Key, ConstantKeeper.IV); NotebookModelIO.SaveNotesIV(toFile); }
public void DeSerializeNotes() { byte[] readBytes = NotebookModelIO.ReadNotesIV(); byte[] decodedBytes = NotebookCryptography.Decode( readBytes, ConstantKeeper.Key, ConstantKeeper.IV ); var binFormatter = new BinaryFormatter(); using (var memory = new MemoryStream(decodedBytes)) { var noteList = binFormatter.Deserialize(memory) as List <Note>; if (noteList != null) { _notes = noteList; } } }