<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bill Nice G. HAVUGUKURI on Medium]]></title>
        <description><![CDATA[Stories by Bill Nice G. HAVUGUKURI on Medium]]></description>
        <link>https://medium.com/@billnicejig?source=rss-1a3dc0ee180e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jAsXOsM9A4Kc0guLntDZQg@2x.jpeg</url>
            <title>Stories by Bill Nice G. HAVUGUKURI on Medium</title>
            <link>https://medium.com/@billnicejig?source=rss-1a3dc0ee180e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 13:39:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@billnicejig/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Base64 Works — and Why Output Length mod 4 = 1 Is Invalid: A Simple 6-Bit Proof]]></title>
            <link>https://medium.com/@billnicejig/how-base64-works-and-why-output-length-mod-4-1-is-invalid-a-simple-6-bit-proof-e37af8d9f616?source=rss-1a3dc0ee180e------2</link>
            <guid isPermaLink="false">https://medium.com/p/e37af8d9f616</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[base64]]></category>
            <dc:creator><![CDATA[Bill Nice G. HAVUGUKURI]]></dc:creator>
            <pubDate>Sat, 26 Jul 2025 16:49:32 GMT</pubDate>
            <atom:updated>2025-07-26T16:49:32.855Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why Base64 strings with length mod4 = 1 are invalid – A Simple 6-Bit Proof</h3><p>A quick, intuitive 6-bit view that explains how Base64 encoding works and why it’s valid outputs length can never be 4k+1 (i.e <strong>b64Stringlength%4 !=1</strong>).</p><h3>Summary</h3><p>Base64 emits 6-bit symbols. Even 1 byte of data (8 bits) needs two Base64 characters (6 bits + 2 bits padded with zeros), and standard padding makes the final length a multiple of 4. A length ≡ 1 (mod 4) would require an output with just one Base64 character – impossible.</p><p>Therefore If a Base64 string’s length is 4k+1, it isn’t a valid Base64 encoding output.</p><p>…hooked? Alright. let’s us drive deeper.</p><h3>Think in 6 bits, not in 3 bytes</h3><p>You’ve heard the textbook rule: <strong>3 bytes → 4 Base64 chars</strong>. True.</p><p>But there’s an even simpler way to see the length rule:</p><blockquote><strong>Base64 is a 6-bit conveyor belt.</strong></blockquote><blockquote>Read 6 bits → print a character. Repeat. If the input ends mid-chunk, pad with zeros to complete 6 bits, then add <strong>=</strong> to signal padding and round the total length to a multiple of 4.</blockquote><p>From this angle, one fact jumps out:</p><p>•	<strong>One Base64 character carries 6 bits.</strong></p><p>•	<strong>One byte has 8 bits.</strong></p><p>•	<strong>So any non-empty input needs at least two Base64 characters.</strong></p><p>That alone kills length % 4 == 1.</p><h3>One-byte walkthrough (why 1 char is impossible)</h3><pre>Input: 1 byte = 8 bits<br><br>Take first 6 bits  ──►  1st Base64 char<br>Leftover 2 bits    ──►  pad with 4 zeros = 6 bits  ──►  2nd Base64 char<br>                                        └────────────►  signal with &quot;==&quot;<br>Result: 2 data chars + &quot;==&quot;  →  total length = 4</pre><p>You can never produce <strong>just 1 Base64 character from any non-empty input.</strong></p><p>Therefore, lengths<strong> 1, 5, 9, 13, …</strong> are <strong>impossible for valid Base64</strong>.</p><h3>Mathematical proof</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*zotGmQaixObk9v4Tckg5KQ@2x.jpeg" /></figure><h3>The intuition that sticks</h3><p>•	Base64 = 6-bit symbols.</p><p>•	Minimum non-empty output: 2 symbols (even without padding).</p><p>•	Final length is 4-aligned by padding (=, ==).</p><p>•	Therefore, length % 4 == 1 can never be produced by a correct encoder.</p><h3>What about Base64URL and “unpadded” Base64?</h3><p>Two quick notes:</p><p>1.	<a href="https://datatracker.ietf.org/doc/html/rfc7515#appendix-C"><strong>Base64URL alphabet</strong></a> (used in URLs/JSON tokens) swaps + → – and / → _.</p><p>The bit structure is identical, so the same impossibility holds: you still can’t get length % 4 == 1.</p><p>2.	<strong>Unpadded variants</strong> sometimes strip trailing = for compactness. After trimming padding, lengths can be:</p><p>•	… % 4 == 0 (no padding was needed),</p><p>•	… % 4 == 2 (originally had ==),</p><p>•	… % 4 == 3 (originally had =).</p><p>You will <strong>still never see % 4 == 1</strong> – because that would imply only one Base64 symbol was produced, which can’t encode even a single byte.</p><p>If you’re writing a decoder for an unpadded variant, a robust rule is:</p><p>•	If len % 4 == 2, append ==.</p><p>•	If len % 4 == 3, append =.</p><p>•	If len % 4 == 1, <strong>reject</strong> – it was never valid Base64 to begin with.</p><h3>Quick validator snippet – JavaScript</h3><pre>function isLikelyBase64(s, allowUnpadded = false) {<br>  const len = s.length;<br>  if (!allowUnpadded) {<br>    return len % 4 === 0;<br>  }<br>  const r = len % 4;<br>  return r === 0 || r === 2 || r === 3; // 1 is impossible<br>}</pre><p><em>(This is snippet checks on length only; real validation should also verify the alphabet and, if present, correct placement of =.)</em></p><h3>TL;DR</h3><p>If you remember just one line:</p><blockquote>Base64 is a 6-bit machine. One byte always needs two symbols. That’s why length % 4 == 1 is impossible.</blockquote><p>Use that to quickly spot malformed inputs, tighten validators, and avoid subtle bugs when handling padded vs unpadded Base64.</p><h3>Before you go</h3><p>Enjoyed the tiny proof? <strong>Clap</strong> it up, <strong>share</strong> it with a teammate, and <strong>drop a comment</strong> with your strangest Base64 edge cases or decoder snippets. <strong>Learned something new or spotted a nuance?</strong> Tell me what surprised you – I’ll <strong>update the post and credit you</strong>. <strong>Follow</strong> for more bite-size proofs, encoding lore, security, and backend war stories.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e37af8d9f616" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>