Пример #1
0
        public void EnhancedMemoryStream_Byte()
        {
            var es = new EnhancedMemoryStream();

            es.WriteByte(77);
            es.WriteByte(99);

            es.Seek(0, SeekOrigin.Begin);

            Assert.AreEqual(77, es.ReadByte());
            Assert.AreEqual(99, es.ReadByte());
        }
Пример #2
0
        /// <summary>
        /// Decrypts and deserializes a message from a byte buffer.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="sharedKey">The shared encryption key.</param>
        /// <exception cref="FormatException">Thrown if the buffer does not contain a valid message.</exception>
        public DynDnsMessage(byte[] buffer, SymmetricKey sharedKey)
        {
            try
            {
                using (var ms = new EnhancedMemoryStream(Crypto.Decrypt(buffer, sharedKey)))
                {
                    if (ms.ReadInt32Le() != Magic)
                    {
                        throw new Exception();
                    }

                    this.Version = ms.ReadByte();
                    if (this.Version < FormatVer)
                    {
                        throw new FormatException(string.Format("DynDnsMessage version [{0}] is not supported.", this.Version));
                    }

                    this.TimeStampUtc = new DateTime(ms.ReadInt64Le());
                    this.Flags        = (DynDnsMessageFlag)ms.ReadInt32Le();
                    this.HostEntry    = new DynDnsHostEntry(ms.ReadString16());
                }
            }
            catch (Exception e)
            {
                throw new FormatException("Invalid Dynamic DNS message.", e);
            }
        }
Пример #3
0
        public void SecureFile_Stream_Validate()
        {
            string privateKey              = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
            string publicKey               = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);
            EnhancedMemoryStream original  = new EnhancedMemoryStream();
            EnhancedMemoryStream encrypted = new EnhancedMemoryStream();
            SecureFile           secure    = null;
            byte b;

            for (int i = 0; i < 100; i++)
            {
                original.WriteByte((byte)i);
            }

            secure            = new SecureFile(original, SecureFileMode.Encrypt, publicKey);
            original.Position = 0;
            secure.EncryptTo(encrypted, CryptoAlgorithm.AES, 256);
            secure.Close();
            secure = null;

            encrypted.Position = 0;
            Assert.IsTrue(SecureFile.Validate(encrypted, privateKey));

            encrypted.Position = encrypted.Length - 1;
            b = (byte)encrypted.ReadByte();
            encrypted.Position = encrypted.Length - 1;
            encrypted.WriteByte((byte)(~b));

            encrypted.Position = 0;
            Assert.IsFalse(SecureFile.Validate(encrypted, privateKey));
        }
Пример #4
0
        public void Package_HashVerify()
        {
            Package              package;
            EnhancedBlockStream  bs = new EnhancedBlockStream();
            EnhancedMemoryStream es = new EnhancedMemoryStream();
            PackageEntry         entry;

            byte[] buf;
            byte   v;

            buf = new byte[37000];
            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = (byte)i;
            }

            //-------------------------

            package = new Package();
            package.Create(es);

            bs.WriteBytes32(buf);
            bs.Position = 0;

            entry = package.AddFile("/Foo/Bar/Test1.dat", bs, (int)bs.Length);
            Assert.IsTrue(bs.Eof);
            Assert.IsTrue(entry.IsFile);
            Assert.IsTrue(package["/Foo"].IsFolder);
            Assert.IsTrue(package["/Foo/Bar"].IsFolder);
            Assert.IsTrue(package["/Foo/Bar/Test1.dat"].IsFile);

            package.Close(true);

            es.Position = es.Length / 2;  // Corrupt a byte in the middle of the stream
                                          // to verify that the MD5 hash comparision
                                          // catches it.
            v = (byte)es.ReadByte();
            es.Seek(-1, SeekOrigin.Current);
            es.WriteByte((byte)~v);

            es.Position = 0;

            try
            {
                package = new Package(es);
                Assert.Fail();
            }
            catch (PackageException)
            {
            }
        }
Пример #5
0
        public void EnhancedMemoryStream_ToByteArray()
        {
            var es = new EnhancedMemoryStream();

            byte[] buf;

            es.Write(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 0, 10);
            buf = es.ToArray();
            CollectionAssert.AreEqual(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, buf);

            buf[0]      = 255;
            es.Position = 0;
            Assert.AreEqual(0, es.ReadByte());
        }
Пример #6
0
        /// <summary>
        /// Decrypts and deserializes a message from a byte buffer.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <param name="sharedKey">The shared encryption key.</param>
        /// <exception cref="FormatException">Thrown if the buffer does not contain a valid message.</exception>
        public UdpBroadcastMessage(byte[] buffer, SymmetricKey sharedKey)
        {
            try
            {
                using (var ms = new EnhancedMemoryStream(Crypto.Decrypt(buffer, sharedKey)))
                {
                    if (ms.ReadInt32Le() != Magic)
                    {
                        throw new Exception();
                    }

                    this.TimeStampUtc   = new DateTime(ms.ReadInt64Le());
                    this.SourceAddress  = new IPAddress(ms.ReadBytes(4));
                    this.MessageType    = (UdpBroadcastMessageType)ms.ReadByte();
                    this.BroadcastGroup = ms.ReadByte();
                    this.Payload        = ms.ReadBytes16();
                }
            }
            catch (Exception e)
            {
                throw new FormatException("Invalid UDP broadcast message.", e);
            }
        }
Пример #7
0
        public void SecureFile_Stream_BadHash()
        {
            string privateKey              = AsymmetricCrypto.CreatePrivateKey(CryptoAlgorithm.RSA, 1024);
            string publicKey               = AsymmetricCrypto.GetPublicKey(CryptoAlgorithm.RSA, privateKey);
            EnhancedMemoryStream original  = new EnhancedMemoryStream();
            EnhancedMemoryStream encrypted = new EnhancedMemoryStream();
            EnhancedMemoryStream decrypted = new EnhancedMemoryStream();
            SecureFile           secure    = null;
            byte b;

            for (int i = 0; i < 100; i++)
            {
                original.WriteByte((byte)i);
            }

            secure            = new SecureFile(original, SecureFileMode.Encrypt, publicKey);
            original.Position = 0;
            secure.EncryptTo(encrypted, CryptoAlgorithm.AES, 256);
            secure.Close();
            secure = null;

            // Munge the last byte of the hash digest and then
            // confirm the this is detected

            encrypted.Position = encrypted.Length - 1;
            b = (byte)encrypted.ReadByte();
            encrypted.Position = encrypted.Length - 1;
            encrypted.WriteByte((byte)(~b));

            encrypted.Position = 0;
            secure             = new SecureFile(encrypted, SecureFileMode.Decrypt, privateKey);

            try
            {
                secure.DecryptTo(decrypted);
                Assert.Fail("Corrupt hash digest not detected.");
            }
            catch
            {
                // Expecting an exception
            }
        }