Example #1
0
        public static BitmapSource InsertionWithAlgorithmStandard(string imagePath, string messagePath, string key)
        {
            // Read pixel
            BitmapImage bitmapImage = new BitmapImage(new Uri(imagePath));
            int         height      = bitmapImage.PixelHeight;
            int         width       = bitmapImage.PixelWidth;
            int         nStride     = (bitmapImage.PixelWidth * bitmapImage.Format.BitsPerPixel + 7) / 8;

            byte[] pixels = new byte[bitmapImage.PixelHeight * nStride];
            bitmapImage.CopyPixels(pixels, nStride, 0);
            Console.WriteLine("Capacity = " + pixels.Length);

            // Create extended message
            string fileName = Path.GetFileName(messagePath);

            byte[] fileNameLengthBytes = BitConverter.GetBytes((Int32)fileName.Length);

            byte[] fileNameBytes = new byte[fileName.Length * 2];
            for (int i = 0; i < fileName.Length; i++)
            {
                byte[] bt = BitConverter.GetBytes(fileName[i]);
                fileNameBytes[i * 2]     = bt[0];
                fileNameBytes[i * 2 + 1] = bt[1];
            }
            byte[] messageBytesBeforeEncrypt = File.ReadAllBytes(messagePath);
            byte[] messageBytes       = Viginere.Encrypt(messageBytesBeforeEncrypt, key);
            byte[] messageLengthBytes = BitConverter.GetBytes((Int32)messageBytes.Length);
            byte[] messageExtended    = new byte[4 + fileName.Length * 2 + 4 + messageBytes.Length];
            int    idx = 0;

            for (int i = 0; i < fileNameLengthBytes.Length; i++)
            {
                messageExtended[idx] = fileNameLengthBytes[i];
                idx++;
            }
            for (int i = 0; i < fileNameBytes.Length; i++)
            {
                messageExtended[idx] = fileNameBytes[i];
                idx++;
            }
            for (int i = 0; i < messageLengthBytes.Length; i++)
            {
                messageExtended[idx] = messageLengthBytes[i];
                idx++;
            }
            for (int i = 0; i < messageBytes.Length; i++)
            {
                messageExtended[idx] = messageBytes[i];
                idx++;
            }

            // Inserting message
            List <int> seq = PRNG.GenerateSequence(key, pixels.Length);

            for (int i = 0; i < messageExtended.Length; i++)
            {
                for (byte j = 0; j < 8; j++)
                {
                    pixels[seq[i * 8 + j]] = pixels[seq[i * 8 + j]].SetBit(0, messageExtended[i].GetBit(j));
                }
            }
            BitmapSource bitmapOutput = BitmapImage.Create(bitmapImage.PixelWidth, bitmapImage.PixelHeight, bitmapImage.DpiX, bitmapImage.DpiY, bitmapImage.Format, bitmapImage.Palette, pixels, nStride);

            return(bitmapOutput);
        }
