Example #1
0
        /// <summary>
        /// Puts message into container.
        /// </summary>
        /// <param name="container">Container.</param>
        /// <param name="message">Message.</param>
        public void Embed(Container container, byte[] message)
        {
            if (!container.ContentType.Contains(Constants.IMAGE_CONTENT_TYPE))
                throw new ArgumentException("LSB is only works with image containers.");

            Bitmap bitmap = new Bitmap(container.InputStream);

            if (bitmap.Width * bitmap.Height < message.Length << 3)
                throw new ArgumentException("Message is to big for this container.");

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    int number = x + y * bitmap.Width;

                    if (number >= message.Length << 3)
                    {
                        container.InputStream = SaveBitmap(bitmap, FormatFactory.GetInstance(container.ContentType));
                        return;
                    }
                    bitmap.SetPixel(x, y, insertDataIntoPixel(number, bitmap.GetPixel(x, y), message));
                }
            }

            container.InputStream = SaveBitmap(bitmap, FormatFactory.GetInstance(container.ContentType));
        }
Example #2
0
        /// <summary>
        /// Gets message from container.
        /// </summary>
        /// <param name="container">Container.</param>
        /// <param name="key">Key.</param>
        /// <returns>Hidden message.</returns>
        public byte[] Extract(Container container, Key key)
        {
            if (!container.ContentType.Contains(Constants.IMAGE_CONTENT_TYPE))
                throw new ArgumentException("LSB is only works with image containers.");

            Bitmap bitmap = new Bitmap(container.InputStream);
            byte[] bytes = new byte[key.MessageLength];

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    int number = x + y * bitmap.Width;

                    if (number >= key.MessageLength << 3)
                    {
                        return bytes;
                    }

                    Color pixel = bitmap.GetPixel(x, y);
                    extractDataFromPixel(number, pixel, bytes);
                }
            }

            return bytes;
        }
        public static string ExtractDwm(IEmbeder embeder, ICryptor cryptor, Key key, Container container)
        {
            byte[] byteMessage = embeder.Extract(container, key);
            Stream message = cryptor.Decrypt(StreamHelper.BytesToStream(byteMessage), key);

            return StreamHelper.StreamToString(message);
        }
