Base64编码和解码不起作用

时间:2013-03-29 21:21:05

标签: c# encoding base64 decoding

我到处看了很多教程,他们都说它应该可以工作,但它没有。

我想输入“编码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;
        }
    }

1 个答案:

答案 0 :(得分:0)

Base64区分大小写。您正在降低输入数据的大小写。因此它不起作用。