Example #2
0
        public static BitmapSource InsertionWithAlgorithmSwain(string imagePath, string messagePath, string key)
        {
            // Read pixel
            BitmapImage bitmapImage = new BitmapImage(new Uri(imagePath));
            int         height      = bitmapImage.PixelHeight;
            int         width       = bitmapImage.PixelWidth;
            int         nStride     = (bitmapImage.PixelWidth * bitmapImage.Format.BitsPerPixel + 7) / 8;

            byte[] pixels = new byte[bitmapImage.PixelHeight * nStride];
            bitmapImage.CopyPixels(pixels, nStride, 0);

            // Create extended message
            string fileName = Path.GetFileName(messagePath);

            byte[] fileNameLengthBytes = BitConverter.GetBytes((Int32)fileName.Length);

            byte[] fileNameBytes = new byte[fileName.Length * 2];
            for (int i = 0; i < fileName.Length; i++)
            {
                byte[] bt = BitConverter.GetBytes(fileName[i]);
                fileNameBytes[i * 2]     = bt[0];
                fileNameBytes[i * 2 + 1] = bt[1];
            }
            byte[] messageBytesBeforeEncrypt = File.ReadAllBytes(messagePath);
            byte[] messageBytes       = Viginere.Encrypt(messageBytesBeforeEncrypt, key);
            byte[] messageLengthBytes = BitConverter.GetBytes((Int32)messageBytes.Length);
            byte[] messageExtended    = new byte[4 + fileName.Length * 2 + 4 + messageBytes.Length];
            int    idx = 0;

            for (int i = 0; i < fileNameLengthBytes.Length; i++)
            {
                messageExtended[idx] = fileNameLengthBytes[i];
                idx++;
            }
            for (int i = 0; i < fileNameBytes.Length; i++)
            {
                messageExtended[idx] = fileNameBytes[i];
                idx++;
            }
            for (int i = 0; i < messageLengthBytes.Length; i++)
            {
                messageExtended[idx] = messageLengthBytes[i];
                idx++;
            }
            for (int i = 0; i < messageBytes.Length; i++)
            {
                messageExtended[idx] = messageBytes[i];
                idx++;
            }

            // Inserting message
            byteCnt = 0;
            bitCnt  = 0;
            cap     = 0;
            // process per block
            for (int i = 0; i < pixels.Length - 8; i += 9)
            {
                if (messageExtended.Length <= byteCnt)
                {
                    break;
                }

                // copy pixels to block
                byte[] block = new byte[9];
                block[0] = pixels[i];
                block[1] = pixels[i + 1];
                block[2] = pixels[i + 2];
                block[3] = pixels[i + 3];
                block[4] = pixels[i + 4];
                block[5] = pixels[i + 5];
                block[6] = pixels[i + 6];
                block[7] = pixels[i + 7];
                block[8] = pixels[i + 8];
                block    = processedBlock(block, messageExtended);

                // fill the pixels with processed block
                pixels[i]     = block[0];
                pixels[i + 1] = block[1];
                pixels[i + 2] = block[2];
                pixels[i + 3] = block[3];
                pixels[i + 4] = block[4];
                pixels[i + 5] = block[5];
                pixels[i + 6] = block[6];
                pixels[i + 7] = block[7];
                pixels[i + 8] = block[8];
            }

            BitmapSource bitmapOutput = BitmapImage.Create(bitmapImage.PixelWidth, bitmapImage.PixelHeight, bitmapImage.DpiX, bitmapImage.DpiY, bitmapImage.Format, bitmapImage.Palette, pixels, nStride);

            return(bitmapOutput);
        }