Example #4
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!File.Exists(ContainerTextBox.Text))
            {
                MessageBox.Show("Container is not selected.");
                return;
            }

            try
            {
                EmbedingOptions options = GetOptions();

                //Inject dependencies
                CryptType cryptType = (CryptType)System.Enum.Parse(typeof(CryptType), CryptTypeComboBox.Text);
                EmbedType embedType = (EmbedType)System.Enum.Parse(typeof(EmbedType), EmbedType.Text);
                ICryptor cryptor = _algorithmFactory.GetInstance(cryptType);
                IEmbeder embeder = _embederFactory.GetInstance((EmbedType)System.Enum.Parse(typeof(EmbedType), EmbedType.Text), GetOptions());

                //Create container and key
                string contentType = DefineContentType(ContainerTextBox.Text);
                Container container = new Container(File.Open(ContainerTextBox.Text, FileMode.Open), contentType);
                StenoCryptor.Commons.Key key = KeyMakerHelper.GenerateKey(container, MessageTextBox.Text, cryptType, embedType, cryptor, KeyTextBox.Text);

                //Embed DWM
                DwmProcessorHelper.EmbedDwm(cryptor, embeder, MessageTextBox.Text, key, container);

                if (options.HasFlag(EmbedingOptions.EmbedKey))
                {
                    //Prepare streams to be saved to the files
                    Stream keyStream = SerializeHelper.SerializeBinary(key);
                    StreamHelper.SaveFile(keyStream, Constants.DEFAULT_KEY_NAME);

                    StreamHelper.SaveFile(container.InputStream, Path.GetFileName(ContainerTextBox.Text));
                }
                else
                {
                    //Prepare streams to be saved to the files
                    Stream keyStream = SerializeHelper.SerializeBinary(key);
                    Dictionary<string, Stream> files = new Dictionary<string, Stream>();

                    files.Add(Path.GetFileName(ContainerTextBox.Text), container.InputStream);
                    files.Add(Constants.DEFAULT_KEY_NAME, keyStream);

                    //Packing files to the zip file
                    string zipFileName = ZipHelper.CompressFiles(files);

                    SaveFile(zipFileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #5
0
        public static Key GenerateKey(Container container, string message, CryptType cryptType, EmbedType embedType, IKeyAware keyParser, string stringkey)
        {
            if (!keyParser.ValidateKey(stringkey))
                throw new ArgumentException("Key is not valid.");

            Key key = new Key();
            key.CryptType = cryptType;
            key.EmbedType = embedType;
            key.MessageLength = Constants.DEFAULT_ENCODING.GetByteCount(message);
            key.Value = keyParser.ParseKey(stringkey);

            return key;
        }
Example #6
0
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            if ((!File.Exists(ExtractKeyTextBox.Text)) && !KeyIsIntoImageChechBox.IsChecked.Value)
            {
                MessageBox.Show("Select key!");
                return;
            }
            if (!File.Exists(ExtractContainerTextBox.Text))
            {
                MessageBox.Show("Select container!");
                return;
            }

            try
            {
                StenoCryptor.Commons.Key key = KeyIsIntoImageChechBox.IsChecked.Value ?
                        SerializeHelper.DeserializeBinary(File.Open(Constants.DEFAULT_KEY_NAME, FileMode.Open)) as StenoCryptor.Commons.Key :
                        SerializeHelper.DeserializeBinary(File.Open(ExtractKeyTextBox.Text, FileMode.Open)) as StenoCryptor.Commons.Key;
                if (key == null)
                {
                    MessageBox.Show("Key is not correct!");
                    return;
                }

                IEmbeder embeder = _embederFactory.GetInstance(key.EmbedType, GetOptions());
                ICryptor cryptor = _algorithmFactory.GetInstance(key.CryptType);
                Stream containerStream = DesktopCheckBox.IsChecked.Value ? File.Open(Path.GetFileName(ExtractContainerTextBox.Text), FileMode.Open) : File.Open(ExtractContainerTextBox.Text, FileMode.Open);
                Container container = new Container(containerStream, DefineContentType(ExtractContainerTextBox.Text));
                string message = DwmProcessorHelper.ExtractDwm(embeder, cryptor, key, container);

                ResultTextBlock.Text = message;
                ResultTextBlock.Visibility = System.Windows.Visibility.Visible;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #7
0
        public ActionResult Embed(Models.EmbedDwmModel model, HttpPostedFileBase photoFile)
        {
            if (photoFile == null)
                ModelState.AddModelError("photoFile", Localization.Views.Shared.FileIsNotSelected);

            if (ModelState.IsValid)
            {
                try
                {
                    //Inject dependencies
                    ICryptor cryptor = _algorithmFactory.GetInstance(model.CryptType);
                    IEmbeder embeder = _embederFactory.GetInstance(model.EmbedType, Enums.Commons.EmbedingOptions.UseAlpha);

                    //Create container and key
                    Container container = new Container(photoFile.InputStream, photoFile.ContentType);
                    Key key = KeyMakerHelper.GenerateKey(container, model.Message, model.CryptType, model.EmbedType, cryptor, model.Key);

                    //Embed DWM
                    DwmProcessorHelper.EmbedDwm(cryptor, embeder, model.Message, key, container);

                    //Prepare streams to be saved to the files
                    Stream keyStream = SerializeHelper.SerializeBinary(key);
                    Dictionary<string, Stream> files = new Dictionary<string, Stream>();

                    files.Add(Path.GetFileName(photoFile.FileName), container.InputStream);
                    files.Add(Constants.DEFAULT_KEY_NAME, keyStream);

                    //Packing files to the zip file
                    string zipFileName = ZipHelper.CompressFiles(files);
                    TempData[TempDataKeys.FILE_NAME] = new FileModel(zipFileName, photoFile.ContentType);

                    return RedirectToAction(HomeController.EMBED_RESULT);
                }
                catch (Exception ex)
                {
                    TempData[TempDataKeys.ERROR] = ex.Message;
                    return View(SharedController.ERROR);
                }
            }

            return View();
        }
Example #8
0
        public ActionResult Extract(HttpPostedFileBase photoFile, HttpPostedFileBase keyFile)
        {
            if (photoFile == null || keyFile == null)
                ModelState.AddModelError("photoFile", Localization.Views.Home.ErrPostFileIsNullOrEmpty);

            Key key = SerializeHelper.DeserializeBinary(keyFile.InputStream) as Key;
            if (key == null)
                ModelState.AddModelError("keyFile", Localization.Views.Home.errKeyFileIsNotValid);

            if (ModelState.IsValid)
            {
                IEmbeder embeder = _embederFactory.GetInstance(key.EmbedType, Enums.Commons.EmbedingOptions.UseAlpha);
                ICryptor cryptor = _algorithmFactory.GetInstance(key.CryptType);
                Container container = new Container(photoFile.InputStream, photoFile.ContentType);
                string message = DwmProcessorHelper.ExtractDwm(embeder, cryptor, key, container);

                TempData[TempDataKeys.EXTRACTED_MESSAGE] = message;

                return RedirectToAction(EXTRACT_RESULT);
            }

            return View();
        }
Example #9
0
 public bool ValidateKey(Key key, Container container)
 {
     return key.CryptType == CryptType.DES && key.MessageLength == container.InputStream.Length;
 }
 /// <summary>
 /// Embeds DWM within container.
 /// </summary>
 /// <param name="cryptor">Crypt algorithm.</param>
 /// <param name="embeder">Embeding algorithm.</param>
 /// <param name="message">Secret message.</param>
 /// <param name="key">Crypting key.</param>
 /// <param name="container">Container.</param>
 /// <param name="fileName">Container file name.</param>
 /// <returns>Saved file name at server.</returns>
 public static void EmbedDwm(ICryptor cryptor, IEmbeder embeder, string message, Key key, Container container)
 {
     Stream cryptedMessage = cryptor.Encrypt(StreamHelper.StringToStream(message), key);
     embeder.Embed(container, StreamHelper.StreamToBytesArray(cryptedMessage));
 }
 /// <summary>
 /// Detects is there Dwm in container.
 /// </summary>
 /// <param name="detector">Detecting algorithm.</param>
 /// <param name="inputStream">Conteiner file stream.</param>
 /// <returns>True if Dwm exists in container or false if it is not.</returns>
 public static bool DetectDwm(IDetector detector, Stream inputStream)
 {
     Container container = new Container(inputStream);
     return detector.Detect(container);
 }
Example #12
0
 public bool ValidateKey(Key key, Container container)
 {
     return true;
 }
Example #13
0
 /// <summary>
 /// Detects DWM in container
 /// </summary>
 /// <param name="container">Container.</param>
 /// <returns>True if there is a dwm in container, false otherwise.</returns>
 public bool Detect(Container container)
 {
     return random.Next() % 2 == 1;
 }