{"id":11991,"date":"2025-11-15T10:48:34","date_gmt":"2025-11-15T10:48:34","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11991"},"modified":"2025-11-15T10:53:08","modified_gmt":"2025-11-15T10:53:08","slug":"base64-encoding","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/base64-encoding\/","title":{"rendered":"Base64 encoding"},"content":{"rendered":"<p>Base64 encoding is used when embedding binary in text based formats such as JSON, XML, YML etc. in such cases if we need to add a binary type, such as images or files etc. and we must pass then via a text format, then we need to Base64 encode this type of data first. <\/p>\n<p><strong>Use cases<\/strong><\/p>\n<p>Within web applications this is often used to pass binary within a JSON request\/response object, but can be also seen when embedding images directly into HTML, for example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;img src=&quot;data:image\/png;base64,your-binary-encoded-data...&quot; \/&gt;\r\n<\/pre>\n<p>It&#8217;s also used for Email attachments (MIME) as well as Authentication tokens &#8211; JWT tokens often use Base64URL (a variant of Base64).<\/p>\n<p>Other use cases include clipboard copy\/pasting of blobs (images\/files etc.) into a text based clipboard format as well as being used from transporting over text only channels.<\/p>\n<p><strong>Where and why not to use Base64 encoding?<\/strong><\/p>\n<p>Base64 should <strong>NOT<\/strong> be used for streaming raw binary (application\/octet-stream), large files or in binary safe protocols such as gRPC, websockets and HTTP when using the aforementioned large binary data etc.<\/p>\n<p>First off, these protocols already support raw binary data so the affects of encoding are only on the negative side &#8211; if we encode to Base64 we will see, potentially, significant increases in the data size&#8230;<\/p>\n<p>To Base54 encode using Javascript in the browser we can use<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ encode \r\nconst text = &quot;Hello, world!&quot;;\r\nconst encoded = btoa(text);\r\n\r\n\/\/ decode\r\nconst decoded = atob(encoded); \r\n<\/pre>\n<p>In C# we can use<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ encode\r\nbyte&#x5B;] bytes = Encoding.UTF8.GetBytes(&quot;Hello, world!&quot;);\r\nstring base64 = Convert.ToBase64String(bytes);\r\n\r\n\/\/ decode\r\nbyte&#x5B;] decodedBytes = Convert.FromBase64String(base64);\r\nstring decoded = Encoding.UTF8.GetString(decodedBytes);\r\n<\/pre>\n<p><strong>Calculating the Base64 encoding affects<\/strong><\/p>\n<p>Base64 encoding encodes every 3 bytes of binary data in 4 ASCII characters, so we essentially expand a binary data payload when using Base64 encoding<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvar base64Size = (binarySize\/3) * 4\r\n<\/pre>\n<p>Or we can approximate with<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvar base64Size = 1.33 * binarySize;\r\n<\/pre>\n<p>Plus up to 2 padding characters &#8220;=&#8221; if the binary size is not divisible by 3.<\/p>\n<p>This means that for every 1MB (1048576 bytes) the Base64 size is about 1.4MB (1398104 chars) giving us a 33% overhead.<\/p>\n<p>This ofcourse is significant in streaming of data as it adds to the bandwidth and memory overhead along with increases in CPU usage for the encoding\/decoding. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Base64 encoding is used when embedding binary in text based formats such as JSON, XML, YML etc. in such cases if we need to add a binary type, such as images or files etc. and we must pass then via a text format, then we need to Base64 encode this type of data first. Use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[42,701,356],"tags":[],"class_list":["post-11991","post","type-post","status-publish","format-standard","hentry","category-html","category-http","category-web-api"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11991"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11991\/revisions"}],"predecessor-version":[{"id":11999,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11991\/revisions\/11999"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}