连接两个 Base64 字符串,然后解码它们

Don*_*onO 5 embed url base64 uri

我试图弄清楚如何连接两个 Base64 编码的字符串,然后解码并获得组合结果。

例子:

string1 你好 --- string1 Base64 SGVsbG8=

string2 世界 --- string2 Base64 V29ybGQ=

如果我加入 base64,我会得到一些无法解码 SGVsbG8=V29ybGQ= 的内容

我想要的结果是:Hello World

我不希望只有这个示例能够工作,而是希望能够与任何字符串一起工作。这是一个非常简单的问题,这是我试图编写的应用程序中的一个步骤,但我却陷入了困境。

Ric*_*lva -1

我找到了一种最好的方法来做到这一点,在一个字符串和另一个字符串之间添加加号,然后添加一个,并且只有一个等于字符串末尾的字符('=')。返回将是“Hello>World”,然后删除“>”:

class Program
{
    static void Main(string[] args)
    {
        string base64String = "SGVsbG8+V29ybGQ=";
        byte[] encodedByte = Convert.FromBase64String(base64String);
        var finalString = Encoding.Default.GetString(encodedByte)).Replace(">", " ");
        Console.WriteLine(finalString.ToString());
    }
 }
Run Code Online (Sandbox Code Playgroud)

(老方法)在 C# 中我做了这样的事情:

class Program
{
    static void Main(string[] args)
    {
        string base64String = "SGVsbG8=V29ybGQ=";
        Console.WriteLine(DecodeBase64String(base64String));
        Console.ReadLine();
    }

    public static string DecodeBase64String(string base64String)
    {
        StringBuilder finalString = new StringBuilder();

        foreach (var text in base64String.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries))
        {
            byte[] encodedByte = Convert.FromBase64String(text + "=");

            finalString.Append(Encoding.Default.GetString(encodedByte));
            finalString.Append(" "); //This line exists only to attend the "Hello World" case. The correct is remove this and let the one that will receive the return to decide what will do with returned string.
        }

        return finalString.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)