Esempio n. 1
0
        public void Deserialize(IReceiveContext context)
        {
            _wrappedSerializer.Deserialize(context);
            IConsumeContext <EncryptedMessageEnvelope> encryptedContext;

            context.TryGetContext(out encryptedContext);

            if (encryptedContext == null)
            {
                throw new SerializationException("Could not deserialize message.");
            }


            byte[] cipherBytes = Convert.FromBase64String(encryptedContext.Message.CipheredMessage);
            byte[] iv          = Convert.FromBase64String(encryptedContext.Message.Iv);

            var cipherStream = new EncryptedStream(cipherBytes, iv);

            using (ICryptographyService cryptographyService = new RijndaelCryptographyService(_key))
            {
                Stream clearStream = cryptographyService.Decrypt(cipherStream);

                context.SetBodyStream(clearStream);

                _wrappedSerializer.Deserialize(context);
            }
        }
        public object Deserialize(Stream input)
        {
            object message = _xmlSerializer.Deserialize(input);

            if (message == null)
            {
                throw new SerializationException("Could not deserialize message.");
            }

            if (message is EncryptedMessageEnvelope)
            {
                var envelope = message as EncryptedMessageEnvelope;

                var cipherBytes = Convert.FromBase64String(envelope.CipheredMessage);
                var iv          = Convert.FromBase64String(envelope.Iv);

                var cipherStream = new EncryptedStream(cipherBytes, iv);
                using (ICryptographyService cryptographyService = new RijndaelCryptographyService(_key))
                {
                    var clearStream = cryptographyService.Decrypt(cipherStream);

                    return(_xmlSerializer.Deserialize(clearStream));
                }
            }
            return(message);
        }
Esempio n. 3
0
        public void SecureReadWriteTest()
        {
            var location = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            using (var stream1 = new MemoryStream())
                using (var stream2 = new MemoryStream())
                    using (var requestSend = new EncryptedStream(stream1, canDispose: false))
                        using (var requestReceive = new EncryptedStream(stream2, canDispose: false))
                        {
                            byte[] data = Encoding.UTF8.GetBytes("changeit");
                            requestSend.Write(data, 0, data.Length);
                            requestSend.Flush();

                            var streamData = stream1.ToArray();
                            Console.WriteLine(string.Join(" ", data.Select(m => $"{m:X2}").ToArray()));

                            Console.WriteLine(streamData.Length);
                            Console.WriteLine(string.Join(" ", streamData.Select(m => $"{m:X2}").ToArray()));

                            TransferData(requestSend, requestReceive);
                            byte[] data1 = new byte[data.Length];
                            int    count = requestReceive.Read(data1, 0, data1.Length);
                            Assert.AreEqual(data.Length, count);

                            var str = Encoding.UTF8.GetString(data1);
                            Assert.AreEqual("changeit", str);
                        }
        }
        public void Serialize <T>(Stream output, T message)
        {
            try
            {
                using (var clearStream = new MemoryStream())
                {
                    _xmlSerializer.Serialize(clearStream, message);

                    clearStream.Seek(0, SeekOrigin.Begin);

                    using (ICryptographyService cryptographyService = new RijndaelCryptographyService(_key))
                    {
                        EncryptedStream encryptedStream = cryptographyService.Encrypt(clearStream);

                        var encryptedMessage = new EncryptedMessageEnvelope
                        {
                            CipheredMessage = Convert.ToBase64String(encryptedStream.GetBytes()),
                            Iv = Convert.ToBase64String(encryptedStream.Iv),
                        };

                        _xmlSerializer.Serialize(output, encryptedMessage);
                    }
                }
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            IStream stream = new EncryptedStream(new CompressedStream(new Stream()));

            stream.Write("stream");

            Console.ReadKey();
        }
Esempio n. 6
0
        public void SaveEncryptedStreamToFile()
        {
            // Setup
            var stream = new EncryptedStream("FileToRead.txt");
            var newFilename = "FileToWrite.txt";

            // Act
            FileManager.StreamToFile(stream, newFilename);
        }
Esempio n. 7
0
        internal static void Save(string path, Account account, Settings settings)
        {
            Precondition.NotNull(account);
            Precondition.NotNull(settings);

            Directory.CreateDirectory(Path.GetDirectoryName(path) ?? "");
            using (var file = File.Open(path, FileMode.Create))
                using (var cs = EncryptedStream.CreateEncryptionStream(key, file))
                    using (var bw = new BinaryWriter(cs)) {
                        account.WriteTo(bw);
                        settings.WriteTo(bw);
                    }
        }
Esempio n. 8
0
        internal static Tuple <Account, Settings> Load(string path)
        {
            if (!File.Exists(path))
            {
                return(null);
            }

            using (var file = File.Open(path, FileMode.Open))
                using (var cs = EncryptedStream.CreateDecryptionStream(key, file))
                    using (var br = new BinaryReader(cs)) {
                        var account  = new Account(br);
                        var settings = new Settings(br);
                        return(new Tuple <Account, Settings>(account, settings));
                    }
        }
        public void Serialize <T>(Stream output, ISendContext <T> context)
            where T : class
        {
            try
            {
                using (var clearStream = new MemoryStream())
                {
                    _wrappedSerializer.Serialize(clearStream, context);

                    using (var readStream = new MemoryStream(clearStream.ToArray(), false))
                    {
                        using (ICryptographyService cryptographyService = new RijndaelCryptographyService(_key))
                        {
                            EncryptedStream encryptedStream = cryptographyService.Encrypt(readStream);

                            var encryptedMessage = new EncryptedMessageEnvelope
                            {
                                CipheredMessage = Convert.ToBase64String(encryptedStream.GetBytes()),
                                Iv = Convert.ToBase64String(encryptedStream.Iv),
                            };

                            // Encrypt message and set context
                            var encryptedContext = new SendContext <EncryptedMessageEnvelope>(encryptedMessage);
                            encryptedContext.SetUsing(context);
                            encryptedContext.SetMessageType(typeof(EncryptedMessageEnvelope));

                            // Serialize secure message to output
                            _wrappedSerializer.Serialize(output, encryptedContext);

                            // Set the encrypted context back into the send context
                            encryptedContext.SetContentType(ContentTypeHeaderValue);
                            context.SetUsing(encryptedContext);
                        }
                    }
                }
            }
            catch (SerializationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SerializationException("Failed to serialize message", ex);
            }
        }
        public void EncryptedStream_DecryptedStream_ReturnSameValue()
        {
            var stream          = new MemoryStream(source.ToByteArray());
            var encryptedStream = EncryptedStream.Create(stream, "123456", "ABCDEFGHIJK");
            var encryptedData   = encryptedStream.ReadToByteArray();

            encryptedStream.Dispose();

            stream = new MemoryStream(encryptedData);
            var decryptedStream = DecryptedStream.Create(stream, "123456", "ABCDEFGHIJK", (int)stream.Length);
            var decryptedData   = decryptedStream.ReadToByteArray();

            decryptedStream.Dispose();

            var result = decryptedData.To <string>();

            Assert.Equal(result, source);
        }
Esempio n. 11
0
        public void TransferData(EncryptedStream source, EncryptedStream target)
        {
            var pos = target.Position;

            source.Position = 0;

            Stream a = source.GetRawStream();
            Stream b = target.GetRawStream();

            byte[] buf = new byte[1024];
            while (true)
            {
                int nbytes = a.Read(buf, 0, buf.Length);
                if (nbytes == 0)
                {
                    break;
                }

                b.Write(buf, 0, nbytes);
            }
            source.SetLength(0);

            target.Position = pos;
        }