Ejemplo n.º 1
0
        private void ChunkMessageFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            messageChunks.Clear();

            int n_byte = nBitPerBlock / 8;
            int offset = 0;
            byte[] temp = new byte[n_byte];
            int length = fs.Read(temp, offset, n_byte);

            //Console.WriteLine(Encoding.ASCII.GetString(temp));
            Chunk chunk;

            while (length == n_byte)
            {
                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] { temp[i], temp[i+1], temp[i+2], temp[i+3] }));
                }

                messageChunks.Add(chunk);

                offset += length;
                length = fs.Read(temp, 0, n_byte);
                //Console.WriteLine(Encoding.ASCII.GetString(temp));
            }
            //Console.WriteLine(Encoding.ASCII.GetString(temp) +":"+ length);

            int k_zero = n_byte - ((int)(fs.Length % n_byte)) - 64 / 8;
            if (k_zero == 0) k_zero = 448 / 8;

            //Console.WriteLine("Zero adding : "+ k_zero);

            byte[] bytes_to_append = new byte[k_zero];
            bytes_to_append[0] = 128;
            for (int i = 1; i < k_zero; i++)
            {
                bytes_to_append[i] = 0;
            }

            bytes_to_append = bytes_to_append.Concat(LongToBytes(fs.Length*8,64/8)).ToArray();

            int i_append = 0;
            if (length > 0)
            {
                for (int i = length; i < n_byte; i++)
                {
                    temp[i] = bytes_to_append[i_append++];
                }

                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] { temp[i], temp[i + 1], temp[i + 2], temp[i + 3] }));
                }
                messageChunks.Add(chunk);
            }
            //Console.WriteLine("Length of Bit Append :" + bytes_to_append.Count());
            //Console.WriteLine("Index of Bit Append :" + i_append);
            while (i_append < bytes_to_append.Count())
            {
                chunk = new Chunk();
                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] { bytes_to_append[i_append], bytes_to_append[i_append+1],
                    bytes_to_append[i_append+2], bytes_to_append[i_append+3] }));
                    i_append += 4;
                }
                messageChunks.Add(chunk);
            }

            fs.Close();
        }
Ejemplo n.º 2
0
        private void ChunkMessage(string message)
        {
            messageChunks.Clear();

            int n_byte = nBitPerBlock / 8;
            int offset = 0;

            byte[] temp = new byte[n_byte];

            int length = (message.Length - offset) > n_byte ? n_byte : message.Length - offset;

            Encoding.ASCII.GetBytes(message.Substring(offset, length)).CopyTo(temp, 0);

            Chunk chunk;

            while (length == n_byte)
            {
                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] {
                        temp[i], temp[i + 1], temp[i + 2], temp[i + 3]
                    }));
                }

                messageChunks.Add(chunk);

                offset += length;
                length  = (message.Length - offset) > n_byte ? n_byte : message.Length - offset;
                Encoding.ASCII.GetBytes(message.Substring(offset, length)).CopyTo(temp, 0);
                Console.WriteLine(Encoding.ASCII.GetString(temp));
            }
            //Console.WriteLine(Encoding.ASCII.GetString(temp) + ":" + length);

            int k_zero = n_byte - ((int)(message.Length % n_byte)) - 64 / 8;

            if (k_zero == 0)
            {
                k_zero = 448 / 8;
            }

            //Console.WriteLine("Zero adding : " + k_zero);

            byte[] bytes_to_append = new byte[k_zero];
            bytes_to_append[0] = 128;
            for (int i = 1; i < k_zero; i++)
            {
                bytes_to_append[i] = 0;
            }

            bytes_to_append = bytes_to_append.Concat(LongToBytes(message.Length * 8, 64 / 8)).ToArray();

            int i_append = 0;

            if (length > 0)
            {
                for (int i = length; i < n_byte; i++)
                {
                    temp[i] = bytes_to_append[i_append++];
                }

                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] {
                        temp[i], temp[i + 1], temp[i + 2], temp[i + 3]
                    }));
                }
                messageChunks.Add(chunk);
            }
            //Console.WriteLine("Length of Bit Append :" + bytes_to_append.Count());
            //Console.WriteLine("Index of Bit Append :" + i_append);
            while (i_append < bytes_to_append.Count())
            {
                chunk = new Chunk();
                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] {
                        bytes_to_append[i_append], bytes_to_append[i_append + 1],
                        bytes_to_append[i_append + 2], bytes_to_append[i_append + 3]
                    }));
                    i_append += 4;
                }
                messageChunks.Add(chunk);
            }
        }
