예제 #1
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));
        }
예제 #2
0
        public void RailFenceTestNewDec()
        {
            RailFence algorithm = new RailFence();
            string    plain     = algorithm.Decrypt(newCipher, newkey);

            Assert.IsTrue(plain.Equals(newPlain, StringComparison.InvariantCultureIgnoreCase));
        }
예제 #3
0
        public void DecryptBasic()
        {
            RailFence rf4    = new RailFence(4);
            var       output = rf4.Decrypt("DTTEDHSWFNEAALEEL");

            Assert.AreEqual(BasicText, output);
        }
예제 #4
0
        public void RailFenceTestDec1()
        {
            RailFence algorithm = new RailFence();
            string    plain     = algorithm.Decrypt(mainCipher, mainKey);

            Assert.IsTrue(plain.Equals(mainPlain1, StringComparison.InvariantCultureIgnoreCase));
        }
예제 #5
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);
                }
            }
        }
예제 #6
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);
            }
        }
예제 #7
0
        public void ReturnsUnchangedWordForVeryLongKeyDecryption()
        {
            string input = "Dsadajkaajddjh";

            RailFence railFence = new RailFence(100);

            string decrypted = railFence.Decrypt(input);

            Assert.AreEqual(input, decrypted);
        }
예제 #8
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);
        }
예제 #9
0
        public void ReturnsDecryptedWordForKeyShorterThanWord()
        {
            string input    = "CTARPORPYYGH";
            string expected = "CRYPTOGRAPHY";

            RailFence railFence = new RailFence(3);

            string decrypted = railFence.Decrypt(input);

            Assert.AreEqual(decrypted, expected);
        }
예제 #10
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;
        }
예제 #11
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);
        }
예제 #12
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
            {
            }
        }
        public void RailfenceDecode(string input, int key, string output)
        {
            var result = railFence.Decrypt(input, key);

            Assert.AreEqual(output, result);
        }
예제 #14
0
 private void RailFenceDecrypt_Click(object sender, EventArgs e)
 {
     RailFenceOutput.Text = RailFence.Decrypt(RailFenceInput.Text, Convert.ToInt32(Rails.Value));
 }
예제 #15
0
 private void button3_Click(object sender, EventArgs e)
 {
     if (comboBox1.Text.Contains("Ceaser"))
     {
         Ceaser c   = new Ceaser();
         string Res = c.Decrypt(textBox2.Text.ToString(), int.Parse(textBox3.Text.ToString()));
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("Monoalphabetic"))
     {
         Monoalphabetic c   = new Monoalphabetic();
         string         Res = c.Decrypt(textBox2.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.Decrypt(textBox2.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 < textBox2.Text.Length; i++)
             {
                 Plaintext1.Add(int.Parse(textBox2.Text[i].ToString()));
             }
             for (int i = 0; i < textBox3.Text.Length; i++)
             {
                 key1.Add(int.Parse(textBox3.Text[i].ToString()));
             }
             ResDig        = c.Decrypt(Plaintext1, key1);
             textBox4.Text = ResDig.ToString();
         }
         else
         {
             Res           = c.Decrypt(textBox2.Text.ToString(), textBox3.Text.ToString());
             textBox4.Text = Res;
         }
     }
     else if (comboBox1.Text.Contains("PlayFair"))
     {
         PlayFair c   = new PlayFair();
         string   Res = c.Decrypt(textBox2.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RailFence"))
     {
         RailFence c   = new RailFence();
         string    Res = c.Decrypt(textBox2.Text.ToString(), int.Parse(textBox3.Text.ToString()));
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RepeatingKeyVigenere"))
     {
         RepeatingkeyVigenere c = new RepeatingkeyVigenere();
         string Res             = c.Decrypt(textBox2.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("AutokeyVigenere"))
     {
         AutokeyVigenere c   = new AutokeyVigenere();
         string          Res = c.Decrypt(textBox2.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.Decrypt(p, q, M, ee);
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("AES"))
     {
         AES    c   = new AES();
         string Res = c.Decrypt(textBox2.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
 }