示例#1
0
        public void DecryptTest()
        {
            string key      = "CONVENIENCE";
            string input    = "HEESPNI RR SSEES EIY A SCBT EMGEPN ANDI CT RTAHSO IEERO";
            string expected = "HEREISASECRETMESSAGEENCIPHEREDBYTRANSPOSITION";

            ColumnarTranspositionC ct = new ColumnarTranspositionC(key);
            string output             = ct.Decrypt(input);

            Assert.AreEqual(output, expected);
        }
示例#2
0
        private void filesencryptDecryptPressed(object sender, RoutedEventArgs e)
        {
            // setup outcome list
            outcomeScrollViewerList.Clear();

            // init key, algorithm and temp fields
            string buttonName    = ((Button)sender).Name;
            string userKey       = fileskeyTextBox.Text.ToString();
            string userInput     = "";
            string encrypted     = "";
            string decrypted     = "";
            string status        = "";
            string algorithmName = "";
            Cipher algorithm;

            // needed cause compiler cries about algorithm not being set
            algorithm = new RailFence(3);
            // check for encryption status
            if (buttonName == "filesencrypt")
            {
                status = "encrypting";
            }
            else
            {
                status = "decrypting";
            }

            parseColumnarTranspKey(userKey);

            // inicialize algorithm with key
            switch (currentAlgorithm)
            {
            case "RAIL_FENCE":
                if (fileskeyTextBox.Text == "")
                {
                    MessageBox.Show("Rail fence key is emtpy. Please type in something.");
                    return;
                }
                if (validateRailfenceFields(fileskeyTextBox.Text.ToString()))
                {
                    int i = int.Parse(userKey);
                    algorithm     = new RailFence(i);
                    algorithmName = "rail fence";
                }
                else
                {
                    return;
                }
                break;

            case "COLUMNAR_TRANSP":
                if (fileskeyTextBox.Text == "")
                {
                    MessageBox.Show("Columnar transp key is emtpy. Please type in something.");
                    return;
                }
                int[] inputTab = parseColumnarTranspKey(userKey);
                if (validateColumnarTranspkey(inputTab))
                {
                    algorithm     = new ColumnarTransposition(parseColumnarTranspKey(userKey));
                    algorithmName = "columnar transposition";
                }
                else
                {
                    return;
                }
                break;

            case "MATRIX_TRANSP":
                if (fileskeyTextBox.Text == "")
                {
                    MessageBox.Show("Matrix transp key is emtpy. Please type in something.");
                    return;
                }
                if (validateMatrixTransp(userKey))
                {
                    algorithm = new MatrixTransp(parseMatrixTranspKey(userKey));
                }
                else
                {
                    return;
                }
                break;

            case "COLUMNAR_C":
                if (fileskeyTextBox.Text == "")
                {
                    MessageBox.Show("Matrix transp version C text input is empty. Please type in something.");
                    return;
                }
                if (validateMatrixTranspVerCKey(userKey))
                {
                    algorithm = new ColumnarTranspositionC(userKey);
                }
                else
                {
                    return;
                }
                break;

            case "ViGENERE":
                if (fileskeyTextBox.Text == "")
                {
                    MessageBox.Show("Winegret text input is empty. Please type in something.");
                    return;
                }
                if (validateVinegretKey(userKey))
                {
                    algorithm = new Vigenere(userKey);
                }
                else
                {
                    return;
                }
                break;

            case "CEZAR":
                if (fileskeyTextBox.Text == "")
                {
                    MessageBox.Show("Cezar text input is empty. Please type in something.");
                    return;
                }
                if (validateCezarKey(userKey))
                {
                    int i = int.Parse(userKey);
                    algorithm = new Cezar(i);
                }
                else
                {
                    return;
                }
                break;

            default:
                return;
            }

            // depending on the status move through input list and update outcome listsdfafsdafsadfsad

            string currentDate = DateTime.Now.ToString().Replace(':', ' ').Replace('/', ' ');


            if (status == "encrypting")
            {
                if (listOfLines.Count > textFileLinesAmountBreakPoint)
                {
                    TextWriter tw      = new StreamWriter("EncryptedListOfWords - " + currentAlgorithm + " - " + currentDate + ".txt");
                    string     outcome = "";
                    foreach (string s in listOfLines)
                    {
                        outcome = algorithm.Encrypt(s);
                        tw.WriteLine(outcome);
                    }
                    tw.Close();
                }
                else
                {
                    foreach (string line in userInputScrollList)
                    {
                        userInput = line;
                        encrypted = algorithm.Encrypt(line);
                        outcomeScrollViewerList.Add(encrypted);
                    }
                }
            }
            else
            {
                if (listOfLines.Count > textFileLinesAmountBreakPoint)
                {
                    TextWriter tw      = new StreamWriter("DecryptedListOfWords - " + currentAlgorithm + " - " + currentDate + ".txt");
                    string     outcome = "";
                    foreach (string s in listOfLines)
                    {
                        outcome = algorithm.Decrypt(s);
                        tw.WriteLine(outcome);
                    }
                    tw.Close();
                }
                else
                {
                    foreach (string line in userInputScrollList)
                    {
                        userInput = line;
                        decrypted = algorithm.Decrypt(line);
                        outcomeScrollViewerList.Add(decrypted);
                    }
                }
            }
            if (listOfLines.Count > textFileLinesAmountBreakPoint)
            {
                MessageBox.Show("Check app's folder for outcome of this operation. ");
                outcomeScrollViewerList.Add("Check your app's folder for");
                if (status == "encrypting")
                {
                    outcomeScrollViewerList.Add("EncryptedListOfWords - ");
                }
                else
                {
                    outcomeScrollViewerList.Add("DecryptedListOfWords - ");
                }
                outcomeScrollViewerList.Add(currentAlgorithm);
                outcomeScrollViewerList.Add(" - ");
                outcomeScrollViewerList.Add(currentDate);
                outcomeScrollViewerList.Add(".txt");
            }
        }
