简体   繁体   English

Base64编码和解码不起作用

[英]Base64 Encoding and decoding does not work

I watched a lot of tutorials everywhere and they all say it should work but it doesnt. 我到处都看过很多教程,他们都说应该可以,但是没有。

I want to enter " encode RandomText " and then it should encode the " RandomText " to Base64 and if I type " decode [theEncodedText] " it should decode from the Base64 and give me " RandomText ". 我想输入“ encoding RandomText ”,然后它应该将“ RandomText ”编码为Base64,如果我键入“ decode [theEncodedText] ”,它应该从Base64解码并给我“ RandomText ”。 I did everything I could do but it still doesn't work. 我做了我能做的一切,但仍然没用。

Check my full code: 检查我的完整代码:

    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 is case sensitive. Base64区分大小写。 You're lowering the case on your input data. 您正在降低输入数据的大小写。 Therefore it won't work. 因此,它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM