我到处看了很多教程,他们都说应该可以,但没有。
我想输入“ encode RandomText ”,然后它应该将“ RandomText ”编码为 Base64,如果我输入“ decode [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;
}
}