示例#3
0
        private void encryptDecryptPressed(object sender, RoutedEventArgs e)
        {
            string buttonName = ((Button)sender).Name;

            string userInput = mTextBox.Text.ToString();
            string userKey   = keyTextBox.Text.ToString();

            string encrypted = "";
            string decrypted = "";

            bool normalDialog = true;

            switch (currentAlgorithm)
            {
            case "RAIL_FENCE":
                if (mTextBox.Text == "")
                {
                    MessageBox.Show("Rail fence text input is empty. Please type in something.");
                    return;
                }
                if (validateRailfenceFields(keyTextBox.Text.ToString()))
                {
                    int i = int.Parse(userKey);
                    rf        = new RailFence(i);
                    encrypted = rf.Encrypt(userInput);
                    decrypted = rf.Decrypt(userInput);
                }
                break;

            case "COLUMNAR_TRANSP":
                if (mTextBox.Text == "")
                {
                    MessageBox.Show("Columanr transp text input is empty. Please type in something.");
                    return;
                }
                int[] inputTab = parseColumnarTranspKey(userKey);
                if (validateColumnarTranspkey(inputTab))
                {
                    ct        = new ColumnarTransposition(parseColumnarTranspKey(userKey));
                    encrypted = ct.Encrypt(userInput);
                    decrypted = ct.Decrypt(userInput);
                }
                else
                {
                    return;
                }
                break;

            case "MATRIX_TRANSP":
                if (mTextBox.Text == "")
                {
                    MessageBox.Show("Matrix transp text input is empty. Please type in something.");
                    return;
                }
                if (validateMatrixTransp(userKey))
                {
                    mt        = new MatrixTransp(parseMatrixTranspKey(userKey));
                    encrypted = mt.Encrypt(userInput);
                    decrypted = mt.Decrypt(userInput);
                }
                else
                {
                    return;
                }
                break;

            case "COLUMNAR_C":
                if (mTextBox.Text == "")
                {
                    MessageBox.Show("Matrix transp version C text input is empty. Please type in something.");
                    return;
                }
                if (validateMatrixTranspVerCKey(userKey) && validateMatrixTranspVerCWord(userInput))
                {
                    ctc       = new ColumnarTranspositionC(userKey);
                    encrypted = ctc.Encrypt(userInput);
                    decrypted = ctc.Decrypt(userInput);
                }
                else
                {
                    return;
                }
                break;

            case "ViGENERE":
                if (mTextBox.Text == "")
                {
                    MessageBox.Show("Winegret text input is empty. Please type in something.");
                    return;
                }
                if (validateVinegretKey(userKey) && validateVinegretWord(userInput))
                {
                    vig       = new Vigenere(userKey);
                    encrypted = vig.Encrypt(userInput);
                    decrypted = vig.Decrypt(userInput);
                }
                else
                {
                    return;
                }
                break;

            case "CEZAR":
                if (mTextBox.Text == "")
                {
                    MessageBox.Show("Cezar text input is empty. Please type in something.");
                    return;
                }
                if (validateCezarKey(userKey) && validateCezarWord(userInput))
                {
                    int i = int.Parse(userKey);
                    cz        = new Cezar(i);
                    encrypted = cz.Encrypt(userInput);
                    decrypted = cz.Decrypt(userInput);
                }
                else
                {
                    return;
                }
                break;

            case "SYNC":
                if (syncFileName.Content.ToString() == " ")
                {
                    MessageBox.Show("SYNC file input is empty. Please type in something.");
                    return;
                }
                if (keyTextBox.Text == "")
                {
                    MessageBox.Show("SYNC key text input is empty. Please type in something.");
                    return;
                }
                if (!syncKeyGenerated)
                {
                    MessageBox.Show("SYNC key needs to be generated first, please press run and then stop button before proceeding.");
                    return;
                }
                normalDialog            = false;
                synchronousStreamCipher = new SynchronousStreamCipher(lsfrGen.GetSequence());
                TextWriter  tw                    = new StreamWriter("LFSR KEY USED.txt");
                List <bool> listOfBools           = lsfrGen.GetSequence();
                char        boolOutcome           = ' ';
                string      boolOutcomeCollection = "";
                int         ij                    = 0;
                foreach (bool b in listOfBools)
                {
                    if (b == true)
                    {
                        boolOutcome = '1';
                    }
                    else
                    {
                        boolOutcome = '0';
                    }
                    boolOutcomeCollection += boolOutcome;
                    ij++;

                    if (ij > 100)
                    {
                        ij = 0;
                        tw.WriteLine(boolOutcomeCollection);
                        boolOutcomeCollection = "";
                    }
                }
                tw.WriteLine(boolOutcomeCollection);
                tw.Close();

                if (buttonName == "encrypt")
                {
                    synchronousStreamCipher.Encrypt(syncFileName.Content.ToString());
                }
                else
                {
                    synchronousStreamCipher.Decrypt(syncFileName.Content.ToString());
                }
                MessageBox.Show("Encrypted/decrypted file is located in folder where your .exe file is ( most probably bin/debug ). You will also find text file that contains LFSR key in it there.");
                break;

            case "DES":
                if (syncFileName.Content.ToString() == " ")
                {
                    MessageBox.Show("DES file input is empty. Please type in something.");
                    return;
                }
                if (keyTextBox.Text == "")
                {
                    MessageBox.Show("DES key text input is empty. Please type in something.");
                    return;
                }
                if (!validateDesKey(userKey))
                {
                    return;
                }
                des = new DES(desKeyParsing(userKey));

                if (buttonName == "encrypt")
                {
                    des.EncryptFile(syncFileName.Content.ToString());
                }
                else
                {
                    des.DecryptFile(syncFileName.Content.ToString());
                }

                MessageBox.Show("Encrypted/decrypted file is located in folder where your .exe file is ( most probably bin/debug ).");
                break;

            default:
                break;
            }


            int length = encrypted.Length;

            outcomeLabel.FontSize = 30;
            if (length > 10)
            {
                outcomeLabel.FontSize = 20;
            }
            if (length > 20)
            {
                outcomeLabel.FontSize = 15;
            }

            if (normalDialog)
            {
                if (buttonName == "encrypt")
                {
                    if (encrypted != "")
                    {
                        outcomeTypeLabel.Content = "Encrypted:";
                    }
                    outcomeLabel.Content = encrypted;
                }
                else
                {
                    if (decrypted != "")
                    {
                        outcomeTypeLabel.Content = "Decrypted:";
                    }
                    outcomeLabel.Content = decrypted;
                }
            }
            else
            {
            }
        }