示例#1
0
        public override void DisplayMenu()
        {
            StreamReader sr = null;
            string       file, line;
            int          counter = 0;
            int          key     = 0;

            Console.WriteLine("Welcome to decryption mode");
            while (String.IsNullOrEmpty(Path))
            {
                Console.WriteLine("Enter path to file:");
                Path = Console.ReadLine();
            }

            while (String.IsNullOrEmpty(FileName))
            {
                Console.WriteLine("Enter the file name:");
                FileName = Console.ReadLine();
            }

            file = @Path + FileName;
            Console.WriteLine(file);

            try
            {
                sr = new StreamReader(file);
                while ((line = sr.ReadLine()) != null)
                {
                    key      = int.Parse(line.Split('+')[0].ToString());
                    Decypher = CaesarCipher.Decrypt(line.Split('+')[1].ToString(), key);
                    Console.WriteLine($"The Cipher is: {Decypher}");

                    counter++;
                }
            }
            catch (Exception)
            {
                Path     = string.Empty;
                FileName = string.Empty;
                ExceptionHandler();
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    WriteToFile(Decypher, key);
                    Console.ReadLine();
                }
            }
        }
示例#2
0
        private void encryptBtn_Click(object sender, EventArgs e)
        {
            var shift = 0;

            if (!string.IsNullOrWhiteSpace(textToEncrypt.Text) && int.TryParse(charShift.Text, out shift))
            {
                if (decryptRB.Checked)
                {
                    shift = shift * -1;
                }

                cipher.Text = CaesarCipher.Encrypt(textToEncrypt.Text, shift);
            }
            else
            {
                textToEncrypt.Text = "INVALID FORM DATA";
            }
        }
示例#3
0
        public override void DisplayMenu()
        {
            int    keyMenu = 0;
            bool   success = false;
            string cipherString;

            Console.WriteLine("Welcome to encryption mode");
            while (String.IsNullOrEmpty(Message))
            {
                Console.WriteLine("Enter message to encrypt:");
                Message = Console.ReadLine();
                if (String.IsNullOrEmpty(Message))
                {
                    ErrorMessage();
                }
            }

            while (!success)
            {
                Console.WriteLine("Enter the key:");
                success = int.TryParse(Console.ReadLine(), out keyMenu);
                if (!success)
                {
                    ErrorMessage();
                }
            }

            Key    = keyMenu;
            Cipher = CaesarCipher.Encrypt(Message, Key);
            Console.WriteLine("The Cipher is: ");
            Console.WriteLine(Cipher);

            cipherString = new String(Cipher);

            WriteToFile(cipherString, Key);

            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }