public Bitmap HideText(char[] chars)
        {
            List <bool> bits = BitManipulator.CharsToBits(chars);

            this.InsertMsgSize(chars.Length);
            return(this.HideText(bits));
        }
 public static bool[] HexToBits(string hex)
 {
     bool[] ret;
     byte[] toByte = BitManipulator.FromHex(hex);
     ret = (BitManipulator.GetMasks(toByte)).ToArray();
     return(ret);
 }
        public Bitmap HideText(string msg)
        {
            this.InsertMsgSize(msg.Length);
            // byte[] byteMsg = Encoding.ASCII.GetBytes(msg);
            // List<bool> masks = BitManipulator.GetMasks(byteMsg);
            List <bool> masks = BitManipulator.StrToBits(msg);

            return(this.HideText(masks));
        }
        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);
        }
示例#5
0
        public char[] Decrypt(char[] ciphertext)
        {
            double[] ciphervector = BitManipulator.CharToDouble(ciphertext);
            //MWArray[] mw = this.aesMatObj.AESDecrypt(2, (MWNumericArray)(MatricesFactory.To2D(ciphervector)), CipherKey, SboxDec, SboxInv, Rcon, MatMixColInv);
            //MWNumericArray mn = (MWNumericArray)mw[1];
            MWArray plaintext = (MWArray)this.aesMatObj.AESDecrypt((MWNumericArray)(MatricesFactory.To2D(ciphervector)), CipherKey, SboxDec, SboxInv, Rcon, MatMixColInv);

            char[,] plaintextchars = (char[, ])plaintext.ToArray();

            return(MatricesFactory.To1D(plaintextchars));
        }
        private Bitmap InsertMsgSize(int n)
        {
            //int msgSize = msg.Length;
            //byte[] msgSizeInBytes = BitConverter.GetBytes(msgSize);
            //if (BitConverter.IsLittleEndian)
            //    Array.Reverse(msgSizeInBytes);

            //List<bool> masks = BitManipulator.GetMasks(msgSizeInBytes);

            List <bool> masks = BitManipulator.IntToBits(n);
            //masks.Reverse();
            Color clr;
            bool  done = false;
            byte  m = 254;
            int   k = 0, i = 0, j = 0;

            for (i = 0; i < this.bmp.Width && k < masks.Count; i++)
            {
                for (j = 0; j < this.bmp.Height && k < masks.Count; j++)
                {
                    if (k == masks.Count - 2)
                    {
                        clr = this.bmp.GetPixel(i, j);
                        clr = Color.FromArgb(clr.R & (byte)254,
                                             clr.G & (byte)254, clr.B);

                        clr = Color.FromArgb(clr.R | Convert.ToByte(masks[k]),
                                             clr.G | Convert.ToInt32(masks[k + 1]), clr.B);
                        this.bmp.SetPixel(i, j, clr);
                        done = true;
                        break;
                    }
                    else
                    {
                        clr = this.bmp.GetPixel(i, j);
                        clr = Color.FromArgb(clr.R & (byte)254,
                                             clr.G & (byte)254, clr.B & (byte)254);
                        clr = Color.FromArgb(clr.R | Convert.ToInt32(masks[k]),
                                             clr.G | Convert.ToInt32(masks[k + 1]), clr.B | Convert.ToInt32(masks[k + 2]));
                        this.bmp.SetPixel(i, j, clr);
                        k += 3;
                    }
                }
                if (done)
                {
                    break;
                }
            }
            this.w = i;
            this.h = j;
            return(bmp);
        }
示例#7
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);
        }
示例#8
0
 public DES(string key)
 {
     this.key     = BitManipulator.StringToBinary(key);
     this.allKeys = new bool[16][];
     this.sboxes  = new List <int[, ]>();
     this.sboxes.Add(s1);
     this.sboxes.Add(s2);
     this.sboxes.Add(s3);
     this.sboxes.Add(s4);
     this.sboxes.Add(s5);
     this.sboxes.Add(s6);
     this.sboxes.Add(s7);
     this.sboxes.Add(s8);
     this.GenerateKeys();
 }
示例#9
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);
        }
示例#10
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);
        }
示例#11
0
        private void SetBlocksOfencyptedMessage(char[] encyptedMessage)
        {
            int numOfBlock = encyptedMessage.Length / 8;

            this.blocks = new bool[numOfBlock][];
            byte[] MsgByte = new byte[8];
            for (int i = 0; i < numOfBlock; i++)
            {
                for (int j = 0; j < MsgByte.Length; j++)
                {
                    MsgByte[j] = (byte)encyptedMessage[j + (i * 8)];
                }
                Array.Reverse(MsgByte);
                this.blocks[i] = (BitManipulator.GetMasks(MsgByte)).ToArray();
            }
        }
示例#12
0
 public void GenerateKeys()
 {
     bool[] keyPc1   = this.Permutation(this.PC1, this.key);
     bool[] roundkey = keyPc1;
     bool[] C        = new bool[28];
     bool[] D        = new bool[28];
     bool[] keyPc2;
     for (int i = 0; i < 16; i++)
     {
         Array.Copy(roundkey, 0, C, 0, C.Length);
         Array.Copy(roundkey, 28, D, 0, D.Length);
         C = BitManipulator.LCS(this.LCS[i], C);
         D = BitManipulator.LCS(this.LCS[i], D);
         Array.Copy(C, 0, roundkey, 0, C.Length);
         Array.Copy(D, 0, roundkey, C.Length, D.Length);
         keyPc2          = this.Permutation(this.PC2, roundkey);
         this.allKeys[i] = keyPc2;
     }
 }
示例#13
0
        private void SetBlocksOfPlainText(string secretMsg)
        {
            bool[] secretMsgBits = BitManipulator.StringToBinary(secretMsg);
            int    numOfBlocks   = secretMsgBits.Length / 64;

            numOfBlocks = (secretMsgBits.Length % 64 != 0) ? ++numOfBlocks : numOfBlocks;
            blocks      = new bool[numOfBlocks][];
            bool[] block = new bool[64];
            int    i, j = 0, count = 0;

            /////Padding & Block Division
            for (i = 0; i < secretMsgBits.Length && j < numOfBlocks; i++)
            {
                if (count == 63)
                {
                    block[count] = secretMsgBits[i];
                    blocks[j]    = block;
                    block        = new bool[64];
                    j++;
                    count = 0;
                }
                else
                {
                    block[count] = secretMsgBits[i];
                    ++count;
                    if (i == secretMsgBits.Length - 1)
                    {
                        blocks[j] = block;
                    }
                }
            }
            if (count != 0)
            {
                for (int k = count; k < 64; k++)
                {
                    blocks[j][k] = false;
                }
            }
        }
        public int GetMsgSize()
        {
            List <bool> bits  = new List <bool>();
            bool        done  = false;
            int         count = 0;
            Color       clr;
            int         i = 0, j = 0;

            for (i = 0; i < bmp.Width; i++)
            {
                for (j = 0; j < bmp.Height; j++)
                {
                    clr = bmp.GetPixel(i, j);
                    if (count == 30)
                    {
                        bits.Add(BitManipulator.GetBit(clr.R, 1));
                        bits.Add(BitManipulator.GetBit(clr.G, 1));
                        done = true;
                        break;
                    }
                    else
                    {
                        clr = 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));
                        count += 3;
                    }
                }
                if (done)
                {
                    break;
                }
            }
            //byte[] bytes = BitManipulator.BoolToByte(bits.ToArray());
            this.w = i;
            this.h = j;
            return(BitManipulator.BoolToInt(bits.ToArray())[0]);
        }