3

I'm trying to figure out how to join two strings that are encoded Base64 and then decode and get the combined result.

Example:

string1 Hello --- string1 Base64 SGVsbG8=

string2 World --- string2 Base64 V29ybGQ=

If I join the base64 I get something that wont decode SGVsbG8=V29ybGQ=

I want the result to say: Hello World

I don't want only this example to work but rather something that will work with any string. This is a very simplified problem which is a step on an application I'm trying to write I'm stuck on.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
DonO
  • 950
  • 1
  • 12
  • 27
  • thought old post, but may help, try this with javascript, https://jsfiddle.net/1hcrvc16/3/ – G J Sep 19 '17 at 01:30

2 Answers2

0

What if you encode both strings to array, then combine those arrays and finally GetString from the bytes?

using System;
using System.Text;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var base1 = "SGVsbG8=";
        var base2 = "V29ybGQ=";
        var array1 = Convert.FromBase64String(base1);
        var array2 = Convert.FromBase64String(base2);   
        var comb = Combine(array1, array2);
        var data = Encoding.Default.GetString(comb);
        Console.WriteLine(data);
    }
    private static byte[] Combine(byte[] first, byte[] second)
    {
        return first.Concat(second).ToArray();
    }
 }
Levon Ravel
  • 80
  • 1
  • 8
-1

I found a best way to do this, add plus between one string and other, and add ONE, and only ONE equals char ('=') at the end of string. The return will be "Hello>World", then remove the ">":

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());
    }
 }

(Old way) In C# I do something like this:

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();
    }
}
Ricardo Silva
  • 964
  • 1
  • 9
  • 18
  • Thank you! It worked for a bunch of test I ran. There is a spacing issue if you use a string that doesn't end with '=' such as the word big ("Ymln"). But I am not even worried abut the spacing as it should be added on the string before it being encoded. Thank You very much. – DonO Feb 25 '15 at 20:02
  • Yes, I put the space only to attend the "Hello World" case, but the correct way is let the decision to put, or not, the space to one that will receive return of DecodeBase64String method. – Ricardo Silva Feb 27 '15 at 15:24