Beispiel #1
0
        private NotBitArray Coding_rounds(int round_count, NotBitArray block, NotBitArray[] Keys, bool is_encoding)
        {
            int start, finish, step;

            if (is_encoding)
            {
                start  = 1;
                step   = 1;
                finish = round_count + 1;
            }
            else
            {
                start  = round_count;
                step   = -1;
                finish = 0;
            }
            block = Permutation(block, IP_table);
            NotBitArray L_block = new NotBitArray(block.bits.Substring(0, Block_size / 2)),
                        R_block = new NotBitArray(block.bits.Substring(Block_size / 2));

            for (int i = start; i != finish; i += step)
            {
                NotBitArray buffer = R_block;

                R_block = L_block.Xor(F(R_block, Keys[i - 1]));
                L_block = buffer;
            }
            return(Permutation(new NotBitArray(R_block.bits + L_block.bits), IP_1_table));
        }
Beispiel #2
0
        NotBitArray Permutation(NotBitArray Input_array, int[] table)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < table.Length; i++)
            {
                sb.Append(Input_array.bits[table[i] - 1]);
            }
            return(new NotBitArray(sb.ToString()));
        }
Beispiel #3
0
        NotBitArray Get_CD_i(int round, NotBitArray CD)
        {
            int shift = round > 1 &&                    //  instead of shifts table;
                        (round + 1) % 9 != 0 &&         //  '%' to have some 1-bit shift
                        (round + 1) % 16 != 0 ? 2 : 1;  //  on more then 16 rounds

            return(new NotBitArray(new StringBuilder()
                                   .Append(CD.bits.Substring(shift, 28 - shift))
                                   .Append(CD.bits.Substring(0, shift))
                                   .Append(CD.bits.Substring(28 + shift, 28 - shift))
                                   .Append(CD.bits.Substring(28, shift)).ToString()));
        }   // Shifts C_i, D_i vectors
Beispiel #4
0
        NotBitArray F(NotBitArray B, NotBitArray K)
        {
            NotBitArray E_B = Permutation(B, E_table); // Extending

            E_B = E_B.Xor(K);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 8; i++)
            {
                NotBitArray B_i = new NotBitArray(E_B.bits.Substring(i * 6, 6)); // Get 6bit vector
                NotBitArray S_i = S(i, B_i);                                     // Change to 4bit
                sb.Append(S_i.bits);                                             // Add to result
            }
            return(Permutation(new NotBitArray(sb.ToString()), P_table));        // Final premutation for F(R_i, K_i)
        }
Beispiel #5
0
        byte[] DES(byte[] byte_block, byte[] byte_key, int round_count, bool is_encoding)
        {
            NotBitArray key   = new NotBitArray(byte_key);
            NotBitArray block = new NotBitArray(byte_block);

            NotBitArray CD_i = Permutation(key, G_table); // C_0 + D_0

            NotBitArray[] Keys = new NotBitArray[round_count];
            for (int i = 0; i < round_count; i++)
            {
                CD_i    = Get_CD_i(i, CD_i);
                Keys[i] = Permutation(CD_i, H_table);
            }
            NotBitArray Output = Coding_rounds(round_count, block, Keys, is_encoding);

            return(Output.ToByteArray());
        }
Beispiel #6
0
            public NotBitArray Xor(NotBitArray block)
            {
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < bits.Length; i++)
                {
                    if (this[i] == block[i])
                    {
                        sb.Append('0');
                    }
                    else
                    {
                        sb.Append('1');
                    }
                }
                return(new NotBitArray(sb.ToString()));
            }
Beispiel #7
0
        }   // Shifts C_i, D_i vectors

        NotBitArray S(int i, NotBitArray B_i)
        {
            int a = 0, b = 0;

            a += B_i[0] ? 2 : 0;
            a += B_i[5] ? 1 : 0;

            for (int b_count = 0; b_count < 4; b_count++)
            {
                b += B_i[4 - b_count] ? 1 << b_count : 0;
            }

            int[][] S_table;
            switch (i)
            {
            case 0:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7
                    },
                    new int[16] {
                        0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8
                    },
                    new int[16] {
                        4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0
                    },
                    new int[16] {
                        15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
                    }
                };
                break;
            }

            case 1:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10
                    },
                    new int[16] {
                        3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5
                    },
                    new int[16] {
                        0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15
                    },
                    new int[16] {
                        13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
                    }
                };
                break;
            }

            case 2:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8
                    },
                    new int[16] {
                        13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1
                    },
                    new int[16] {
                        13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7
                    },
                    new int[16] {
                        1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
                    }
                };
                break;
            }

            case 3:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15
                    },
                    new int[16] {
                        13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9
                    },
                    new int[16] {
                        10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4
                    },
                    new int[16] {
                        3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
                    }
                };
                break;
            }

            case 4:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9
                    },
                    new int[16] {
                        14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6
                    },
                    new int[16] {
                        4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14
                    },
                    new int[16] {
                        11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
                    }
                };
                break;
            }

            case 5:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11
                    },
                    new int[16] {
                        10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8
                    },
                    new int[16] {
                        9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6
                    },
                    new int[16] {
                        4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
                    }
                };
                break;
            }

            case 6:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1
                    },
                    new int[16] {
                        13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6
                    },
                    new int[16] {
                        1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2
                    },
                    new int[16] {
                        6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
                    }
                };
                break;
            }

            case 7:
            {
                S_table = new int[4][]
                {
                    new int[16] {
                        13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7
                    },
                    new int[16] {
                        1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2
                    },
                    new int[16] {
                        7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8
                    },
                    new int[16] {
                        2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
                    }
                };
                break;
            }

            default: return(null);
            }
            ;   // Get S table for i's B

            int B_out = S_table[a][b];

            StringBuilder sb = new StringBuilder();

            for (int c = 0; c < 4; c++)
            {
                sb.Append((B_out & (1 << c)) != 0 ? '1' : '0');
            }

            return(new NotBitArray(sb.ToString()));
        }