Example #3
0
        public static BitmapSource InsertionWithAlgorithmLiao(string imagePath, string messagePath, string key, int T, int Kl, int Kh, int cpp)
        {
            // Create extended message
            string fileName = Path.GetFileName(messagePath);

            byte[] fileNameLengthBytes = BitConverter.GetBytes((Int32)fileName.Length);

            byte[] fileNameBytes = new byte[fileName.Length * 2];
            for (int i = 0; i < fileName.Length; i++)
            {
                byte[] bt = BitConverter.GetBytes(fileName[i]);
                fileNameBytes[i * 2]     = bt[0];
                fileNameBytes[i * 2 + 1] = bt[1];
            }
            byte[] messageBytesBeforeEncrypt = File.ReadAllBytes(messagePath);
            byte[] messageBytes       = Viginere.Encrypt(messageBytesBeforeEncrypt, key);
            byte[] messageLengthBytes = BitConverter.GetBytes((Int32)messageBytes.Length);
            byte[] messageExtended    = new byte[4 + fileName.Length * 2 + 4 + messageBytes.Length];
            int    idx = 0;

            for (int i = 0; i < fileNameLengthBytes.Length; i++)
            {
                messageExtended[idx] = fileNameLengthBytes[i];
                idx++;
            }
            for (int i = 0; i < fileNameBytes.Length; i++)
            {
                messageExtended[idx] = fileNameBytes[i];
                idx++;
            }
            for (int i = 0; i < messageLengthBytes.Length; i++)
            {
                messageExtended[idx] = messageLengthBytes[i];
                idx++;
            }
            for (int i = 0; i < messageBytes.Length; i++)
            {
                messageExtended[idx] = messageBytes[i];
                idx++;
            }

            //Lockbitmap pixel handling

            LiaoAlgorithm lal        = new LiaoAlgorithm();
            Bitmap        bmp        = (Bitmap)Image.FromFile(imagePath, true);
            LockBitmap    lockBitmap = new LockBitmap(bmp);
            BitArray      bitmsg     = new BitArray(messageExtended);

            lockBitmap.LockBits();


            System.Drawing.Color[] pixes = new System.Drawing.Color[4];
            byte[] pixelInput            = new byte[4];
            bool   looping     = true;
            int    imgcapacity = 0;

            //capacity counting
            for (int i = 0; (i < lockBitmap.Width / 2) && looping; i++)
            {
                for (int j = 0; j < lockBitmap.Height / 2 && looping; j++)
                {
                    pixes[0] = lockBitmap.GetPixel(i * 2, j * 2);
                    pixes[1] = lockBitmap.GetPixel(i * 2 + 1, j * 2);
                    pixes[2] = lockBitmap.GetPixel(i * 2, j * 2 + 1);
                    pixes[3] = lockBitmap.GetPixel(i * 2 + 1, j * 2 + 1);

                    for (int nrgb = 0; nrgb < cpp && looping; nrgb++)
                    {
                        switch (nrgb)
                        {
                        case 0:
                            for (int pixIn = 0; pixIn < 4; pixIn++)
                            {
                                pixelInput[pixIn] = pixes[pixIn].R;
                            }
                            break;

                        case 1:
                            for (int pixIn = 0; pixIn < 4; pixIn++)
                            {
                                pixelInput[pixIn] = pixes[pixIn].G;
                            }
                            break;

                        case 2:
                            for (int pixIn = 0; pixIn < 4; pixIn++)
                            {
                                pixelInput[pixIn] = pixes[pixIn].B;
                            }
                            break;

                        default:
                            break;
                        }
                        imgcapacity += lal.Capacity(pixelInput, T, Kl, Kh);
                    }
                }
            }
            //end of capacity counting
            Console.WriteLine("capacity : " + imgcapacity);
            for (int i = 0; (i < lockBitmap.Width / 2) && looping; i++)
            {
                for (int j = 0; j < lockBitmap.Height / 2 && looping; j++)
                {
                    pixes[0] = lockBitmap.GetPixel(i * 2, j * 2);
                    pixes[1] = lockBitmap.GetPixel(i * 2 + 1, j * 2);
                    pixes[2] = lockBitmap.GetPixel(i * 2, j * 2 + 1);
                    pixes[3] = lockBitmap.GetPixel(i * 2 + 1, j * 2 + 1);

                    for (int nrgb = 0; nrgb < cpp && looping; nrgb++)
                    {
                        switch (nrgb)
                        {
                        case 0:
                            for (int pixIn = 0; pixIn < 4; pixIn++)
                            {
                                pixelInput[pixIn] = pixes[pixIn].R;
                            }
                            break;

                        case 1:
                            for (int pixIn = 0; pixIn < 4; pixIn++)
                            {
                                pixelInput[pixIn] = pixes[pixIn].G;
                            }
                            break;

                        case 2:
                            for (int pixIn = 0; pixIn < 4; pixIn++)
                            {
                                pixelInput[pixIn] = pixes[pixIn].B;
                            }
                            break;

                        default:
                            break;
                        }

                        if (lal.liaoEncrypt(pixelInput, T, Kl, Kh, bitmsg) != null) //parameter undefined
                        {
                            pixelInput = lal.liaoEncrypt(pixelInput, T, Kl, Kh, bitmsg);
                            if ((bitmsg.Length - 4 * lal.recentk) <= 0)
                            {
                                looping = false;
                            }
                            else
                            {
                                BitArray temp = new BitArray(bitmsg.Length - 4 * lal.recentk);
                                for (int k = 0; k < temp.Length; k++)
                                {
                                    temp[k] = bitmsg[k + 4 * lal.recentk];
                                }
                                bitmsg = temp;
                            }

                            switch (nrgb)
                            {
                            case 0:
                                for (int pixIn = 0; pixIn < 4; pixIn++)
                                {
                                    if (cpp == 3)
                                    {
                                        pixes[pixIn] = System.Drawing.Color.FromArgb(pixelInput[pixIn], pixes[pixIn].G, pixes[pixIn].B);
                                    }
                                    else
                                    {
                                        pixes[pixIn] = System.Drawing.Color.FromArgb(pixelInput[pixIn], pixelInput[pixIn], pixelInput[pixIn]);
                                    }
                                }
                                break;

                            case 1:
                                for (int pixIn = 0; pixIn < 4; pixIn++)
                                {
                                    pixes[pixIn] = System.Drawing.Color.FromArgb(pixes[pixIn].R, pixelInput[pixIn], pixes[pixIn].B);
                                }
                                break;

                            case 2:
                                for (int pixIn = 0; pixIn < 4; pixIn++)
                                {
                                    pixes[pixIn] = System.Drawing.Color.FromArgb(pixes[pixIn].R, pixes[pixIn].G, pixelInput[pixIn]);
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    }
                    lockBitmap.SetPixel(i * 2, j * 2, pixes[0]);
                    lockBitmap.SetPixel(i * 2 + 1, j * 2, pixes[1]);
                    lockBitmap.SetPixel(i * 2, j * 2 + 1, pixes[2]);
                    lockBitmap.SetPixel(i * 2 + 1, j * 2 + 1, pixes[3]);
                }
            }
            lockBitmap.UnlockBits();
            BitmapSource bs = LiaoAlgorithm.ConvertBitmap(bmp);

            return(bs);
        }