[英]Base64 Encoding and decoding does not work
我到處都看過很多教程,他們都說應該可以,但是沒有。
我想輸入“ encoding 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;
}
}
Base64區分大小寫。 您正在降低輸入數據的大小寫。 因此,它將無法正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.