public void TestExistingFileReader() { string tempFile = DirectoryHelper.GetNewTempFilePath(); FileStream tempWriter = new FileStream(tempFile, FileMode.CreateNew); tempWriter.Write(ShortData, 0, ShortData.Length); tempWriter.Write(LongData, 0, LongData.Length); tempWriter.Close(); CStreamWriter cstream = new CStreamWriter(tempFile); CStreamReader reader = cstream.CreateReader(); byte[] buf = new byte[ShortData.Length]; reader.ReadFully(buf); Assert.IsTrue(buf.SequenceEqual(ShortData)); buf = new byte[LongData.Length]; reader.ReadFully(buf); Assert.IsTrue(buf.SequenceEqual(LongData)); // force gc reader = null; cstream = null; GC.Collect(); GC.WaitForPendingFinalizers(); File.Delete(tempFile); }
public void TestSeek() { CStreamWriter writer = new CStreamWriter(); writer.Write(LongData); writer.Close(); CStreamReader reader = writer.CreateReader(); byte[] buf = new byte[1024]; { // seek 5 bytes before EOF, attempt to read much, get 5 bytes reader.Seek(LongData.Length - 5, SeekOrigin.Begin); int read = reader.Read(buf, 0, int.MaxValue); Assert.AreEqual(5, read); } { // read EOF int read = reader.Read(buf, 0, int.MaxValue); Assert.AreEqual(0, read); } { // seek beyond stream length, read EOF reader.Seek(LongData.Length * 3, SeekOrigin.Begin); int read = reader.Read(buf, 0, int.MaxValue); Assert.AreEqual(0, read); } { // seek back, read again reader.Seek(LongData.Length - 5, SeekOrigin.Begin); int read = reader.Read(buf, 0, int.MaxValue); Assert.AreEqual(5, read); } }
public void TestSeekSwap() { CStreamWriter writer = new CStreamWriter(); writer.Write(LongData); CStreamReader reader = writer.CreateReader(); byte[] buf = new byte[1024]; // seek 5 bytes before EOF reader.Seek(LongData.Length - 5, SeekOrigin.Begin); // write more, ensure swap writer.Write(LongData); writer.Write(LongData); Assert.IsTrue(writer.IsSwapped); { // seek somewhere to the middle, read reader.Seek(LongData.Length * 2, SeekOrigin.Begin); int read = reader.Read(buf); Assert.AreEqual(buf.Length, read); } { // seek beyond length, assert still open, get EOF reader.Seek(LongData.Length * 3, SeekOrigin.Current); Assert.IsFalse(writer.IsClosed); int read = reader.Read(buf); Assert.AreEqual(0, read); } }
public void TestSwapWithReader() { CStreamWriter writer = new CStreamWriter(); CStreamReader reader = writer.CreateReader(); // write, not swapped writer.Write(LongData); Assert.IsFalse(writer.IsSwapped); // read a few bytes, but there are still a few bytes left byte[] buf = new byte[ShortData.Length]; reader.Read(buf); Assert.IsTrue(reader.Position > 0); Assert.IsTrue(reader.Length > reader.Position); // fill buffer, assert swap writer.Write(LongData); writer.Write(LongData); Assert.IsTrue(writer.IsSwapped); Assert.IsTrue(reader.IsSwapped); // try to read more than available, receive less buf = new byte[writer.Length * 2]; int read = reader.Read(buf); Assert.IsTrue(read < buf.Length); // close writer, assert EOF writer.Close(); int result = reader.ReadByte(); Assert.AreEqual(-1, result); }
public void TestReader() { CStreamWriter writer = new CStreamWriter(); // put 6 bytes writer.Write(ShortData); CStreamReader reader = writer.CreateReader(); // length==6, position==0 Assert.AreEqual(0, reader.Position); Assert.AreEqual(ShortData.Length, reader.Length); // try to read more bytes than available byte[] buf = new byte[ShortData.Length * 1000]; Assert.AreNotEqual(buf.Length, ShortData.Length); Assert.IsFalse(buf.SequenceEqual(ShortData)); int read = reader.Read(buf); Assert.AreEqual(ShortData.Length, read); // assert the first few bytes are still correct byte[] buf2 = new byte[ShortData.Length]; Array.Copy(buf, buf2, buf2.Length); Assert.IsTrue(buf2.SequenceEqual(ShortData)); // not swapped Assert.IsFalse(reader.IsSwapped); }
public void TestDestructorWithReader() { CStreamWriter writer = new CStreamWriter(); // write, not swapped writer.Write(LongData); // read something and assert there's more CStreamReader reader = writer.CreateReader(); byte[] buf = new byte[ShortData.Length]; reader.Read(buf); Assert.IsTrue(reader.Position > 0); Assert.IsTrue(reader.Length > reader.Position); Assert.IsFalse(reader.IsSwapped); // write more, assert swap writer.Write(LongData); writer.Write(LongData); Assert.IsTrue(reader.IsSwapped); string filePath = writer.FilePath; // destroy ref to writer writer.Close(); writer = null; GC.Collect(); GC.WaitForPendingFinalizers(); Assert.IsNull(writer); // assert reading still works Assert.IsTrue(File.Exists(filePath)); Assert.IsNotNull(reader); int sum = 0; while (sum < LongData.Length * 2) { int read = reader.Read(buf); Assert.IsTrue(read > 0); sum += read; } // destroy reader reader = null; GC.Collect(); GC.WaitForPendingFinalizers(); // deleted tempfile Assert.IsFalse(File.Exists(filePath)); }
public void TestExhaustiveRead() { CStreamWriter writer = new CStreamWriter(); writer.Write(LongData); writer.Write(LongData); writer.Write(LongData); writer.Close(); CStreamReader reader = writer.CreateReader(); byte[] bigbuf = reader.ReadFully(); Assert.AreEqual(LongData.Length * 3, bigbuf.Length); }
private void Encrypt() { int length = settings.BlockKeyLen, bytesRead = 0; int roundMax = (int)(inputStreamReader.Length / length) + 1; List <byte> xor = new List <byte>(); List <byte> index = new List <byte>(); List <byte> ciphertext = new List <byte>(); byte[] plaintext = new byte[length]; matrixKey = new byte[passwordBytes.Length]; Buffer.BlockCopy(passwordBytes, 0, matrixKey, 0, passwordBytes.Length); int round = 1; while ((bytesRead = inputStreamReader.ReadFully(plaintext)) > 0) { if (bytesRead < length) { // in der letzten Runde Padding durch hinzufügen von Leerzeichen bis der Puffer voll ist for (int i = bytesRead; i < plaintext.Length; i++) { plaintext[i] = 0x20; } } // Schlüssel generieren Generator(round); // Verschlüsseln // 1. Klartext XOR Blockschlüssel for (int i = 0; i < length; i++) { xor.Add((byte)(plaintext[i] ^ blockKey[i])); } // bit conversation int puffer = 0; int bitCount = 0; for (int i = 0; i < length; i++) { puffer <<= 8; // mache für die nächsten 8 Bits Platz puffer |= xor[i]; // schreibe die nächsten 8 Bits in den Puffer bitCount += 8; // bitCount als Zähler für die Bits im Puffer, erhöhe um 8 index.Add((byte)(puffer >> (bitCount - 7) & 0x7F)); // lies die obersten 7 Bits aus bitCount -= 7; // verringere um 7, da 7 Bits ausgelesen wurden // aus Performancegründen werden die gelesenen Bits nicht gelöscht if (bitCount == 7) { index.Add((byte)(puffer & 0x7F)); // haben sich 7 Bits angesammelt, so ließ sie aus bitCount = 0; // 7 Bits gelesen, 7 - 7 = 0 } } switch (bitCount) { case 1: index.Add((byte)(puffer & 0x01)); break; case 2: index.Add((byte)(puffer & 0x03)); break; case 3: index.Add((byte)(puffer & 0x07)); break; case 4: index.Add((byte)(puffer & 0x0F)); break; case 5: index.Add((byte)(puffer & 0x1F)); break; case 6: index.Add((byte)(puffer & 0x3F)); break; default: break; } // Abbildung auf Chiffre-Alphabet for (int i = 0; i < index.Count; i++) { outputStreamWriter.WriteByte(cipherChars[index[i]]); } // Debugdaten schreiben if (settings.Debug) { WriteDebug("\r\n\r\nplaintext (hex): \r\n "); foreach (byte b in plaintext) { WriteDebug(String.Format(" {0:X2}", b)); } WriteDebug("\r\n\r\nplaintext xor block key (hex): \r\n "); xor.ForEach(delegate(byte b) { WriteDebug(String.Format(" {0:X2}", b)); }); WriteDebug("\r\n\r\nindex: \r\n "); index.ForEach(delegate(byte b) { WriteDebug(String.Format(" {0}", b)); }); WriteDebug("\r\n\r\nciphertext (hex): \r\n "); CStreamReader reader = outputStreamWriter.CreateReader(); int start = settings.BlockKeyLen * (round - 1); reader.Seek(start, SeekOrigin.Begin); for (int i = 0; i < index.Count; i++) { WriteDebug(String.Format(" {0:X2}", (byte)reader.ReadByte())); } WriteDebug("\r\n\r\n>>>>>>>>>> END OF ROUND <<<<<<<<<<\r\n\r\n\r\n"); } // Vorbereitungen für die nächste Runde cipherChars.Clear(); blockKey.Clear(); xor.Clear(); index.Clear(); if (stop) { break; } roundMax = (int)(inputStreamReader.Length / length) + 1; ProgressChanged(round, roundMax); round++; } }