// original: may be is null if mode is xor
        public T_Data Decrypt(Bitmap original)
        {
            ImageOrigimal = original;

            WriteReadData.SetSettingsMode();

            if (GetConfPixel(WriteReadDataIdPosition) != WriteReadData.GetID())
            {
                return(DataReader.ToObject(System.Text.Encoding.UTF8.GetBytes("There is no hidden data in image!")));
            }
            var CompressionId = GetConfPixel(DataCompressionIdPosition);
            var EncryptId1    = GetConfPixel(Encrypt1IdPosition);
            var EncryptId2    = GetConfPixel(Encrypt2IdPosition);


            byte[] countDataList = new byte[4];
            for (int i = 0; i < 4; ++i)
            {
                var r          = i * 2;
                var bigPath    = GetConfPixel(r);
                var littlePath = GetConfPixel(r + 1);

                bigPath = (byte)(bigPath << 4);
                countDataList[3 - i] = (byte)(bigPath | littlePath); // index from end becouse big endian format
            }
            var dataLengthBits  = BitConverter.ToInt32(countDataList, 0);
            var dataLengthBytes = dataLengthBits / 8;

            countDataList = new byte[dataLengthBytes * 2];

            WriteReadData.SetDataMode();
            int y = 0;

            for (int i = 0; i < countDataList.Length;)
            {
                byte bR = imageArray[y + 0];
                byte bG = imageArray[y + 1];
                byte bB = imageArray[y + 2];

                var d0 = (byte)(0b00001111 & bR);
                var d1 = (byte)(0b00001111 & bG);
                var d2 = (byte)(0b00001111 & bB);

                //var pixelEnc = ImageModifying.GetPixel(x, y);
                //var pixelOrg = original == null ? new Color() : original.GetPixel(x, y);
                //var d = WriteReadData.ReadData(pixelEnc, pixelOrg);

                if (i < countDataList.Length)
                {
                    countDataList[i++] = d0;
                }
                if (i < countDataList.Length)
                {
                    countDataList[i++] = d1;
                }
                if (i < countDataList.Length)
                {
                    countDataList[i++] = d2;
                }
                y += 4;
            }

            var data = _byteOperations.JoinBytes(countDataList);

            if (EncryptId2 > 0)
            {
                var enc = this.Encrypts.Where(r => r.GetID() == EncryptId2).FirstOrDefault();
                if (enc != null)
                {
                    data = enc.Decrypt(data);
                }
            }
            if (EncryptId1 > 0)
            {
                var enc = this.Encrypts.Where(r => r.GetID() == EncryptId1).FirstOrDefault();
                if (enc != null)
                {
                    data = enc.Decrypt(data);
                }
            }
            if (CompressionId > 0)
            {
                var compr = this.Compressions.Where(r => r.GetID() == CompressionId).FirstOrDefault();
                if (compr != null)
                {
                    data = compr.Recovery(data);
                }
            }

            return(DataReader.ToObject(data));
        }
Example #2
0
        // original: may be is null if mode is xor
        public T_Data Decrypt(Bitmap original)
        {
            ImageOrigimal = original;

            WriteReadData.SetSettingsMode();

            if (GetConfPixel(WriteReadDataIdPosition) != WriteReadData.GetID())
            {
                return(DataReader.ToObject(System.Text.Encoding.UTF8.GetBytes("There is no hidden data in image!")));
                //throw new ArgumentException("Parameter is not correct", "WriteReadData");
            }
            var CompressionId = GetConfPixel(DataCompressionIdPosition);
            var EncryptId1    = GetConfPixel(Encrypt1IdPosition);
            var EncryptId2    = GetConfPixel(Encrypt2IdPosition);


            byte[] countDataList = new byte[4];
            for (int i = 0; i < 4; ++i)
            {
                var r          = i * 2;
                var bigPath    = GetConfPixel(r);
                var littlePath = GetConfPixel(r + 1);

                bigPath = (byte)(bigPath << 4);
                countDataList[3 - i] = (byte)(bigPath | littlePath); // index from end becouse big endian format
            }
            var dataLengthBits  = BitConverter.ToInt32(countDataList, 0);
            var dataLengthBytes = dataLengthBits / 8;

            countDataList = new byte[dataLengthBytes * 2];

            WriteReadData.SetDataMode();
            int x = 0, y = 1;

            for (int i = 0; i < countDataList.Length;)
            {
                if (x >= ImageModifying.Width)
                {
                    y++; x = 0;
                }
                var pixelEnc = ImageModifying.GetPixel(x, y);
                var pixelOrg = original == null ? new Color() : original.GetPixel(x, y);
                var d        = WriteReadData.ReadData(pixelEnc, pixelOrg);


                if (i < countDataList.Length)
                {
                    countDataList[i++] = d[0];
                }
                if (i < countDataList.Length)
                {
                    countDataList[i++] = d[1];
                }
                if (i < countDataList.Length)
                {
                    countDataList[i++] = d[2];
                }
                ++x;
            }

            var data = _byteOperations.JoinBytes(countDataList);

            if (EncryptId2 > 0)
            {
                var enc = this.Encrypts.Where(r => r.GetID() == EncryptId2).FirstOrDefault();
                if (enc != null)
                {
                    data = enc.Decrypt(data);
                }
            }
            if (EncryptId1 > 0)
            {
                var enc = this.Encrypts.Where(r => r.GetID() == EncryptId1).FirstOrDefault();
                if (enc != null)
                {
                    data = enc.Decrypt(data);
                }
            }
            if (CompressionId > 0)
            {
                var compr = this.Compressions.Where(r => r.GetID() == CompressionId).FirstOrDefault();
                if (compr != null)
                {
                    data = compr.Recovery(data);
                }
            }

            return(DataReader.ToObject(data));
        }