示例#1
0
        public static void StegBinary(string PngFilePath, BitArray Bits, int SecurityLevel)
        {
            var bitmap    = new Bitmap(PngFilePath);
            var imgHelper = new ImageHelper(bitmap);

            imgHelper.LockBits();
            var bytes   = imgHelper.Pixels;
            var bitsLen = Bits.Length;
            // Count all bytes useful for embedding (2 bits per slot = byte)
            var slots = 2 * imgHelper.Pixels.Length;

            if (slots < bitsLen)
            {
                throw new Exception("Content too large for the selected image.");
            }
            // If ok continue
            int  currentBit = 0;
            int  byteCount  = 0;
            bool canIterate = true;

            while (canIterate)
            {
                // Define the step of bits to write according to security level
                if ((0 == byteCount % SecurityLevel) || (currentBit < 64))
                {
                    // Get the current color component value
                    byte currentVal = bytes[byteCount];
                    // Get the current bit
                    bool bit = Bits[currentBit];
                    // Write the new value in position 0
                    byte newVal = getNewValForComponent(currentVal, bit, 0);
                    currentBit++;
                    canIterate = currentBit < bitsLen;
                    // If still bits write on the 1st position after the first 64 bits
                    if (canIterate && currentBit > 64)
                    {
                        // Get the current bit
                        bit = Bits[currentBit];
                        // Write the new value in position 1
                        newVal = getNewValForComponent(newVal, bit, 1);
                        currentBit++;
                        canIterate = currentBit < bitsLen;
                    }
                    // Set the new value for the color component
                    bytes[byteCount] = newVal;
                }
                byteCount++;
            }
            imgHelper.UnlockBits();
            var flPath             = System.IO.Path.GetDirectoryName(PngFilePath);
            var filename           = PngFilePath.Remove(0, string.Concat(flPath, "\\").Length);
            var docPath            = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var newFileNameAndPath = $@"{docPath}\StegImageUI\steg_{DateTime.Now.ToString("yyyyMMddHHmmss")}_{filename}";

            bitmap.Save(newFileNameAndPath, ImageFormat.Png);
        }
示例#2
0
        /// <summary>
        /// Return the available slots for embedding.
        /// </summary>
        /// <param name="TifFilePath"></param>
        /// <returns></returns>
        public static int GetAvailableSlots(string TifFilePath)
        {
            Bitmap bitmap = new Bitmap(TifFilePath);
            var    ih     = new ImageHelper(bitmap);

            ih.LockBits();
            var slots = ih.Pixels.Length;

            ih.UnlockBits();
            // TIF: 2 bits per byte
            return(slots * 2);
        }
示例#3
0
        /// <summary>
        /// Return the available slots for embedding.
        /// </summary>
        /// <param name="PngFilePath"></param>
        /// <returns></returns>
        public static int GetAvailableSlots(string PngFilePath)
        {
            var bitmap    = new Bitmap(PngFilePath);
            var imgHelper = new ImageHelper(bitmap);
            int byteCount = 0;

            imgHelper.LockBits();
            byteCount = imgHelper.Pixels.Length;
            imgHelper.UnlockBits();
            // Check slots availability
            // Count all bytes useful for embedding (2 bits per slot = byte)
            var slots = 2 * byteCount;

            return(slots);
        }
示例#4
0
        public static StegStruct UnstegBinary(string PngFilePath)
        {
            var ssRet     = new StegStruct();
            int iter      = 0;
            var bitmap    = new Bitmap(PngFilePath);
            var imgHelper = new ImageHelper(bitmap);

            imgHelper.LockBits();
            var bytes = imgHelper.Pixels;

            imgHelper.UnlockBits();
            // Read the content length
            BitArray baTotLength;

            try
            {
                baTotLength = readAllBitArray(bytes, iter, 32, oneBitReading: true);
            }
            catch (Exception ex)
            {
                _log.Debug(ex.Message);
                throw new Exception("No data embedded in this image or the image is corrupted.");
            }
            // If data is correct, continue
            int totalBits = baTotLength.ToInteger();

            ssRet.TotalSize = totalBits;
            // Read the security level: int => 32 bits (2 bits per component)
            iter += 32;
            BitArray baSecLev;

            try
            {
                baSecLev = readAllBitArray(bytes, iter, 32, oneBitReading: true);
            }
            catch (Exception ex)
            {
                _log.Debug(ex.Message);
                throw new Exception("No data embedded in this image or the image is corrupted.");
            }
            // If data is correct, continue
            int secLev = baSecLev.ToInteger();

            ssRet.SecurityLevel = secLev;
            // Read all steg bits step by security level
            // Start from index 16 for every DCT coeff 2 bits to take => end DCT index is 64
            iter += 32;
            // The security level bitrun has to excluded
            var bits2Read = totalBits - 32;
            // Get the byte array with the required data
            BitArray stegBits;

            try
            {
                stegBits = readAllBitArray(bytes, iter, bits2Read, secLev);
            }
            catch (Exception ex)
            {
                _log.Debug(ex.Message);
                throw new Exception("No data embedded in this image or the image is corrupted.");
            }
            // If data is correct, continue
            ssRet.ContentFull = baTotLength.Append(baSecLev).Append(stegBits);
            // Start reading the bit array struct --------------------------------------------------
            // Read if it is a string or file
            ssRet.IsFile = stegBits[0];
            // Read all the content
            int idx   = 1;
            int lng   = stegBits.Length - idx;
            var baTmp = readBitArrayData(stegBits, idx, lng);

            ssRet.Content     = baTmp;
            ssRet.ContentSize = ssRet.Content.Length;

            return(ssRet);
        }