none
Vigenere cipher help. RRS feed

  • Question

  • Guys, i got this program and its working fine, but i would like that the program would ask me for a "key" to encrypt or decrypt a message instead of using a predetermined one... Ill post the program below, thanks.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp8
    {
                class VigenereCipher
            {
                public string encrypt(string text, string key, int d)
                {
                    int l1 = 0, temp;
                    string ns = "";
                    text = text.ToUpper();
                    key = key.ToUpper();
                    foreach (char t in text)
                    {
                        if (t < 65) continue;
                        temp = t - 65 + d * (key[l1] - 65);
                        if (temp < 0) temp += 26;
                        ns += Convert.ToChar(65 + (temp % 26));
                        if (++l1 == key.Length) l1 = 0;
                    }

                    return ns;
                }
            }

            class Program
            {
                private static VigenereCipher v = new VigenereCipher();

                
                private static string key = "unip";


                static void Main(string[] args)
                {
                    int option;
                    do
                    {
                        Console.WriteLine("");
                        Console.WriteLine("[1] Encrypt");
                        Console.WriteLine("[2] Decrypt");
                        Console.WriteLine("[0] Leave");
                        Console.WriteLine("");
                        Console.WriteLine("-------------------------------------");
                        Console.WriteLine(" Chose an option: ");
                        option = Int32.Parse(Console.ReadLine());

                        switch (option)
                        {
                            case 1:
                                Encrypttext();
                                break;
                            case 2:
                                Decrypttext();
                                break;
                            default:
                                Leave();
                                break;
                        }
                        Console.ReadKey();
                        Console.Clear();
                    }
                    while (option != 0);
                }

                private static void Leave()
                {
                    Console.WriteLine();
                    Console.WriteLine(" Type anything to leave");

                }

                private static string Encrypttext()
                {
                    string text, s1;
                    Console.WriteLine(" Type message to be encrypted: ");
                    text = Console.ReadLine();
                    s1 = v.encrypt(text, key, 1);
                    Console.WriteLine(" Encrypted message: " + s1);
                    return s1;
                }
                private static string Decrypttext()
                {
                    string s1, text;
                    Console.WriteLine(" Type message to be decrypted:");
                    text = Console.ReadLine();
                    s1 = v.encrypt(text, key, -1);
                    Console.WriteLine(" Decrypted message: " + s1);
                    return s1;
                }
            }
        }

    Thursday, November 22, 2018 10:26 AM