Ejemplo n.º 3
0
        private void ChunkMessage(string message)
        {
            messageChunks.Clear();

            int n_byte = nBitPerBlock / 8;
            int offset = 0;
            byte[] temp = new byte[n_byte];

            int length = (message.Length-offset) > n_byte ? n_byte : message.Length-offset;
            Encoding.ASCII.GetBytes(message.Substring(offset, length)).CopyTo(temp, 0);

            Chunk chunk;

            while (length == n_byte)
            {
                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] { temp[i], temp[i + 1], temp[i + 2], temp[i + 3] }));
                }

                messageChunks.Add(chunk);

                offset += length;
                length = (message.Length-offset) > n_byte ? n_byte : message.Length-offset;
                Encoding.ASCII.GetBytes(message.Substring(offset, length)).CopyTo(temp, 0);
                Console.WriteLine(Encoding.ASCII.GetString(temp));
            }
            //Console.WriteLine(Encoding.ASCII.GetString(temp) + ":" + length);

            int k_zero = n_byte - ((int)(message.Length % n_byte)) - 64 / 8;
            if (k_zero == 0) k_zero = 448 / 8;

            //Console.WriteLine("Zero adding : " + k_zero);

            byte[] bytes_to_append = new byte[k_zero];
            bytes_to_append[0] = 128;
            for (int i = 1; i < k_zero; i++)
            {
                bytes_to_append[i] = 0;
            }

            bytes_to_append = bytes_to_append.Concat(LongToBytes(message.Length * 8, 64 / 8)).ToArray();

            int i_append = 0;
            if (length > 0)
            {
                for (int i = length; i < n_byte; i++)
                {
                    temp[i] = bytes_to_append[i_append++];
                }

                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] { temp[i], temp[i + 1], temp[i + 2], temp[i + 3] }));
                }
                messageChunks.Add(chunk);
            }
            //Console.WriteLine("Length of Bit Append :" + bytes_to_append.Count());
            //Console.WriteLine("Index of Bit Append :" + i_append);
            while (i_append < bytes_to_append.Count())
            {
                chunk = new Chunk();
                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] { bytes_to_append[i_append], bytes_to_append[i_append+1],
                    bytes_to_append[i_append+2], bytes_to_append[i_append+3] }));
                    i_append += 4;
                }
                messageChunks.Add(chunk);
            }
        }
Ejemplo n.º 4
0
        private void ChunkMessageFile(string fileName)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            messageChunks.Clear();

            int n_byte = nBitPerBlock / 8;
            int offset = 0;

            byte[] temp   = new byte[n_byte];
            int    length = fs.Read(temp, offset, n_byte);

            //Console.WriteLine(Encoding.ASCII.GetString(temp));
            Chunk chunk;

            while (length == n_byte)
            {
                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] {
                        temp[i], temp[i + 1], temp[i + 2], temp[i + 3]
                    }));
                }

                messageChunks.Add(chunk);

                offset += length;
                length  = fs.Read(temp, 0, n_byte);
                //Console.WriteLine(Encoding.ASCII.GetString(temp));
            }
            //Console.WriteLine(Encoding.ASCII.GetString(temp) +":"+ length);

            int k_zero = n_byte - ((int)(fs.Length % n_byte)) - 64 / 8;

            if (k_zero == 0)
            {
                k_zero = 448 / 8;
            }

            //Console.WriteLine("Zero adding : "+ k_zero);

            byte[] bytes_to_append = new byte[k_zero];
            bytes_to_append[0] = 128;
            for (int i = 1; i < k_zero; i++)
            {
                bytes_to_append[i] = 0;
            }

            bytes_to_append = bytes_to_append.Concat(LongToBytes(fs.Length * 8, 64 / 8)).ToArray();

            int i_append = 0;

            if (length > 0)
            {
                for (int i = length; i < n_byte; i++)
                {
                    temp[i] = bytes_to_append[i_append++];
                }

                chunk = new Chunk();

                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] {
                        temp[i], temp[i + 1], temp[i + 2], temp[i + 3]
                    }));
                }
                messageChunks.Add(chunk);
            }
            //Console.WriteLine("Length of Bit Append :" + bytes_to_append.Count());
            //Console.WriteLine("Index of Bit Append :" + i_append);
            while (i_append < bytes_to_append.Count())
            {
                chunk = new Chunk();
                for (int i = 0; i < n_byte; i += 4)
                {
                    chunk.Add32Bit(BytesToUInt(new byte[4] {
                        bytes_to_append[i_append], bytes_to_append[i_append + 1],
                        bytes_to_append[i_append + 2], bytes_to_append[i_append + 3]
                    }));
                    i_append += 4;
                }
                messageChunks.Add(chunk);
            }

            fs.Close();
        }