Ejemplo n.º 1
0
        public BitArrayPair GenerateKeys(BitArray inputKey)
        {
            var keyPair = new BitArrayPair();

            keyPair.firstItem  = new BitArray(8);
            keyPair.secondItem = new BitArray(8);

            var workingKey = PermuteTen(inputKey);

            var splitKey = SplitBitArray(workingKey);

            splitKey.firstItem  = Shift(splitKey.firstItem, -1);
            splitKey.secondItem = Shift(splitKey.secondItem, -1);

            workingKey = JoinBitArrays(splitKey);

            workingKey        = PermuteEight(workingKey); //Key 1
            keyPair.firstItem = workingKey;

            splitKey.firstItem  = Shift(splitKey.firstItem, -2);
            splitKey.secondItem = Shift(splitKey.secondItem, -2);

            workingKey = JoinBitArrays(splitKey);

            workingKey         = PermuteEight(workingKey); //Key 2
            keyPair.secondItem = workingKey;

            return(keyPair);
        }
Ejemplo n.º 2
0
        // Same as Encrypt, just switch keys
        public BitArray Decrypt(BitArray plainText, BitArray key)
        {
            BitArrayPair keyPair = GenerateKeys(key);

            BitArrayPair splitBitArray = SplitBitArray(PermuteEightEncrypt(plainText));             //do not overwrite this
            BitArray     xorTemp       = EncryptExpand(splitBitArray.secondItem).Xor(keyPair.secondItem);

            BitArrayPair splitXoredArray = SplitBitArray(xorTemp);
            BitArrayPair sBoxArrays      = new BitArrayPair();

            sBoxArrays.firstItem = SBoxes(splitXoredArray.firstItem, new char[, ] {
                { '1', '0', '3', '2' },
                { '3', '2', '1', '0' },
                { '0', '2', '1', '3' },
                { '3', '1', '3', '2' }
            });

            sBoxArrays.secondItem = SBoxes(splitXoredArray.secondItem, new char[, ] {
                { '0', '1', '2', '3' },
                { '2', '0', '1', '3' },
                { '3', '0', '1', '0' },
                { '2', '1', '0', '3' }
            });

            BitArray temp = new BitArray(splitBitArray.firstItem);


            BitArray firstHalfOfDecrypt = temp.Xor(EncryptPermuteFour(JoinBitArrays(sBoxArrays)));

            xorTemp         = EncryptExpand(firstHalfOfDecrypt).Xor(keyPair.firstItem);
            splitXoredArray = SplitBitArray(xorTemp);

            sBoxArrays.firstItem = SBoxes(splitXoredArray.firstItem, new char[, ] {
                { '1', '0', '3', '2' },
                { '3', '2', '1', '0' },
                { '0', '2', '1', '3' },
                { '3', '1', '3', '2' }
            });

            sBoxArrays.secondItem = SBoxes(splitXoredArray.secondItem, new char[, ] {
                { '0', '1', '2', '3' },
                { '2', '0', '1', '3' },
                { '3', '0', '1', '0' },
                { '2', '1', '0', '3' }
            });

            BitArray     secondHalfOfDecrypt = splitBitArray.secondItem.Xor(EncryptPermuteFour(JoinBitArrays(sBoxArrays)));
            BitArrayPair bothHalves          = new BitArrayPair();

            bothHalves.firstItem  = secondHalfOfDecrypt;
            bothHalves.secondItem = firstHalfOfDecrypt;

            BitArray DecryptedOutput = PermuteEightEncryptInverted(JoinBitArrays(bothHalves));

            return(DecryptedOutput);
        }
Ejemplo n.º 3
0
        public BitArray JoinBitArrays(BitArrayPair input)
        {
            BitArray output = new BitArray(input.firstItem.Count + input.secondItem.Count);

            for (int i = 0; i < input.firstItem.Count; i++)
            {
                output[i] = input.firstItem[i];
            }

            for (int i = 0; i < input.secondItem.Count; i++)
            {
                output[i + input.firstItem.Count] = input.secondItem[i];
            }

            return(output);
        }
