Ejemplo n.º 1
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);
        }