Answers

  • hmm, okay.The Main() method is just the entry point for a program. As C# is an object-oriented language, we should implement logic into classes. But the Program class is not the correct place for that. This is especially a problem as you need a class level variable, which requires a object instance.

    E.g.

    namespace ConsoleCS
    {
        using System;
    
        public class VigenereCipher
        {
            private readonly string key;
    
            public VigenereCipher(string key) { this.key = key.ToUpper(); }
    
            public string Decrypt(string text) { return this.Process(text, -1); }
    
            public string Encrypt(string text) { return this.Process(text, 1); }
    
            private string Process(string text, int d)
            {
                int l1 = 0, temp;
                string ns = "";
                text = text.ToUpper();
                foreach (char t in text)
                {
                    if (t < 65) continue;
                    temp = t - 65 + d * (this.key[l1] - 65);
                    if (temp < 0) temp += 26;
                    ns += Convert.ToChar(65 + (temp % 26));
                    if (++l1 == this.key.Length) l1 = 0;
                }
    
                return ns;
            }
        }
    
        class VigenereCipherApplication
        {
            private VigenereCipher vigenereCipher = null;
    
            public void Run()
            {
                MenuShow();
                MenuLoop();
            }
    
            private void MenuLoop()
            {
                int option;
                do
                {
                    Console.WriteLine("\n---");
                    if (int.TryParse(Console.ReadLine(), out option))
                    {
                        switch (option)
                        {
                            case 1:
                                MenuEncrypt();
                                break;
                            case 2:
                                MenuDecrypt();
                                break;
                            case 3:
                                MenuKey();
                                break;
                            default:
                                Console.WriteLine();
                                Console.WriteLine(" Type anything to leave");
                                break;
                        }
                    }
                }
                while (option != 0);
            }
    
            private void MenuKey()
            {
                Console.WriteLine("Enter key:");
                string key = Console.ReadLine();
                this.vigenereCipher = new VigenereCipher(key);
            }
    
            private void MenuDecrypt()
            {
                if (this.vigenereCipher == null)
                {
                    Console.WriteLine("Enter key first.");
                    return;
                }
    
                Console.WriteLine("Type message to be decrypted:");
                string cipherText = Console.ReadLine();
                string plainText = this.vigenereCipher.Decrypt(cipherText);
                Console.WriteLine($"Plain text: {plainText}");
            }
    
            private void MenuEncrypt()
            {
                if (this.vigenereCipher == null)
                {
                    Console.WriteLine("Enter key first.");
                    return;
                }
    
                Console.WriteLine("Type message to be encrypted: ");
                string plainText = Console.ReadLine();
                string cipherText = this.vigenereCipher.Encrypt(plainText);
                Console.WriteLine($"Cipher text: {cipherText}");
            }
    
            private void MenuShow()
            {
                Console.WriteLine("Chose an option: ");
                Console.WriteLine("[0] Leave");
                Console.WriteLine("[1] Encrypt");
                Console.WriteLine("[2] Decrypt");
                Console.WriteLine("[3] Key");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                VigenereCipherApplication vigenereCipherApplication = new VigenereCipherApplication();
                vigenereCipherApplication.Run();
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
        }
    }


    • Edited by Stefan Hoffmann Thursday, November 22, 2018 4:01 PM
    • Marked as answer by RRA29 Thursday, November 22, 2018 7:44 PM
    Thursday, November 22, 2018 3:18 PM

All replies

  • Modify your cipher class and ask for one. Just replicate your Console interaction as you already do e.g.

    namespace ConsoleCS
    {
        using System;
    
        public class VigenereCipher
        {
            private readonly string key;
            public VigenereCipher(string key) { this.key = key.ToUpper(); }
            public string Decrypt(string text) { return this.Process(text, -1); }
            public string Encrypt(string text) { return this.Process(text, 1); }
            private string Process(string text, int d)
            {
                int l1 = 0, temp;
                string ns = "";
                text = text.ToUpper();
                foreach (char t in text)
                {
                    if (t < 65) continue;
                    temp = t - 65 + d * (this.key[l1] - 65);
                    if (temp < 0) temp += 26;
                    ns += Convert.ToChar(65 + (temp % 26));
                    if (++l1 == this.key.Length) l1 = 0;
                }
    
                return ns;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter key:");
                string key = Console.ReadLine();
    
                string sampleText = "Sample text.";
                VigenereCipher vigenereCipher = new VigenereCipher(key);
                string cipherText = vigenereCipher.Encrypt(sampleText);
                string plainText = vigenereCipher.Decrypt(cipherText);
                Console.WriteLine($"Cipher text:\t{cipherText}");
                Console.WriteLine($"Plain text:\t{plainText}");
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
        }
    }


    Thursday, November 22, 2018 11:41 AM
  • Thank you for your answer Stefan but i got confused with your explanation. I just started to learn programming... Could you show me where i should make the changes on my program? I tried to implement what you showed me on my program but i keep getting error messages. 
    Thursday, November 22, 2018 1:42 PM
  • hmm, okay.The Main() method is just the entry point for a program. As C# is an object-oriented language, we should implement logic into classes. But the Program class is not the correct place for that. This is especially a problem as you need a class level variable, which requires a object instance.

    E.g.

    namespace ConsoleCS
    {
        using System;
    
        public class VigenereCipher
        {
            private readonly string key;
    
            public VigenereCipher(string key) { this.key = key.ToUpper(); }
    
            public string Decrypt(string text) { return this.Process(text, -1); }
    
            public string Encrypt(string text) { return this.Process(text, 1); }
    
            private string Process(string text, int d)
            {
                int l1 = 0, temp;
                string ns = "";
                text = text.ToUpper();
                foreach (char t in text)
                {
                    if (t < 65) continue;
                    temp = t - 65 + d * (this.key[l1] - 65);
                    if (temp < 0) temp += 26;
                    ns += Convert.ToChar(65 + (temp % 26));
                    if (++l1 == this.key.Length) l1 = 0;
                }
    
                return ns;
            }
        }
    
        class VigenereCipherApplication
        {
            private VigenereCipher vigenereCipher = null;
    
            public void Run()
            {
                MenuShow();
                MenuLoop();
            }
    
            private void MenuLoop()
            {
                int option;
                do
                {
                    Console.WriteLine("\n---");
                    if (int.TryParse(Console.ReadLine(), out option))
                    {
                        switch (option)
                        {
                            case 1:
                                MenuEncrypt();
                                break;
                            case 2:
                                MenuDecrypt();
                                break;
                            case 3:
                                MenuKey();
                                break;
                            default:
                                Console.WriteLine();
                                Console.WriteLine(" Type anything to leave");
                                break;
                        }
                    }
                }
                while (option != 0);
            }
    
            private void MenuKey()
            {
                Console.WriteLine("Enter key:");
                string key = Console.ReadLine();
                this.vigenereCipher = new VigenereCipher(key);
            }
    
            private void MenuDecrypt()
            {
                if (this.vigenereCipher == null)
                {
                    Console.WriteLine("Enter key first.");
                    return;
                }
    
                Console.WriteLine("Type message to be decrypted:");
                string cipherText = Console.ReadLine();
                string plainText = this.vigenereCipher.Decrypt(cipherText);
                Console.WriteLine($"Plain text: {plainText}");
            }
    
            private void MenuEncrypt()
            {
                if (this.vigenereCipher == null)
                {
                    Console.WriteLine("Enter key first.");
                    return;
                }
    
                Console.WriteLine("Type message to be encrypted: ");
                string plainText = Console.ReadLine();
                string cipherText = this.vigenereCipher.Encrypt(plainText);
                Console.WriteLine($"Cipher text: {cipherText}");
            }
    
            private void MenuShow()
            {
                Console.WriteLine("Chose an option: ");
                Console.WriteLine("[0] Leave");
                Console.WriteLine("[1] Encrypt");
                Console.WriteLine("[2] Decrypt");
                Console.WriteLine("[3] Key");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                VigenereCipherApplication vigenereCipherApplication = new VigenereCipherApplication();
                vigenereCipherApplication.Run();
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
        }
    }


    • Edited by Stefan Hoffmann Thursday, November 22, 2018 4:01 PM
    • Marked as answer by RRA29 Thursday, November 22, 2018 7:44 PM
    Thursday, November 22, 2018 3:18 PM