コード例 #1
0
 private void Cipher_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     EncodedText.Clear();
     DecodedText.Clear();
     tbTextfromDecrHash.Clear();
     tbKey.Clear();
     if (Cipher.SelectedIndex == 0 || Cipher.SelectedIndex == 2)
     {
         tbKey.IsHitTestVisible = true;
     }
     else
     {
         tbKey.IsHitTestVisible = false;
         tbKey.Clear();
     }
 }
コード例 #2
0
        private void EncodeText(object sender, RoutedEventArgs e)
        {
            // Caesar cipher encode
            if (Cipher.Text == "Caesar cipher" && tbKey.Text != "")
            {
                lbDecText.Content = "  Your Decoded text";
                Decode.Content    = "Decode";
                key = Convert.ToInt32(tbKey.Text);
                if (EncodedText.Text != "")
                {
                    EncodedText.Clear();
                }
                try
                {
                    foreach (var item in charsToRemove)
                    {
                        YourText.Text = YourText.Text.Replace(item, string.Empty);
                    }

                    char[] text = YourText.Text.ToCharArray().Where(s => !char.IsWhiteSpace(s)).ToArray();

                    for (int i = 0; i < text.Length; i++)
                    {
                        for (int j = 0; j <= alphabet.Length && j <= alphabetUpper.Length; j++)
                        {
                            if (text[i].ToString() == alphabet[j].ToString() || text[i].ToString() == alphabetUpper[j].ToString())
                            {
                                char.ToLower(text[i]);

                                EncodedText.Text += Convert.ToChar(alphabet[(j + key) % alphabet.Length]);
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            // HASHFUNC encode
            if (Cipher.Text == "HashFunc")
            {
                lbDecText.Content = "  Comparable hash";
                Decode.Content    = "Check!";
                string text = YourText.Text;

                if (string.IsNullOrEmpty(text))
                {
                    text = string.Empty;
                }

                using (System.Security.Cryptography.SHA512Managed sha = new System.Security.Cryptography.SHA512Managed())
                {
                    byte[] textData = Encoding.Default.GetBytes(text);
                    byte[] hash     = sha.ComputeHash(textData);
                    EncodedText.Text = BitConverter.ToString(hash).Replace("-", string.Empty);
                }
                current = EncodedText.Text;
            }

            //  Vigenere cipher encode
            if (Cipher.Text == "Vigenere cipher")
            {
                lbDecText.Content = "  Your Decoded text";
                Decode.Content    = "Decode";
                if (EncodedText.Text != "")
                {
                    EncodedText.Clear();
                }
                foreach (var item in charsToRemove)
                {
                    YourText.Text = YourText.Text.Replace(item, string.Empty);
                }
                char[] text = YourText.Text.ToCharArray().Where(s => !char.IsWhiteSpace(s)).ToArray();
                char[] key  = tbKey.Text.ToCharArray();
                try
                {
                    char[,] Vigenere_Table = new char[26, 26];

                    int temp = 0;
                    for (int i = 0; i < alphabet.Length; i++)
                    {
                        for (int j = 0; j < 26; j++)
                        {
                            temp = j + i;
                            if (temp >= 26)
                            {
                                temp = temp % 26;
                            }
                            Vigenere_Table[i, j] = alphabet[temp];
                        }
                    }

                    for (int t = 0, k = 0; t < text.Length || k < key.Length; t++, k++)
                    {
                        if (t >= text.Length)
                        {
                            break;
                        }
                        if (k == key.Length /*t % key.Length == 0*/)
                        {
                            k = 0;
                            for (int y = 0; y <= alphabet.Length; y++)
                            {
                                if (text[t].ToString() == alphabet[y].ToString())
                                {
                                    Ytext = y;
                                    for (int x = 0; x <= alphabet.Length; x++)
                                    {
                                        if (key[k].ToString() == alphabet[x].ToString())
                                        {
                                            Xkey              = x;
                                            EncodedText.Text += Vigenere_Table[Ytext, Xkey].ToString();
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        else
                        {
                            for (int y = 0; y <= alphabet.Length; y++)
                            {
                                if (text[t].ToString() == alphabet[y].ToString())
                                {
                                    Ytext = y;
                                    for (int x = 0; x <= alphabet.Length; x++)
                                    {
                                        if (key[k].ToString() == alphabet[x].ToString())
                                        {
                                            Xkey              = x;
                                            EncodedText.Text += Vigenere_Table[Ytext, Xkey].ToString();
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }