private void SaveData(IStream stream) { // Create a memory buffer and write the strings there using (var buffer = new MemoryStream()) using (var bufferWriter = new EndianWriter(buffer, stream.Endianness)) { // Write the strings to the buffer foreach (string str in _strings) { if (str != null) { bufferWriter.WriteAscii(str); } } // Align the buffer's length if encryption is necessary if (_key != null) { buffer.SetLength(AES.AlignSize((int)buffer.Length)); } byte[] data = buffer.ToArray(); // Encrypt the buffer if necessary if (_key != null) { data = AES.Encrypt(data, 0, (int)buffer.Length, _key.Key, _key.IV); } // Resize the data area and write it in _data.Resize((int)buffer.Length, stream); stream.SeekTo(_data.Offset); stream.WriteBlock(data, 0, (int)buffer.Length); } }
private IReader DecryptData(IReader reader, FileSegment dataLocation, AESKey key) { reader.SeekTo(dataLocation.Offset); byte[] data = reader.ReadBlock(AES.AlignSize(dataLocation.Size)); if (key != null) { data = AES.Decrypt(data, key.Key, key.IV); } return(new EndianReader(new MemoryStream(data), Endian.BigEndian)); }