I’m trying to figure out a way to parse out a base64 string from with a larger string.
I have the string "Hello <base64 content> World" and I want to be able to parse out the base64 content and convert it back to a string. "Hello Awesome World"
Answers in C# preferred.
Edit: Updated with a more real example.
--abcdef
\n
Content-Type: Text/Plain;
Content-Transfer-Encoding: base64
\n
<base64 content>
\n
--abcdef--
This is taken from 1 sample. The problem is that the Content…. vary quite a bit from one record to the next.
In short form you could:
In code:
Without a delimiter though, you can end up converting non-base64 text that happens to be also be valid as base64 encoded text.
Looking at your example of trying to convert
"Hello QXdlc29tZQ== World"to"Hello Awesome World"the above algorithm could easily generate something like"ée¡Ý•ͽµ”¢¹]"by trying to convert the whole string from base64 since there is no delimiter between plain and encoded text.Update (based on comments):
If there are no
'\n's in the base64 content and it is always preceded by"Content-Transfer-Encoding: base64\n", then there is a way:'\n'"Content-Transfer-Encoding: base64"In code: