Exemplo n.º 1
0
        public void decryptMessage()
        {
            string message = "0123456789ABCDEF";

            BitArray excepted_message;
            string excepted_message_string = "00000001 00100011 01000101 01100111 10001001 10101011 11001101 11101111";

            excepted_message = this.fromStringToBitArray(excepted_message_string);

            Key key = new Key("133457799BBCDFF1");
            key.initialPermutation();
            key.splitting();
            key.shifts();
            key.finalPermutation();

            Message test_message = new Message(message, 0);
            test_message.initialPermutation();
            test_message.splitting();

            Iteration.setKeys(key.keys);
            Iteration iteration = new Iteration(test_message.msg_left_side, test_message.msg_right_side);
            for (int i = 1; i <= 16; i++)
            {
                iteration.ePermutation(i);
                iteration.xorWithKey(i);
                iteration.sBoxing(i);
                iteration.pPermutation(i);
                iteration.afterIteration(i);
            }
            test_message.reverseConnecting(iteration.leftSide[16], iteration.rightSide[16]);
            test_message.finalPermutation();

            test_message = new Message(test_message.bitMsg);
            test_message.initialPermutation();
            test_message.splitting();
            key.reverseKey();
            Iteration.setKeys(key.keys);
            iteration = new Iteration(test_message.msg_left_side, test_message.msg_right_side);
            for (int i = 1; i <= 16; i++)
            {
                iteration.ePermutation(i);
                iteration.xorWithKey(i);
                iteration.sBoxing(i);
                iteration.pPermutation(i);
                iteration.afterIteration(i);
            }
            test_message.reverseConnecting(iteration.leftSide[16], iteration.rightSide[16]);
            test_message.finalPermutation();

            CollectionAssert.AreEqual(excepted_message, test_message.bitMsg, "Excepted:\n" + this.bitArrayToString(excepted_message) + " Actual\n" + this.bitArrayToString(test_message.bitMsg));
        }
