Beispiel #1
0
        public void RailFenceTestNewEnc()
        {
            RailFence algorithm = new RailFence();
            string    cipher    = algorithm.Encrypt(newPlain, newkey);

            Assert.IsTrue(cipher.Equals(newCipher, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #2
0
        public void RailFenceTestDec1()
        {
            RailFence algorithm = new RailFence();
            string    plain     = algorithm.Decrypt(mainCipher, mainKey);

            Assert.IsTrue(plain.Equals(mainPlain1, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #3
0
        public void EncryptBasic2()
        {
            RailFence rf4    = new RailFence(4);
            var       output = rf4.Encrypt(BasicText);

            Assert.AreEqual("DTTEDHSWFNEAALEEL", output);
        }
Beispiel #4
0
        public void RailFenceTestNewDec()
        {
            RailFence algorithm = new RailFence();
            string    plain     = algorithm.Decrypt(newCipher, newkey);

            Assert.IsTrue(plain.Equals(newPlain, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #5
0
        public void RailFenceTestNewAnalysis()
        {
            RailFence algorithm = new RailFence();
            int       key       = algorithm.Analyse(newPlain, newCipher);

            Assert.AreEqual(newkey, key);
        }
Beispiel #6
0
        public void DecryptBasic()
        {
            RailFence rf4    = new RailFence(4);
            var       output = rf4.Decrypt("DTTEDHSWFNEAALEEL");

            Assert.AreEqual(BasicText, output);
        }
Beispiel #7
0
        public void RailFenceTestEnc1()
        {
            RailFence algorithm = new RailFence();
            string    cipher    = algorithm.Encrypt(mainPlain1, mainKey);

            Assert.IsTrue(cipher.Equals(mainCipher, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #8
0
        public void Unigraph_RailFenceTest()
        {
            RailFence railfence = new RailFence(Utility.KeyedEnglishAlphabet("KRYPTOS"));

            cipher    = "";
            clear     = "";
            generated = "";

            byte[] tokenData = new byte[2];
            using (System.Security.Cryptography.RandomNumberGenerator rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
            {
                rng.GetBytes(tokenData);

                railfence.Key = (int)(BitConverter.ToUInt16(tokenData, 0) >> 8);

                for (int i = 0; i < 25; i++)
                {
                    generated = railfence.GenerateRandomString();

                    cipher = railfence.Encrypt(generated);
                    clear  = railfence.Decrypt(cipher);

                    Assert.AreEqual(generated, clear);
                }
            }
        }
Beispiel #9
0
        public void EncryptBasic()
        {
            RailFence rf3    = new RailFence(3);
            var       output = rf3.Encrypt(BasicText);

            Assert.AreEqual("DNETLEEDHESWLFTAA", output);
        }
Beispiel #10
0
        public void RailFenceTestAnalysis1()
        {
            RailFence algorithm = new RailFence();
            int       key       = algorithm.Analyse(mainPlain1, mainCipher);

            Assert.AreEqual(mainKey, key);
        }
Beispiel #11
0
        public void RailFenceTestAnalysis2()
        {
            RailFence algorithm = new RailFence();
            int       key       = algorithm.Analyse(mainPlain1, mainCipher2);
            int       key2      = algorithm.Analyse(mainPlain2, mainCipher3);

            Assert.IsTrue(mainKey2 == key || mainKey2 == key2);
        }
Beispiel #12
0
        public void RailFenceTestEnc2()
        {
            RailFence algorithm = new RailFence();
            string    cipher    = algorithm.Encrypt(mainPlain1, mainKey2);

            // Add x's or not
            Assert.IsTrue(cipher.Equals(mainCipher2, StringComparison.InvariantCultureIgnoreCase) ||
                          cipher.Equals(mainCipher3, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #13
0
        public void RailFenceTestDec2()
        {
            RailFence algorithm = new RailFence();
            string    plain1    = algorithm.Decrypt(mainCipher2, mainKey2);
            string    plain2    = algorithm.Decrypt(mainCipher3, mainKey2);

            Assert.IsTrue(plain1.Equals(mainPlain1, StringComparison.InvariantCultureIgnoreCase) ||
                          plain2.Equals(mainPlain2, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #14
0
        /// <summary>
        /// Performs one of the possible actions: encrypts or decrypts data.
        /// </summary>
        /// <param name="text">Data to be encrypted / decrypted.</param>
        /// <param name="key">The key for the encryption / decryption algorithm.</param>
        /// <param name="typeOfChiper">The name of the encryption / decryption algorithm.</param>
        /// <param name="action">Action to be taken (encrypt / decrypt).</param>
        /// <returns>The result of encryption / decryption.</returns>
        private static string PerformAction(string text, string key,
                                            TypesOfCiphers typeOfChiper, Model.Ciphers.Action action)
        {
            switch (typeOfChiper)
            {
            case TypesOfCiphers.RailFence:
                RailFence railFence = new RailFence();
                if (action == Model.Ciphers.Action.Encrypt)
                {
                    return(railFence.Encrypt(text, key));
                }
                else
                {
                    return(railFence.Decrypt(text, key));
                }

            case TypesOfCiphers.RotatingSquare:
                RotatingGrill rotatingSquare = new RotatingGrill();
                if (action == Model.Ciphers.Action.Encrypt)
                {
                    return(rotatingSquare.Encrypt(text));
                }
                else
                {
                    return(rotatingSquare.Decrypt(text));
                }

            case TypesOfCiphers.Vigenere:
                Vigenere vigener = new Vigenere();
                if (action == Model.Ciphers.Action.Encrypt)
                {
                    try
                    {
                        return(vigener.Encrypt(text, key));
                    }
                    catch (DivideByZeroException)
                    {
                        return(text);
                    }
                }
                else
                {
                    try
                    {
                        return(vigener.Decrypt(text, key));
                    }
                    catch (DivideByZeroException)
                    {
                        return(text);
                    }
                }

            default:
                return(null);
            }
        }
Beispiel #15
0
        private void RailAnalays_Click(object sender, EventArgs e)
        {
            RailFence railfence = new RailFence();

            string res = "";

            int key = railfence.Analyse(PlainText.Text, CipherText.Text);

            RailKey.Text = key.ToString();
        }
Beispiel #16
0
        public void ReturnsUnchangedWordFor1KeyEncryption()
        {
            string input = "Dsadajkaajddjh";

            RailFence railFence = new RailFence(1);

            string encrypted = railFence.Encrypt(input);

            Assert.AreEqual(input, encrypted);
        }
Beispiel #17
0
        public void ReturnsUnchangedWordForVeryLongKeyDecryption()
        {
            string input = "Dsadajkaajddjh";

            RailFence railFence = new RailFence(100);

            string decrypted = railFence.Decrypt(input);

            Assert.AreEqual(input, decrypted);
        }
 public void Setup()
 {
     railFence         = new RailFence();
     matrixShift       = new MatrixShift();
     matrixShiftB      = new MatrixShiftB();
     matrixShiftC      = new MatrixShiftC();
     ceasarCipher      = new CeasarCipher();
     vigenere          = new Vigenere();
     ciphertextAutokey = new CiphertextAutokey();
 }
Beispiel #19
0
        public void ReturnsDecryptedWordForKeyShorterThanWord()
        {
            string input    = "CTARPORPYYGH";
            string expected = "CRYPTOGRAPHY";

            RailFence railFence = new RailFence(3);

            string decrypted = railFence.Decrypt(input);

            Assert.AreEqual(decrypted, expected);
        }
Beispiel #20
0
        public void ReturnsInitialWordAfterBothDecryptionAndEncryption()
        {
            string input = "CRYPTOGRAPHY";

            RailFence railFence = new RailFence(3);

            string res       = railFence.Decrypt(input);
            string encrypted = railFence.Encrypt(res);

            Assert.AreEqual(encrypted, input);
        }
        public ActionResult <string> GetBreak(string method, string str)
        {
            try
            {
                if (method == "caesar")
                {
                    Caesar caesar = new Caesar();

                    var result = caesar.Break(str);

                    SaveBreakItems(method, str, result.Item1, result.Item2);

                    return(result.Item1 + "," + result.Item2.ToString());
                }
                else if (method == "railfence")
                {
                    RailFence railFence = new RailFence();

                    var result = railFence.Break(str);

                    SaveBreakItems(method, str, result.Item1, result.Item2);

                    return(result.Item1 + "," + result.Item2.ToString());
                }
                else if (method == "affine")
                {
                    Affine affine = new Affine();

                    var result = affine.Break(str);

                    SaveBreakItems(method, str, result.Item1, result.Item2);

                    return(result.Item1 + "," + result.Item2.ToString());
                }
                else if (method == "substitution")
                {
                    Substitution substitution = new Substitution();

                    var result = substitution.Break();

                    SaveBreakItems(method, str, result.Item1, result.Item2);

                    return(result.Item1 + "," + result.Item2.ToString());
                }
            }
            catch (System.Exception e)
            {
                return(e.Message);
            }

            return("No such method!");
        }
Beispiel #22
0
        private void RailFenceDecode(object sender, RoutedEventArgs e)
        {
            RailFence algorithm = new RailFence();
            string    value     = RailFenceDecodeInput.Text;
            int       key;

            try
            {
                key = Int32.Parse(RailFenceDecodeKeyInput.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Podano złą wagę", "Zła waga", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            RailFenceDecodeOutput.Text = algorithm.Decrypt(value, key);
        }
Beispiel #23
0
        private void RailDecrypt_Click(object sender, EventArgs e)
        {
            RailFence railfence = new RailFence();

            string res = "";

            if (RailKey.Text != null)
            {
                res = railfence.Decrypt(CipherText.Text, int.Parse(RailKey.Text));
            }
            else
            {
                MessageBox.Show("enter key value");
            }

            PlainText.Text = res;
        }
Beispiel #24
0
 private void button2_Click(object sender, EventArgs e)
 {
     if (comboBox1.Text.Contains("Ceaser"))
     {
         Ceaser c   = new Ceaser();
         int    Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("Monoalphabetic"))
     {
         Monoalphabetic c   = new Monoalphabetic();
         string         Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("Columnar"))
     {
         Columnar   c   = new Columnar();
         List <int> key = new List <int>();
         for (int i = 0; i < textBox3.Text.Length; i++)
         {
             key.Add(int.Parse(textBox3.Text[i].ToString()));
         }
         List <int> Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("HillCipher"))
     {
         HillCipher c          = new HillCipher();
         List <int> key1       = new List <int>();
         List <int> Plaintext1 = new List <int>();
         string     Res        = "";
         List <int> ResDig     = new List <int>();
         if (textBox5.Text == "2")
         {
             if (char.IsDigit(textBox1.Text[0]) && char.IsDigit(textBox2.Text[0]))
             {
                 for (int i = 0; i < textBox1.Text.Length; i++)
                 {
                     Plaintext1.Add(int.Parse(textBox1.Text[i].ToString()));
                 }
                 for (int i = 0; i < textBox2.Text.Length; i++)
                 {
                     key1.Add(int.Parse(textBox2.Text[i].ToString()));
                 }
                 ResDig        = c.Analyse(Plaintext1, key1);
                 textBox4.Text = ResDig.ToString();
             }
             else
             {
                 Res           = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
                 textBox4.Text = Res;
             }
         }
         else if (textBox5.Text == "3")
         {
             if (char.IsDigit(textBox1.Text[0]) && char.IsDigit(textBox2.Text[0]))
             {
                 for (int i = 0; i < textBox1.Text.Length; i++)
                 {
                     Plaintext1.Add(int.Parse(textBox1.Text[i].ToString()));
                 }
                 for (int i = 0; i < textBox2.Text.Length; i++)
                 {
                     key1.Add(int.Parse(textBox2.Text[i].ToString()));
                 }
                 ResDig        = c.Analyse3By3Key(Plaintext1, key1);
                 textBox4.Text = ResDig.ToString();
             }
             else
             {
                 Res           = c.Analyse3By3Key(textBox1.Text.ToString(), textBox2.Text.ToString());
                 textBox4.Text = Res;
             }
         }
     }
     else if (comboBox1.Text.Contains("RailFence"))
     {
         RailFence c   = new RailFence();
         int       Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("RepeatingKeyVigenere"))
     {
         RepeatingkeyVigenere c = new RepeatingkeyVigenere();
         string Res             = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("AutokeyVigenere"))
     {
         AutokeyVigenere c   = new AutokeyVigenere();
         string          Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res;
     }
 }
Beispiel #25
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (comboBox1.Text.Contains("Ceaser"))
     {
         Ceaser c   = new Ceaser();
         string Res = c.Encrypt(textBox1.Text.ToString(), int.Parse(textBox3.Text.ToString()));
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("Monoalphabetic"))
     {
         Monoalphabetic c   = new Monoalphabetic();
         string         Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("Columnar"))
     {
         Columnar   c   = new Columnar();
         List <int> key = new List <int>();
         for (int i = 0; i < textBox3.Text.Length; i++)
         {
             key.Add(int.Parse(textBox3.Text[i].ToString()));
         }
         string Res = c.Encrypt(textBox1.Text.ToString(), key);
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("HillCipher"))
     {
         HillCipher c          = new HillCipher();
         List <int> key1       = new List <int>();
         List <int> Plaintext1 = new List <int>();
         string     Res        = "";
         List <int> ResDig     = new List <int>();
         if (char.IsDigit(textBox3.Text[0]) && char.IsDigit(textBox1.Text[0]))
         {
             for (int i = 0; i < textBox1.Text.Length; i++)
             {
                 Plaintext1.Add(int.Parse(textBox1.Text[i].ToString()));
             }
             for (int i = 0; i < textBox3.Text.Length; i++)
             {
                 key1.Add(int.Parse(textBox3.Text[i].ToString()));
             }
             ResDig        = c.Encrypt(Plaintext1, key1);
             textBox4.Text = ResDig.ToString();
         }
         else
         {
             Res           = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
             textBox4.Text = Res;
         }
     }
     else if (comboBox1.Text.Contains("PlayFair"))
     {
         PlayFair c   = new PlayFair();
         string   Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RailFence"))
     {
         RailFence c   = new RailFence();
         string    Res = c.Encrypt(textBox1.Text.ToString(), int.Parse(textBox3.Text.ToString()));
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RepeatingKeyVigenere"))
     {
         RepeatingkeyVigenere c = new RepeatingkeyVigenere();
         string Res             = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("AutokeyVigenere"))
     {
         AutokeyVigenere c   = new AutokeyVigenere();
         string          Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RSA"))
     {
         RSA      c   = new RSA();
         string   s   = textBox1.Text.ToString();
         string[] str = s.Split(' ');
         int      p   = int.Parse(str[0]);
         int      q   = int.Parse(str[1]);
         int      M   = int.Parse(str[2]);
         int      ee  = int.Parse(str[3]);
         int      Res = c.Encrypt(p, q, M, ee);
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("AES"))
     {
         AES    c   = new AES();
         string Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("MD5"))
     {
         MD5    c   = new MD5();
         string Res = c.GetHash(textBox1.Text.ToString());
         textBox4.Text = Res;
     }
 }
Beispiel #26
0
 private void RailFenceDecrypt_Click(object sender, EventArgs e)
 {
     RailFenceOutput.Text = RailFence.Decrypt(RailFenceInput.Text, Convert.ToInt32(Rails.Value));
 }
        public ActionResult <string> GetEncrypt(string method, string str, string key)
        {
            try
            {
                if (method == "caesar")
                {
                    Caesar caesar = new Caesar();

                    var result = caesar.Encode(str, key);

                    if (result.Item2 == true)
                    {
                        SaveEncryptItems(method, str, key, result.Item1);

                        return(result.Item1);
                    }
                }
                else if (method == "railfence")
                {
                    RailFence railFence = new RailFence();

                    var result = railFence.Encode(str, key);

                    if (result.Item2 == true)
                    {
                        SaveEncryptItems(method, str, key, result.Item1);

                        return(result.Item1);
                    }
                }
                else if (method == "affine")
                {
                    Affine affine = new Affine();

                    var result = affine.Encode(str, key);

                    if (result.Item2 == true)
                    {
                        SaveEncryptItems(method, str, key, result.Item1);

                        return(result.Item1);
                    }
                }
                else if (method == "substitution")
                {
                    Substitution substitution = new Substitution();

                    var result = substitution.Encode(str, key);

                    if (result.Item2 == true)
                    {
                        SaveEncryptItems(method, str, key, result.Item1);

                        return(result.Item1);
                    }
                }
            }
            catch (System.Exception e)
            {
                return(e.Message);
            }

            return("No such method!");
        }
Beispiel #28
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
            {
            }
        }
Beispiel #29
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");
            }
        }