public char[] GetText()
        {
            string      ret       = "";
            int         msgSize   = this.GetMsgSize();
            int         numOfBits = msgSize * 8;
            List <bool> bits      = new List <bool>();

            byte[] bytes;
            Color  clr  = new Color();
            bool   done = false;

            for (int i = this.w; i < this.bmp.Width; i++)
            {
                for (int j = this.h + 1; j < this.bmp.Height; j++)
                {
                    if (numOfBits < 3)
                    {
                        clr = this.bmp.GetPixel(i, j);
                        if (numOfBits == 2)
                        {
                            bits.Add(BitManipulator.GetBit(clr.R, 1));
                            bits.Add(BitManipulator.GetBit(clr.G, 1));
                        }
                        else if (numOfBits == 1)
                        {
                            bits.Add(BitManipulator.GetBit(clr.R, 1));
                        }
                        done = true;

                        numOfBits -= 3;
                        break;
                    }
                    else
                    {
                        clr = this.bmp.GetPixel(i, j);
                        bits.Add(BitManipulator.GetBit(clr.R, 1));
                        bits.Add(BitManipulator.GetBit(clr.G, 1));
                        bits.Add(BitManipulator.GetBit(clr.B, 1));
                        numOfBits -= 3;
                    }
                }
                if (done)
                {
                    break;
                }
            }
            //bytes = BitManipulator.BoolToByte(bits.ToArray());
            //char[] charArray = Encoding.ASCII.GetString(bytes).ToCharArray();
            //Array.Reverse(charArray);
            byte[] ints = BitManipulator.BoolToByte(bits.ToArray());
            //char[] chars = BitManipulator.IntsToChar(ints);
            char[] chars = BitManipulator.BytesToChars(ints);
            Array.Reverse(chars);
            return(chars);
        }
Ejemplo n.º 2
0
        private string Decryption(char[] encyptedMessage)
        {
            string OriginalMessage = "";

            this.SetBlocksOfencyptedMessage(encyptedMessage);
            bool[] msgtIP;
            bool[] LHalf = new bool[32];
            bool[] RHalf = new bool[32];
            bool[] REtable;
            bool[] inpXORmsg;
            bool[] sboxOutput    = new bool[32];
            bool[] sboxCellInput = new bool[6];
            bool[] sboxP;
            bool[] RXorL    = null;
            bool[] encyMsg  = new bool[64];
            bool[] encyMsgP = new bool[64];
            byte[] encyMsgByte;
            for (int i = 0; i < this.blocks.GetLength(0); i++)
            {
                msgtIP = this.Permutation(this.IP, this.blocks[i]);
                Array.Copy(msgtIP, 0, LHalf, 0, LHalf.Length);
                Array.Copy(msgtIP, LHalf.Length, RHalf, 0, RHalf.Length);
                for (int j = 0; j < 16; j++)
                {
                    REtable   = this.Permutation(this.ETable, RHalf);
                    inpXORmsg = BitManipulator.XOR(REtable, this.allKeys[16 - j - 1]);

                    for (int k = 0, l = 0; k < inpXORmsg.Length; k += 6, l++)
                    {
                        Array.Copy(inpXORmsg, k, sboxCellInput, 0, sboxCellInput.Length);
                        bool[] temp = this.Sbox(sboxCellInput, this.sboxes[l]);
                        Array.Copy(temp, 0, sboxOutput, 4 * l, 4);
                    }
                    sboxP = this.Permutation(this.P, sboxOutput);
                    RXorL = BitManipulator.XOR(sboxP, LHalf);
                    LHalf = RHalf;
                    RHalf = RXorL;
                }
                RHalf = LHalf;
                LHalf = RXorL;
                Array.Copy(LHalf, 0, encyMsg, 0, LHalf.Length);
                Array.Copy(RHalf, 0, encyMsg, LHalf.Length, RHalf.Length);
                encyMsgP    = this.Permutation(this.IP1, encyMsg);
                encyMsgByte = BitManipulator.BoolToByte(encyMsgP);
                Array.Reverse(encyMsgByte);
                OriginalMessage += Encoding.ASCII.GetString(encyMsgByte);
            }

            return(OriginalMessage);
        }
Ejemplo n.º 3
0
        private bool[] Sbox(bool[] sboxCellInput, int[,] sbox)
        {
            bool[] rowInBool = { sboxCellInput[0], sboxCellInput[5] };
            bool[] colInBool = { sboxCellInput[1], sboxCellInput[2], sboxCellInput[3], sboxCellInput[4] };
            byte[] rowInByte = BitManipulator.BoolToByte(rowInBool);
            byte[] colInByte = BitManipulator.BoolToByte(colInBool);
            int    row       = (int)(Convert.ToDecimal(rowInByte[0]));
            int    col       = (int)(Convert.ToDecimal(colInByte[0]));
            int    temp      = sbox[row, col];

            byte[] b = BitManipulator.IntTobytes(temp);
            bool[] sboxCellOutput = new bool[4];
            Array.Copy(BitManipulator.GetMasks(b).ToArray(), 28, sboxCellOutput, 0, 4);
            return(sboxCellOutput);
        }
Ejemplo n.º 4
0
        private char[] Encryption(string Msg)
        {
            this.SetBlocksOfPlainText(Msg);
            bool[] msgtIP;
            bool[] LHalf = new bool[32];
            bool[] RHalf = new bool[32];
            bool[] REtable;
            bool[] inpXORmsg;
            bool[] sboxOutput    = new bool[32];
            bool[] sboxCellInput = new bool[6];
            bool[] sboxP;
            bool[] RXorL    = null;
            bool[] encyMsg  = new bool[64];
            bool[] encyMsgP = new bool[64];
            byte[] encyMsgByte;
            char[] encyMsgChar = new char[this.blocks.GetLength(0) * 8];
            for (int i = 0; i < this.blocks.GetLength(0); i++)
            {
                msgtIP = this.Permutation(this.IP, this.blocks[i]);
                Array.Copy(msgtIP, 0, LHalf, 0, LHalf.Length);
                Array.Copy(msgtIP, LHalf.Length, RHalf, 0, RHalf.Length);
                for (int j = 0; j < 16; j++)
                {
                    REtable   = this.Permutation(this.ETable, RHalf);
                    inpXORmsg = BitManipulator.XOR(REtable, this.allKeys[j]);

                    for (int k = 0, l = 0; k < inpXORmsg.Length; k += 6, l++)
                    {
                        Array.Copy(inpXORmsg, k, sboxCellInput, 0, sboxCellInput.Length);
                        bool[] temp = this.Sbox(sboxCellInput, this.sboxes[l]);
                        Array.Copy(temp, 0, sboxOutput, 4 * l, 4);
                    }
                    sboxP = this.Permutation(this.P, sboxOutput);
                    RXorL = BitManipulator.XOR(sboxP, LHalf);
                    LHalf = RHalf;
                    RHalf = RXorL;
                }
                RHalf = LHalf;
                LHalf = RXorL;
                Array.Copy(LHalf, 0, encyMsg, 0, LHalf.Length);
                Array.Copy(RHalf, 0, encyMsg, LHalf.Length, RHalf.Length);
                encyMsgP    = this.Permutation(this.IP1, encyMsg);
                encyMsgByte = BitManipulator.BoolToByte(encyMsgP);
                Array.Copy(this.ToCharArray(encyMsgByte), 0, encyMsgChar, i * 8, 8);
            }

            return(encyMsgChar);
        }