Encryption and Decryption


 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int key = 0;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {//Encryption
           
            String word = txt_encrypt.Text.ToUpper();
            String encrypted = "";
             key = Int32.Parse(txt_key.Text);
           
            for (int l = 0; l <= word.Length-1;l++ )
            {
                int index = findLetter(word[l]);
                int encryptionValue = (key * index + 3) % 26;
                encrypted = encrypted + alphabet[encryptionValue];

            }

            txt_dcrypt.Text = encrypted;
        }


        public int findLetter(char s) {
            for (int i = 0; i<=25;i++ )
            {  
                if(alphabet[i]==s){
                    return i;
                }

            }
            return 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {//Decryption
            String word = txt_dcrypt.Text;
            String decrypted = "";
            for (int l = 0; l <= word.Length - 1; l++)
            {
                int index = findLetter(word[l]);
                int inverse = findMinus(key);
                int decryptionValue = (inverse*( index-3)) % 26;

                decrypted = decrypted + alphabet[decryptionValue];

            }
            txt_encrpt1.Text = decrypted;

        }

        public int findMinus(int a) {
            for (int i = 0; i <= 26;i++ )
            {
                if((a*i)%26 == 1){
                    return i;
                }
            }
            return 0;
        }

        private void txt_encrpt1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}