예제 #1
0
        /// <summary>
        /// Converts a list of locations back to their hex characters.
        /// </summary>
        /// <param name="locations">List of locations read from file.</param>
        /// <returns>A string of hex characters.</returns>
        public string decode(IEnumerable <Location> locations)
        {
            var sb = new StringBuilder();

            foreach (var loc in locations)
            {
                var x            = Convert.ToInt32(loc.getX());
                var y            = Convert.ToInt32(loc.getY());
                var hashLocation = Convert.ToInt32(loc.getHashLocation());

                if (x >= imageWidth || y >= imageHeight || hashLocation >= 128)
                {
                    Console.Error.WriteLine("[ERROR]: Decode file has invalid sizes.");
                    Environment.Exit(91);
                }

                var key = x + "-" + y;

                PixelInformation px;

                try
                {
                    px = pixelMap[key];
                }
                catch (KeyNotFoundException)
                {
                    px = new PixelInformation(this, x, y);
                    pixelMap.Add(key, px);
                }

                sb.Append(px.getLetter(hashLocation));
            }

            return(sb.ToString());
        }
예제 #2
0
        /// <summary>
        /// Adds more pixels from the original image file.
        /// </summary>
        /// <param name="amount">Amount of pixels to add from parent image.</param>
        public void addPixels(int amount)
        {
            if (pixelMap.Count + 1 + amount > getImageSize())
            {
                Console.Error.WriteLine(
                    "[ERROR]: This image is not large enough to hide the data, please start again with a bigger image.");
                Environment.Exit(97);
            }

            using (var provider = new RNGCryptoServiceProvider())
            {
                for (var i = 0; i < amount; i++)
                {
                    var data    = new byte[100];
                    var invalid = true;

                    do
                    {
                        //X
                        provider.GetBytes(data);
                        var value = BitConverter.ToUInt64(data, 0);
                        var x     = (int)(value % Convert.ToUInt64(imageWidth));

                        //Y
                        provider.GetBytes(data);
                        value = BitConverter.ToUInt64(data, 0);
                        var y = (int)(value % Convert.ToUInt64(imageHeight));

                        var key = x + "-" + y;

                        try
                        {
                            var px = new PixelInformation(this, x, y);
                            pixelMap.Add(key, px);
                        }
                        //If it's duplicated we need a different one
                        catch (ArgumentException)
                        {
                            continue;
                        }

                        invalid = false;
                    } while (invalid);
                }

                updateHexCharacters();
            }
        }