Vigenere Cipher

                            
     char encryptedChar;
     string encryptedText = "";
     int charIndexOfText, charIndexOfPassword, newIndex;
     char charOfText;
     char charOfPassword;
     string table = "";
     string result = "";
     alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     string text = txtPlainText.Text;
     string password = txtPass.Text;

     charIndexOfText = charIndexOfPassword = newIndex = 0;

     //If want to use both key and password else it will only use password
     if (AdvancedRadioButton.Checked)
     {
        //Removes duplicate characters from the string "alphabets"
        //For example: if the key is "MRINMAY"
        //The string "alphabets" will be modified to "MRINAYBCDEFGHJKLOPQSTUVWXZ"
        string key = RemoveDuplicateChars(txtKey.Text);
        alphabets = RemoveDuplicateChars(key.ToUpper() + alphabets);
     }
     else// If basic radio button is selected
     {
        alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     }

     //look for white space in the password and discard it
     //it is very that, a vigenere cipher's password should not have white space
     foreach (char value in password)
     {
        // if character is white space.
        if (value == ' ')
        {
            //do nothing
        }
        else
        {
            // Append valid character to the table and the result.
            table += value;
            result += value;
        }

        //password without white space
        password = result;
     }

     for (int i = 0; i < text.Length; i++)
     {
        for (int j = 0; j < password.Length; j++)
        {
            if (i < text.Length)
            {
                //get each character of the text and the password
                //Note: I have used only Upper Case alphabets
                //but if you want to include lower case alphabets, add lower case alphabets(a-z) to string "alphabets"
                //replace the next two lines of code with this:/* charOfText = text[i]; charOfPassword = password[j];*/
                charOfText = char.ToUpper(text[i]);
                charOfPassword = char.ToUpper(password[j]);

                if (charOfText == ' ')// if encounters a white space in the text
                {
                    //add the white case character to the encrypted text
                    encryptedChar = charOfText;
                    encryptedText = encryptedText + encryptedChar;
                    i++;
                    j--;
                }
                else if (charOfText == '\n')// if encounters a new line in the text
                {
                    //add the new line character to the encrypted text
                    encryptedChar = charOfText;
                    encryptedText = encryptedText + encryptedChar;
                    i++;
                    j--;
                }
                else
                {
                     //Get the index of the character of text from alphabets variable
                     charIndexOfText = alphabets.IndexOf(charOfText);

                     //Get the index of the character of password from alphabets variable
                     charIndexOfPassword = alphabets.IndexOf(charOfPassword);

                     //Get the encrypted character
                     newIndex = (charIndexOfText + charIndexOfPassword) % 26;
                     encryptedChar = alphabets[newIndex];

                     //add the encrypted character to a string every time to get an encrypted string
                     encryptedText = encryptedText + encryptedChar;
                     i++;
                }
            }
        }
        i--;
     }

     //Display Encrypted text
     txtPlainText.Clear();
     lblCipherText.Text = encryptedText;
                            
                                

                            
        Dim AllText As String
        Dim i, charsInFile As Short
        Dim letter As Char
        Dim Decrypt As String = ""
        OpenFileDialog1.Filter = "Text file |*.txt"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If My.Computer.FileSystem.FileExists(OpenFileDialog1.FileName) Then
                Try
                    AllText = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
                    ' now decrypt string by subtracting 1
                    charsInFile = AllText.Length ' get length of string
                    For i = 0 To charsInFile - 1 ' loop once for each char
                        letter = AllText.Substring(i, 1) ' get char
                        Decrypt = Decrypt & Chr(Asc(letter) - 1)
                    Next i ' and build new string
                    TextBox1.Text = Decrypt ' then display converted string
                    TextBox1.Select(0, 0) ' remove text selection
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Information)
                End Try
            End If
        End If