-3

I need an advice on decoding base64. I will be doing it in c#.

The thing is, I don't know what type of format the decoding will output it may be text, XML, images or PDF. I only have the base64 encoded string.

How do you guys advice me to proceed? Any suggestions?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
lifeTech
  • 17
  • 6
  • 2
    Possible duplicate of [Determine the file type using C#](https://stackoverflow.com/questions/11547654/determine-the-file-type-using-c-sharp) – ProgrammingLlama Mar 08 '19 at 08:22

2 Answers2

3

Many image types and pdfs include a magic number, where the first X bytes identify the file type. You should decode the string and examine the binary for these (https://asecuritysite.com/forensics/magic gives a list of them). If you still can't identify it check whether it parses as XML using an XML parser else assume it's text.

Ryan Sparks
  • 1,347
  • 14
  • 16
1

Extract the MIME type from a base64 string:

/**
 * Extract the MIME type from a base64 string
 * @param encoded Base64 string
 * @return MIME type string
 */
private static String extractMimeType(final String encoded) {
    final Pattern mime = Pattern.compile("^data:([a-zA-Z0-9]+/[a-zA-Z0-9]+).*,.*");
    final Matcher matcher = mime.matcher(encoded);
    if (!matcher.find())
        return "";
    return matcher.group(1).toLowerCase();
}

Usage:

final String encoded = "data:image/png;base64,iVBORw0KGgoAA...5CYII=";
extractMimeType(encoded); // "image/png"
extractMimeType("garbage"); // ""

Then you can write your byte array:

var filePath = System.IO.Path.Combine(folderPath, string.Format("pdf_{0}.pdf", Guid.NewGuid()));
var byteArray = Convert.FromBase64String(base64pdf);

File.WriteAllBytes(filePath, byteArray);

And open you file:

Device.OpenUri(new Uri("file://" + filePath));

Or tokenize the data since the 64 encoded data look like this "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAKAC" and parse that string.

ferminx360
  • 95
  • 8