Пример #1
0
        /// <summary>
        /// Read a specified clipboard data, encrypt and save it into a file.
        /// </summary>
        /// <param name="clipboardReader">The <see cref="ClipboardReader"/> used to read the data.</param>
        /// <param name="identifier">The data identifier.</param>
        /// <param name="clipboardDataPath">The full path to the clipboard data folder.</param>
        private void WriteClipboardDataToFile(ClipboardReader clipboardReader, DataIdentifier identifier, string clipboardDataPath)
        {
            Requires.NotNull(clipboardReader, nameof(clipboardReader));
            Requires.NotNull(identifier, nameof(identifier));
            Requires.NotNullOrWhiteSpace(clipboardDataPath, nameof(clipboardDataPath));

            var dataFilePath = Path.Combine(clipboardDataPath, $"{identifier.Identifier}.dat");

            if (File.Exists(dataFilePath))
            {
                Logger.Instance.Fatal(new FileLoadException($"The file {dataFilePath} already exists."));
            }

            var dataPassword = SecurityHelper.ToSecureString(SecurityHelper.EncryptString(SecurityHelper.ToSecureString(identifier.Identifier.ToString())));

            Requires.NotNull(dataPassword, nameof(dataPassword));

            clipboardReader.BeginRead(identifier.FormatName);
            Requires.IsTrue(clipboardReader.IsReadable);
            Requires.IsTrue(clipboardReader.CanReadNextBlock());

            using (var fileStream = File.OpenWrite(dataFilePath))
                using (var aesStream = new AesStream(fileStream, dataPassword, SecurityHelper.GetSaltKeys(dataPassword).GetBytes(16)))
                {
                    aesStream.AutoDisposeBaseStream = false;
                    while (clipboardReader.CanReadNextBlock())
                    {
                        var buffer = clipboardReader.ReadNextBlock();
                        aesStream.Write(buffer, 0, buffer.Length);
                    }
                    aesStream.Position = 0;
                }

            clipboardReader.EndRead();
        }
Пример #2
0
        public void ClipboardWriterAndReader()
        {
            using (var clipboardWriter = new ClipboardWriter())
            {
                var value = new MemoryStream(new byte[] { 72, 101, 108, 108, 111 }); // "Hello" in binary

                clipboardWriter.AddData("ASCIITextTest", value);

                clipboardWriter.Flush();
            }

            var dataFromClipboard = (MemoryStream)System.Windows.Clipboard.GetData("ASCIITextTest");

            Assert.IsTrue(dataFromClipboard.ToArray().SequenceEqual(new byte[] { 72, 101, 108, 108, 111 }));

            var dataFromClipboard2 = new MemoryStream();
            var dataObject         = (System.Windows.DataObject)System.Windows.Clipboard.GetDataObject();

            using (var clipboardReader = new ClipboardReader(dataObject))
            {
                Assert.IsFalse(clipboardReader.IsReadable);
                Assert.IsFalse(clipboardReader.CanReadNextBlock());

                foreach (var format in dataObject.GetFormats())
                {
                    clipboardReader.BeginRead(format);
                    Assert.IsTrue(clipboardReader.IsReadable);
                    Assert.IsTrue(clipboardReader.CanReadNextBlock());

                    while (clipboardReader.CanReadNextBlock())
                    {
                        var buffer = clipboardReader.ReadNextBlock();
                        dataFromClipboard2.Write(buffer, 0, buffer.Length);
                    }

                    clipboardReader.EndRead();
                }
            }

            Assert.IsTrue(dataFromClipboard2.ToArray().SequenceEqual(new byte[] { 72, 101, 108, 108, 111 }));
        }