Exemple #1
0
        private static void Main(string[] args)
        {
            string plainText  = "Attack at night with all forces";
            string cipherText = "dwwdfn dw qljkw zlwk doo irufhv";

            char[] cipher;
            Console.WriteLine("Cipher text : ");
            foreach (var item in cipherText)
            {
                Console.Write(item);
            }
            Console.WriteLine();
            Console.WriteLine("Plain text : ");
            //cipher = CaesarCipher.Encrypt(plainText, 3);
            foreach (var item in CaesarCipher.Decrypt(cipherText, 3))
            {
                Console.Write(item);
            }
            Console.WriteLine();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            string continueResp = "yes";
            string filepath;

            while (continueResp.Equals("yes", StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Select an operation:\n1)Encypt\n2)Decrypt\n3)Quit");
                int op = Convert.ToInt32(Console.ReadLine());

                switch (op)
                {
                case (int)Operations.Encrypt:

                    Console.WriteLine("Enter the filepath of the file: ");
                    filepath = Console.ReadLine();
                    try
                    {
                        if (File.Exists(filepath))
                        {
                            Console.WriteLine("Encrypting file...");
                            var msgToEncrypt  = File.ReadAllText(filepath);
                            var encryptedMsg  = CaesarCipher.Encrypt(msgToEncrypt);
                            var encryptedFile = filepath.Replace(".txt", "Encrypted.txt");
                            File.WriteAllText(encryptedFile, encryptedMsg);
                            Console.WriteLine(encryptedMsg);
                        }
                        else
                        {
                            Console.WriteLine("File does not exist. Try again");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                    break;

                case (int)Operations.Decrypt:

                    Console.WriteLine("Enter the filepath of the file: ");
                    filepath = Console.ReadLine();
                    try
                    {
                        if (File.Exists(filepath))
                        {
                            Console.WriteLine("Decrypting file...");
                            var msgToDecrypt  = File.ReadAllText(filepath);
                            var decryptedMsg  = CaesarCipher.Decrypt(msgToDecrypt);
                            var decryptedFile = filepath.Replace(".txt", "Decrypted.txt");
                            File.WriteAllText(decryptedFile, decryptedMsg);
                            Console.WriteLine(decryptedMsg);
                        }
                        else
                        {
                            Console.WriteLine("File does not exist. Try again");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                    break;

                case (int)Operations.Quit:
                    Environment.Exit(1);
                    break;

                default:
                    Console.WriteLine("Invalid operation. Try again.\n");
                    continue;
                }

                Console.WriteLine("Continue operations (yes/no)?");
                continueResp = Console.ReadLine();
            }
        }