Exemplo n.º 2
0
Arquivo: Utils.cs Projeto: WPiotr/BSK
        public static void Decrypt(string file_input, string file_output, string key1)
        {
            FileStream input;
            BinaryReader br;
            FileStream output;
            BinaryWriter bw;
            byte[] block;

            key = new Key(key1);
            Utils.key.makeKey();
            Utils.key.reverseKey();
            try
            {
                input = new FileStream(file_input, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(input);
                output = new FileStream(file_output, FileMode.Create, FileAccess.Write);
                bw = new BinaryWriter(output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            ////
            try
            {
                while ((block = br.ReadBytes(8)).Length > 0)
                {
                    BitArray encrypted_message = Utils.makeMessage(napraw(block));
                    bw.Write((encrypted_message.ToByteArray()));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                input.Close();
                output.Close();
                br.Close();
                bw.Close();
            }
            finally
            {
                input.Close();
                output.Close();
                br.Close();
                bw.Close();
            }
        }
Exemplo n.º 3
0
        public void createKey()
        {
            string key = "133457799BBCDFF1";

            BitArray excepted_key = new BitArray(64);
            string given_key_table = "00010011 00110100 01010111 01111001 10011011 10111100 11011111 11110001";
            for (int i = 0, j = 0; i < given_key_table.Length; i++)
            {
                if (given_key_table[i] == '0')
                {
                    excepted_key[j++] = false;
                }
                else if (given_key_table[i] == '1')
                {
                    excepted_key[j++] = true;
                }
            }
            Key test_key = new Key(key);

            CollectionAssert.AreEqual(excepted_key, test_key.bit_key, "Excepted:\n" + this.bitArrayToString(excepted_key) + " Actual\n" + this.bitArrayToString(test_key.bit_key));
        }
Exemplo n.º 4
0
        public void finalPermutationKey()
        {
            string key = "133457799BBCDFF1";
            string[] key_string = new string[17];

            key_string[1] = " 000110 110000 001011 101111 111111 000111 000001 110010";
            key_string[2] = " 011110 011010 111011 011001 110110 111100 100111 100101";
            key_string[3] = " 010101 011111 110010 001010 010000 101100 111110 011001";
            key_string[4] = " 011100 101010 110111 010110 110110 110011 010100 011101";
            key_string[5] = " 011111 001110 110000 000111 111010 110101 001110 101000";
            key_string[6] = " 011000 111010 010100 111110 010100 000111 101100 101111";
            key_string[7] = " 111011 001000 010010 110111 111101 100001 100010 111100";
            key_string[8] = " 111101 111000 101000 111010 110000 010011 101111 111011";
            key_string[9] = " 111000 001101 101111 101011 111011 011110 011110 000001";
            key_string[10] = " 101100 011111 001101 000111 101110 100100 011001 001111";
            key_string[11] = " 001000 010101 111111 010011 110111 101101 001110 000110";
            key_string[12] = " 011101 010111 000111 110101 100101 000110 011111 101001";
            key_string[13] = " 100101 111100 010111 010001 111110 101011 101001 000001";
            key_string[14] = " 010111 110100 001110 110111 111100 101110 011100 111010";
            key_string[15] = " 101111 111001 000110 001101 001111 010011 111100 001010";
            key_string[16] = " 110010 110011 110110 001011 000011 100001 011111 110101";

            BitArray[] excepted_keys = new BitArray[17];

            for (int i = 1; i < 17; i++)
            {
                excepted_keys[i] = this.fromStringToBitArray(key_string[i], 48);
            }
            Key test_key = new Key(key);
            test_key.initialPermutation();
            test_key.splitting();
            test_key.shifts();
            test_key.finalPermutation();
            for (int i = 1; i < 17; i++)
            {
                CollectionAssert.AreEqual(excepted_keys[i], test_key.keys[i], "Excepted:\n" + this.bitArrayToString(excepted_keys[i]) + " Actual\n" + this.bitArrayToString(test_key.keys[i]));
            }
        }
Exemplo n.º 5
0
Arquivo: Utils.cs Projeto: WPiotr/BSK
        public static void DecryptWithDeletingZero(string file_input, string file_output, string key1)
        {
            FileStream input;
            BinaryReader br;
            FileStream output;
            BinaryWriter bw;
            byte[] block;

            key = new Key(key1);
            Utils.key.makeKey();
            Utils.key.reverseKey();
            try
            {
                input = new FileStream(file_input, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(input);
                output = new FileStream(file_output, FileMode.Create, FileAccess.Write);
                bw = new BinaryWriter(output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            try
            {
                byte[] prev_block = new byte[8];
                byte[] prev_block_two = new byte[8];
                bool start = false;
                bool start2 = false;
                while ((block = br.ReadBytes(8)).Length > 0)
                {
                    if (start)
                    {
                        if (start2)
                        {

                            BitArray encrypted_message = Utils.makeMessage(napraw(prev_block_two));
                            bw.Write((encrypted_message.ToByteArray()));
                        }
                        start2 = true;
                        prev_block_two = prev_block;
                    }
                    start = true;
                    prev_block = block;

                }
                prev_block_two = Utils.makeMessage(napraw(prev_block_two)).ToByteArray();
                byte[] zero_count = Utils.makeMessage(napraw(prev_block)).ToByteArray();

                byte[] result_block = new byte[prev_block.Length - zero_count[0]/8];
                for (int i = 0; i < prev_block_two.Length - zero_count[0]; i++)
                {
                    result_block[i] = prev_block_two[i];
                }

                bw.Write((result_block));

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                input.Close();
                output.Close();
                br.Close();
                bw.Close();
            }
            finally
            {
                input.Close();
                output.Close();
                br.Close();
                bw.Close();
            }
        }
Exemplo n.º 6
0
Arquivo: Utils.cs Projeto: WPiotr/BSK
        public static void EncryptWithAddZero(string file_input, string file_output, string key1)
        {
            FileStream input;
            BinaryReader br;
            FileStream output;
            BinaryWriter bw;
            byte[] block;

            key = new Key(key1);
            Utils.key.makeKey();

            try
            {
                input = new FileStream(file_input, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(input);
                output = new FileStream(file_output, FileMode.Create, FileAccess.Write);
                bw = new BinaryWriter(output);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            try
            {
                while ((block = br.ReadBytes(8)).Length > 0)
                {
                    if (input.Position == input.Length)
                    {
                        byte[] lol = new byte[8];
                        for (int i = 0; i < 8; i++)
                        {
                            if (i < block.Length)
                            {
                                lol[i] = block[i];
                            }
                            else
                            {
                                lol[i] += (byte)0;
                            }
                        }
                        int block_do_zapisu = 8 - block.Length;

                        BitArray encrypted_message = Utils.makeMessage(napraw(lol));
                        bw.Write((encrypted_message.ToByteArray()));
                        BitArray b = new BitArray(new int[] { block_do_zapisu, 0 });

                        b = Utils.makeMessage(napraw(b.ToByteArray()));
                        bw.Write((b.ToByteArray()));
                    }
                    else
                    {
                        BitArray encrypted_message = Utils.makeMessage(napraw(block));
                        bw.Write((encrypted_message.ToByteArray()));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                input.Close();
                output.Close();
                br.Close();
                bw.Close();
            }
            finally
            {
                input.Close();
                output.Close();
                br.Close();
                bw.Close();
            }
        }
Exemplo n.º 7
0
        public void splittingKey()
        {
            string key = "133457799BBCDFF1";

            BitArray excepted_left_key;
            BitArray excepted_right_key;
            string key_left_side = "1111000 0110011 0010101 0101111";
            string key_right_side = "0101010 1011001 1001111 0001111";
            excepted_left_key = this.fromStringToBitArray(key_left_side, 28);
            excepted_right_key = this.fromStringToBitArray(key_right_side, 28);

            Key test_key = new Key(key);
            test_key.initialPermutation();
            test_key.splitting();
            CollectionAssert.AreEqual(excepted_left_key, test_key.key_left_side, "Excepted:\n" + this.bitArrayToString(excepted_left_key) + " Actual\n" + this.bitArrayToString(test_key.key_left_side));
            CollectionAssert.AreEqual(excepted_right_key, test_key.key_right_side, "Excepted:\n" + this.bitArrayToString(excepted_right_key) + " Actual\n" + this.bitArrayToString(test_key.key_right_side));
        }