Пример #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            Stopwatch d1   = new Stopwatch();
            LFSR      obj1 = new LFSR();

            obj1.TapPosIndx = (int)nudMaskSize.Value;

            if (textBox1.Text.Length > 10)
            {
                MessageBox.Show("please enter an initial seed less than 10 characters");
            }
            else
            {
                string seed = ImageOperations.AlphanumericSeed(textBox1.Text);
                obj1.Seed = Convert.ToInt64(seed, 2);        //5adet el string as bits where 2 means binary
                int SeedLength = (textBox1.Text.Length * 6);

                d1.Start();
                ImageMatrix = ImageOperations.Encryption(ImageMatrix, ref obj1.Seed, obj1.TapPosIndx, SeedLength);
                d1.Stop();

                ImageOperations.DisplayImage(ImageMatrix, pictureBox2);
                MessageBox.Show("RunTime = " + Convert.ToString(d1.Elapsed.TotalMinutes));
            }
        }
Пример #2
0
        public static String FindValues(int pos, String Data)
        {
            //String BinaryData = Convert.ToString(System.Convert.ToInt32(valueByte), 2);

            LFSR a = new LFSR(Data, pos);

            for (int i = 1; i < Data.Length; ++i)
            {
                a.Shift();
            }

            String temp = Convert.ToInt32(a.Registry, 2).ToString();

            return(temp);
        }
Пример #3
0
        public static byte GeneratingBits(ref long seed, int tap, int SeedLength)
        {
            LFSR Register = new LFSR();

            Register.Seed = seed;  //Initial value of the LFSR entered by user

            for (int i = 0; i < 8; i++)
            {
                Register.One         = Register.One << SeedLength;                   //Shiftting the One Variable until it reaches the end of the Initial Seed
                Register.LeftMostBit = (Register.One & Register.Seed);               //ANDing the One with the leftMostBit of the initial seed to know the value of it.
                Register.LeftMostBit = Register.LeftMostBit >> SeedLength;           //Getting the LeftMostBit's vale
                Register.One         = 1;                                            //Re-Initializing the One Variable.

                Register.One         = Register.One << tap;                          //Shiftting the One Variable until it reaches to the tap Position
                Register.TapPosValue = (Register.One & Register.Seed);               //ANDing the One with the Tap Position's Value to know it.
                Register.TapPosValue = Register.TapPosValue >> tap;                  //Getting the Tap Position 's Value.
                Register.One         = 1;                                            //Re-Initializing the One Variable.

                Register.XORedValue = (Register.LeftMostBit ^ Register.TapPosValue); //XORing the LeftMostBit with the TapPosition's Value to get
                                                                                     //the new bit.

                Register.Key[i] = Register.XORedValue;                               //Starting to fill the 8-bit Key
                Register.Seed   = Register.Seed << 1;                                //Shift the seed one step to the left
                Register.Seed   = (Register.Seed | Register.XORedValue);             //Setting the new Register Seed.

                Register.One = 1;                                                    //Re-Initializing the One Variable for the next Loop.
                seed         = Register.Seed;
            }

            // Converting the Key Array into 1 BYTE to XOR it with the pixel component.

            int index = 8 - Register.Key.Length;

            byte res = 0;

            foreach (int i in Register.Key)
            {
                if (i == 1)
                {
                    res |= (byte)(1 << (7 - index));
                }
                index++;
            }

            return(res);
        }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch d1 = new Stopwatch();


            LFSR obj1 = new LFSR();

            obj1.TapPosIndx = (int)nudMaskSize.Value;
            obj1.Seed       = Convert.ToInt64(textBox1.Text, 2);  //5adet el string as bits where 2 means binary
            int SeedLength = textBox1.Text.Length - 1;

            d1.Start();
            ImageMatrix = ImageOperations.Encryption(ImageMatrix, ref obj1.Seed, obj1.TapPosIndx, SeedLength);
            d1.Stop();

            ImageOperations.DisplayImage(ImageMatrix, pictureBox2);
            MessageBox.Show("RunTime = " + Convert.ToString(d1.Elapsed.TotalMinutes));
        } //Encryption & decryption
        //This function Encrypt or Decrypt the image
        //O(N^2)
        public bool EncDec(string seed, int tap)
        {
            LFSR lf = new LFSR(seed, tap);
            int  R, G, B, oldBlue, oldGreen, oldRed;

            unsafe
            {
                // locking the bits in order to start making operations on them
                BitmapData bitmapData     = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, img.PixelFormat);
                int        bytesPerPixel  = System.Drawing.Bitmap.GetPixelFormatSize(img.PixelFormat) / 8;
                int        heightInPixels = bitmapData.Height;
                int        widthInBytes   = bitmapData.Width * bytesPerPixel;
                byte *     ptrFirstPixel  = (byte *)bitmapData.Scan0;


                for (int y = 0; y < heightInPixels; y++)
                {
                    byte *currentLine = ptrFirstPixel + (y * bitmapData.Stride); // A pointer that points on each line
                    for (int x = 0; x < widthInBytes; x = x + bytesPerPixel)
                    {
                        //Generate 8bits for each color
                        R        = (int)lf.Kstep(8);
                        G        = (int)lf.Kstep(8);
                        B        = (int)lf.Kstep(8);
                        oldBlue  = currentLine[x];
                        oldGreen = currentLine[x + 1];
                        oldRed   = currentLine[x + 2];

                        // Generate and set the new pixel value
                        currentLine[x]     = (byte)(oldBlue ^ B);
                        currentLine[x + 1] = (byte)(oldGreen ^ G);
                        currentLine[x + 2] = (byte)(oldRed ^ R);
                    }
                }
                //Unlocking the bits
                img.UnlockBits(bitmapData);
            }
            return(true);
        }
Пример #6
0
        public static RGBPixel[,] Encryption(RGBPixel[,] ImageMatrix, ref long seed, int tap, int SeedLength)
        {
            int  width  = GetWidth(ImageMatrix);
            int  height = GetHeight(ImageMatrix);
            byte Key    = 0;

            RGBPixel[,] EncreptedImage = new RGBPixel[height, width];

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Key = LFSR.GeneratingBits(ref seed, tap, SeedLength);
                    EncreptedImage[i, j].red = (byte)(ImageMatrix[i, j].red ^ Key);

                    Key = LFSR.GeneratingBits(ref seed, tap, SeedLength);
                    EncreptedImage[i, j].green = (byte)(ImageMatrix[i, j].green ^ Key);

                    Key = LFSR.GeneratingBits(ref seed, tap, SeedLength);
                    EncreptedImage[i, j].blue = (byte)(ImageMatrix[i, j].blue ^ Key);
                }
            }
            return(EncreptedImage);
        }