Я смотрел много учебников повсюду, и все говорят, что они должны работать, но это не так.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;
}
}