2015-11-06 31 views
0

我正在製作一個密碼解密/加密控制檯應用程序。爲用戶提供從文件系統中選擇'.txt'的選項

我想要做的是給用戶選擇從文件系統中選擇.txt文件或鍵入到控制檯窗口。我知道如何讀/寫/保存'.txt'等,但我有點不確定實現它的最佳方式。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace Application 
{ 


class Caesar 
{ 
    static void Main(string[] args) 
    { 

     try{ 
     Console.WriteLine("Enter Key:"); 
     int k = Convert.ToInt32(Console.ReadLine()); 
     Console.WriteLine("What would you like to do?"); 
     Console.WriteLine(""); 
     Console.WriteLine("[E] Encrypt"); 
     Console.WriteLine("[D] Decrypt"); 
     string choice =Convert.ToString(Console.ReadLine()).ToUpper(); 

      // WOULD LIKE TO GIVE THE USER THE CHOICE BETWEEN TYPING OR ADDING A .TXT FILE FROM THE FILSYSTEM 
     switch (choice) 
     { 
      case "E": Console.Write("Enter Plain Text:"); 
         string pt = Console.ReadLine(); 
         caesar_cipher(k, pt); 
         break; 

      case "D": Console.Write("Type CipherText :"); 
         string ct = Console.ReadLine(); 
         caesar_decipher(k, ct); 
         break; 

      default: Console.WriteLine("You've entered an incorrect option!"); 
         break; 
     } 
     } 
     catch(Exception) 
     { 
      Console.WriteLine ("The value you entered is incorrect"); 
      Console.WriteLine ("Press any key to try again"); 
       Console.ReadKey(); 
     } 


    } 
    static void caesar_cipher(int key, string pt) 
    { 
     int size = pt.Length; 
     char[] value = new char[size]; 
     char[] cipher = new char[size]; 
     for (int r = 0; r < size; r++) 
     { 
      value[r] = Convert.ToChar(pt.Substring(r, 1)); 
     } 

     for (int re = 0; re < size; re++) 
     { 
      int count = 0; 
      int a = Convert.ToInt32(value[re]); 
      for (int y = 1; y <= key; y++) 
      { 
       if (count == 0) 
       { 
        if (a == 90) 
        { a = 64; } 
        else if (a == 122) 
        { a = 96; } 
        cipher[re] = Convert.ToChar(a + y); 
        count++; 
       } 
       else 
       { 
        int b = Convert.ToInt32(cipher[re]); 
        if (b == 90) 
        { b = 64; } 
        else if (b == 122) 
        { b = 96; } 
        cipher[re] = Convert.ToChar(b + 1); 

       } 
      } 
     } 
     string ciphertext = ""; 

     for (int p = 0; p < size; p++) 
     { 
      ciphertext = ciphertext + cipher[p].ToString(); 
     } 
     Console.WriteLine("Cipher Text="); 
     Console.WriteLine(ciphertext.ToUpper()); 
    } 


    static void caesar_decipher(int key, string ct) 
    { 
     int size = ct.Length; 
     char[] value = new char[size]; 
     char[] cipher = new char[size]; 
     for (int r = 0; r < size; r++) 
     { 
      cipher[r] = Convert.ToChar(ct.Substring(r, 1)); 
     } 

     for (int re = 0; re < size; re++) 
     { 
      int count = 0; 
      int a = Convert.ToInt32(cipher[re]); 
      for (int y = 1; y <= key; y++) 
      { 
       if (count == 0) 
       { 
        if (a == 65) 
        { a = 91; } 
        else if (a == 97) 
        { a = 123; } 
        value[re] = Convert.ToChar(a - y); 
        count++; 
       } 
       else 
       { 
        int b = Convert.ToInt32(value[re]); 
        if (b == 65) 
        { b = 91; } 
        else if (b == 97) 
        { b = 123; } 
        value[re] = Convert.ToChar(b - 1); 

       } 
      } 
     } 
     string plaintext = ""; 

     for (int p = 0; p < size; p++) 
     { 
      plaintext = plaintext + value[p].ToString(); 
     } 
     Console.WriteLine("Plain Text="); 
     Console.WriteLine(plaintext.ToLower()); 
     Console.ReadKey(); 
    } 
    } 
} 

回答

0

是否希望他們能夠從文件系統中選擇文件或輸入加密/純文本字符串本身之間進行選擇?

還是你希望他們能夠從文件系統中選擇一個文件或輸入文件路徑?

假設你的意思是後者,這裏是我對你的問題的解決方案。

[STAThread] 
    static void Main(string[] args) { 
     try { 
      Console.Write("Enter key : "); 
      int key = Convert.ToInt32(Console.ReadLine()); 
      Console.Write("Would you like to [E]ncrypt or [D]ecrypt? : "); 
      string choice = Console.ReadLine().ToUpper(); 

      // Making the decision of writing the filepath or selecting the filepath. 
      Console.Write("Would you like to [W]rite or [S]elect the file path? : "); 
      string fileChoice = Console.ReadLine().ToUpper(); 

      string filePath = ""; 
      if(fileChoice == "W") { 
       Console.WriteLine("Type the filepath the .txt file with your Cipher/Plain text"); 
       filePath = Console.ReadLine(); 
      } 
      else { 
       OpenFileDialog dial = new OpenFileDialog(); 
       dial.Filter = "Text files (*.txt)|*.txt"; 
       if (dial.ShowDialog() == DialogResult.OK) 
        filePath = dial.FileName; 
      } 

      string stringData; 
      using (var reader = new StreamReader(filePath)) 
       stringData = reader.ReadToEnd(); 


      switch (choice) { 
       case "E": 
        caesar_cipher(key, stringData); 
        break; 

       case "D": 
        caesar_decipher(key, stringData); 
        break; 

       default: 
        Console.WriteLine("You've entered an incorrect option!"); 
        break; 
      } 

      Console.ReadLine(); // Added this to make sure you can read the end result. 
     } 
     catch (Exception) { 
      Console.WriteLine("The value you entered is incorrect"); 
      Console.WriteLine("Press any key to try again"); 
      Console.ReadKey(); 
      Main(null); // This will call the main method allowing you to "try again". 
     } 
    } 
0

這樣做的一種可能的方法是通過允許用戶通過拖放文件到該.exe來啓動程序 - 該程序然後可以加密或解密的文件的內容。如果程序是通過雙擊.exe啓動的,那麼它可以假定它將加密或解密來自控制檯的用戶輸入。

你可以做這樣的事情:

public static void Main(string[] args) 
    { 
     // If args is not empty, then the user has dragged and dropped a file. 
     bool inputFromFile = args.Length > 1; 
     string input = string.Empty; 
     if (inputFromFile) 
     { 
      // This contains the file path of the dragged and dropped file. 
      string fileName = args[0]; 
      input = File.ReadAllText(fileName); 
     } 
     else 
      input = Console.ReadLine(); 
    } 

要進行測試,如果你使用的是Visual Studio,您可以使用設置項目的命令行參數:

Modification of command line arguments in Visual Studio project properties

相關問題