Ejemplo n.º 4
0
        public BitArrayPair SplitBitArray(BitArray input)
        {
            BitArrayPair splitArrays = new BitArrayPair();
            int          midway      = input.Count / 2;

            splitArrays.firstItem  = new BitArray(midway);
            splitArrays.secondItem = new BitArray(midway);

            for (int i = 0; i < midway; i++)
            {
                splitArrays.firstItem[i] = input[i];
            }

            for (int i = midway; i < input.Count; i++)
            {
                splitArrays.secondItem[i - midway] = input[i];
            }

            return(splitArrays);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            BitArray     userKey         = new BitArray(10);
            BitArray     key_1           = new BitArray(8);
            BitArray     key_2           = new BitArray(8);
            BitArray     userText        = new BitArray(8);
            Tools        tools           = new Tools();
            BitArrayPair keys            = new BitArrayPair();
            BitArray     outText         = new BitArray(8);
            string       UserChoice      = "";
            bool         exit            = false;
            bool         validInput      = false;
            string       validCharacters = "eEdDqQ";

            // Loop to allow user to replay
            while (!exit)
            {
                Console.WriteLine("Welcome!");
                Console.WriteLine("Enter Q or q at anytime to quit");

                // Encrypt or Decrypt
                validInput = false;
                while (!validInput)
                {
                    Console.WriteLine("Would you like to encrypt or decrypt your 8 bit input?");
                    Console.WriteLine("E/D.......");
                    UserChoice = Console.ReadLine();
                    if (validCharacters.Contains(UserChoice[0]))
                    {
                        validInput = true;
                        if (UserChoice[0] == 'Q' || UserChoice[0] == 'q')
                        {
                            exit = true;
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please enter correct input...");
                    }
                }

                // Get user plain/ciphertext
                validInput = false;
                while (!validInput && !exit)
                {
                    if (UserChoice[0] == 'e' || UserChoice[0] == 'E')
                    {
                        Console.WriteLine("Please enter your 8 bit plaintext for encryption:");
                    }
                    else
                    {
                        Console.WriteLine("Please enter your 8 bit ciphertext for decryption:");
                    }

                    string userTextString = Console.ReadLine();
                    userText = new BitArray(userTextString.Select(c => c == '1').ToArray());
                    if (userText.Length == 8)
                    {
                        validInput = true;
                        Console.WriteLine("Your eight bit text: " + tools.BitArrayToString(userText));
                    }
                    else
                    {
                        if (userTextString[0] == 'Q' || userTextString[0] == 'q')
                        {
                            exit = true;
                            break;
                        }
                        Console.WriteLine("Please enter exactly 8 bits (only 1 or 0) for the text...");
                    }
                }

                // Get 10 bit key
                validInput = false;
                while (!validInput && !exit)
                {
                    Console.WriteLine("Please enter a 10 Bit Key");
                    string userKeyString = Console.ReadLine();
                    userKey = new BitArray(userKeyString.Select(c => c == '1').ToArray());
                    if (userKey.Length == 10)
                    {
                        validInput = true;
                        Console.WriteLine("Your ten bit key: " + tools.BitArrayToString(userKey));
                    }
                    else
                    {
                        if (userKeyString[0] == 'Q' || userKeyString[0] == 'q')
                        {
                            exit = true;
                            break;
                        }
                        Console.WriteLine("Please enter exactly 10 bits (only 1 or 0) for the key...");
                    }
                }

                // Output Keys and plain/ciphertext here
                if (!exit)
                {
                    // Generate Keys here

                    keys = tools.GenerateKeys(userKey);

                    if (UserChoice[0] == 'e' || UserChoice[0] == 'E')
                    {
                        outText = tools.Encrypt(userText, userKey);
                        Console.WriteLine("Your encrypted ciphertext: " + tools.BitArrayToString(outText));
                    }
                    else
                    {
                        outText = tools.Decrypt(userText, userKey);
                        Console.WriteLine("Your Decrypted plaintext: " + tools.BitArrayToString(outText));
                    }
                    Console.WriteLine("Your 8 bit keys are listed below...");
                    Console.WriteLine("Key 1: " + tools.BitArrayToString(keys.firstItem));
                    Console.WriteLine("Key 2: " + tools.BitArrayToString(keys.secondItem));
                }

                // Ask user if they want to quit
                validInput = false;
                while (!validInput && !exit)
                {
                    Console.WriteLine("Would you like to exit?");
                    Console.WriteLine("Y/N.......");
                    UserChoice = Console.ReadLine();
                    if (UserChoice[0] == 'Y' || UserChoice[0] == 'y')
                    {
                        validInput = true;
                        exit       = true;
                        break;
                    }
                    if (UserChoice[0] == 'N' || UserChoice[0] == 'n')
                    {
                        validInput = true;
                    }
                    else
                    {
                        Console.WriteLine("Please enter correct input...");
                    }
                }
            }
            Console.WriteLine("Exiting!");
            Console.ReadKey();
        }