2013-03-29 110 views
0

我到处看了很多教程,他们都说它应该可以工作,但它没有。Base64编码和解码不起作用

我想输入“编码RandomText”,然后它应该编码“RandomText”为Base64,如果我输入“解码[theEncodedText]”它应该从Base64编码解码,并给我“RandomText“。我做了我能做的所有事情,但仍然无效。

检查我的全码:

static public void Write(string text, int ms) 
    { 
     foreach (char c in text) 
     { 
      Thread.Sleep(ms); 
      Console.Write(c); 
     } 
    } 
    static public string Encode(string text) 
    { 
     byte[] encodedBytes = ASCIIEncoding.ASCII.GetBytes(text); 
     return Convert.ToBase64String(encodedBytes); 
    } 
    static public string Decode(string text) 
    { 
     byte[] decodedBytes = Convert.FromBase64String(text2); 
     return ASCIIEncoding.ASCII.GetString(decodedBytes); 
    } 
    static void Main(string[] args) 
    { 
     string input = ""; 
     while (input != "exit") 
     { 
      Console.ForegroundColor = ConsoleColor.White; 
      Write("Input:> ", 10); Console.ForegroundColor = ConsoleColor.Gray; 
      input = Console.ReadLine().ToLower().Trim(); 

      if (input.StartsWith("encode")) 
      { 
       try 
       { 
        string toEncode = input.Substring(7); 
        Write(Encode(toEncode) + "\n\n", 10); 
       } 
       catch 
       { 
        Write("Please enter the text to encode!\n\n", 10); 
       } 
      } 
      else if (input.StartsWith("decode")) 
      { 
       try 
       { 
        string toDecode = input.Substring(7); 
        Write(Decode(toDecode) + "\n\n", 10); 
       } 
       catch 
       { 
        Write("The entered text is either missing or is not encoded!\n\n", 10); 
       }     
      } 
      else 
      { 
       if (input != "" && input != "exit") 
       { 
        Write("Invalid command.\n\n", 10); 
       } 
       else 
       { 
       } 
      } 
      Console.ForegroundColor = ConsoleColor.White; 
     } 
    } 

回答

0

Base64是区分大小写的。您正在降低输入数据的情况。因此它不会工作。