<?rfc toc="yes" ?>
<?rfc symrefs="no" ?>
<?rfc rfcedstyle="yes" ?>

<rfc number="4648" obsoletes="3548" category="std" ipr="full3978" xmlns:x="http://purl.org/net/xml2rfc/ext">
<front>
  <title abbrev="Base-N Encodings">The Base16, Base32, and Base64 Data Encodings</title>
  <author surname="Josefsson" initials="S." fullname="Simon Josefsson">
    <organization>SJD</organization>
    <address>
      <email>simon@josefsson.org</email>
    </address>
  </author>
  <date year="2006" month="October"/>
  <abstract>
<t>  
   This document describes the commonly used base 64, base 32, and base
   16 encoding schemes.  It also discusses the use of line-feeds in
   encoded data, use of padding in encoded data, use of non-alphabet
   characters in encoded data, use of different encoding alphabets, and
   canonical encodings.
</t>
  </abstract>
</front>
<middle>
<section title="Introduction" anchor="introduction">
<t>
   Base encoding of data is used in many situations to store or transfer
   data in environments that, perhaps for legacy reasons, are restricted
   to US-ASCII <xref target="RFC0020"/> data.  Base encoding can also be used in new
   applications that do not have legacy restrictions, simply because it
   makes it possible to manipulate objects with text editors.
</t>
<t>
   In the past, different applications have had different requirements
   and thus sometimes implemented base encodings in slightly different
   ways.  Today, protocol specifications sometimes use base encodings in
   general, and "base64" in particular, without a precise description or
   reference.  Multipurpose Internet Mail Extensions (MIME) <xref target="RFC2045"/> is often
   used as a reference for base64 without considering the consequences
   for line-wrapping or non-alphabet characters.  The purpose of this
   specification is to establish common alphabet and encoding
   considerations.  This will hopefully reduce ambiguity in other
   documents, leading to better interoperability.
</t>
</section>
<section title="Conventions Used in This Document" anchor="conventions.used.in.this.document">
<t>
   The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
   "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
   document are to be interpreted as described in <xref target="RFC2119"/>.
</t>
</section>
<section title="Implementation Discrepancies" anchor="implementation.discrepancies">
<t>
   Here we discuss the discrepancies between base encoding
   implementations in the past and, where appropriate, mandate a
   specific recommended behavior for the future.
</t>
<section title="Line Feeds in Encoded Data" anchor="line.feeds.in.encoded.data">
<t>
   MIME <xref target="RFC2045"/> is often used as a reference for base 64 encoding.  However,
   MIME does not define "base 64" per se, but rather a "base 64 Content-Transfer-Encoding" for use within MIME.  As such, MIME enforces a
   limit on line length of base 64-encoded data to 76 characters.  MIME
   inherits the encoding from Privacy Enhanced Mail (PEM) <xref target="RFC1421"/>, stating
   that it is "virtually identical"; however, PEM uses a line length of
   64 characters.  The MIME and PEM limits are both due to limits within
   SMTP.
</t>
<t>
   Implementations MUST NOT add line feeds to base-encoded data unless
   the specification referring to this document explicitly directs base
   encoders to add line feeds after a specific number of characters.
</t>
</section>
<section title="Padding of Encoded Data" anchor="padding.of.encoded.data">
<t>
   In some circumstances, the use of padding ("=") in base-encoded data
   is not required or used.  In the general case, when assumptions about
   the size of transported data cannot be made, padding is required to
   yield correct decoded data.
</t>
<t>
   Implementations MUST include appropriate pad characters at the end of
   encoded data unless the specification referring to this document
   explicitly states otherwise.
</t>
<t>
   The base64 and base32 alphabets use padding, as described below in
   sections <xref target="base.64.encoding" format="counter"/> and <xref target="base.32.encoding" format="counter"/>, but the base16 alphabet does not need it; see
   section <xref target="base.16.encoding" format="counter"/>.
</t>
</section>
<section title="Interpretation of Non-Alphabet Characters in Encoded Data" anchor="interpretation.of.non-alphabet-characters.in.encoded.data">
<t>
   Base encodings use a specific, reduced alphabet to encode binary
   data.  Non-alphabet characters could exist within base-encoded data,
   caused by data corruption or by design.  Non-alphabet characters may
   be exploited as a "covert channel", where non-protocol data can be
   sent for nefarious purposes.  Non-alphabet characters might also be
   sent in order to exploit implementation errors leading to, e.g.,
   buffer overflow attacks.
</t>
<t>
   Implementations MUST reject the encoded data if it contains
   characters outside the base alphabet when interpreting base-encoded
   data, unless the specification referring to this document explicitly
   states otherwise.  Such specifications may instead state, as MIME
   does, that characters outside the base encoding alphabet should
   simply be ignored when interpreting data ("be liberal in what you
   accept").  Note that this means that any adjacent carriage return/
   line feed (CRLF) characters constitute "non-alphabet characters" and
   are ignored.  Furthermore, such specifications MAY ignore the pad
   character, "=", treating it as non-alphabet data, if it is present
   before the end of the encoded data.  If more than the allowed number
   of pad characters is found at the end of the string (e.g., a base 64
   string terminated with "==="), the excess pad characters MAY also be
   ignored.
</t>
</section>
<section title="Choosing the Alphabet" anchor="choosing.the.alphabet">
<t>
   Different applications have different requirements on the characters
   in the alphabet.  Here are a few requirements that determine which
   alphabet should be used:
</t>
<ul>
  <li>
       Handled by humans.  The characters "0" and "O" are easily
      confused, as are "1", "l", and "I".  In the base32 alphabet below,
      where 0 (zero) and 1 (one) are not present, a decoder may
      interpret 0 as O, and 1 as I or L depending on case.  (However, by
      default it should not; see previous section.)
  </li>
  <li>
      Encoded into structures that mandate other requirements.  For base
      16 and base 32, this determines the use of upper- or lowercase
      alphabets.  For base 64, the non-alphanumeric characters (in
      particular, "/") may be problematic in file names and URLs.
  </li>
  <li>
      Used as identifiers.  Certain characters, notably "+" and "/" in
      the base 64 alphabet, are treated as word-breaks by legacy text
      search/index tools.
  </li>
</ul>
<t>
   There is no universally accepted alphabet that fulfills all the
   requirements.  For an example of a highly specialized variant, see
   IMAP <xref target="RFC3501"/>.  In this document, we document and name some currently used
   alphabets.
</t>
</section>
<section title="Canonical Encoding" anchor="canonical.ending">
<t>
   The padding step in base 64 and base 32 encoding can, if improperly
   implemented, lead to non-significant alterations of the encoded data.
   For example, if the input is only one octet for a base 64 encoding,
   then all six bits of the first symbol are used, but only the first
   two bits of the next symbol are used.  These pad bits MUST be set to
   zero by conforming encoders, which is described in the descriptions
   on padding below.  If this property do not hold, there is no
   canonical representation of base-encoded data, and multiple base-encoded strings can be decoded to the same binary data.  If this
   property (and others discussed in this document) holds, a canonical
   encoding is guaranteed.
</t>
<t>
   In some environments, the alteration is critical and therefore
   decoders MAY chose to reject an encoding if the pad bits have not
   been set to zero.  The specification referring to this may mandate a
   specific behaviour.
</t>
</section>
</section>

<section title="Base 64 Encoding" anchor="base.64.encoding">
<t>
   The following description of base 64 is derived from <xref target="RFC1421"/>, <xref target="RFC2045"/>, <xref target="RFC2440"/>,
   and <xref target="RFC4033"/>.  This encoding may be referred to as "base64".
</t>
<t>
   The Base 64 encoding is designed to represent arbitrary sequences of
   octets in a form that allows the use of both upper- and lowercase
   letters but that need not be human readable.
</t>
<t>
   A 65-character subset of US-ASCII is used, enabling 6 bits to be
   represented per printable character.  (The extra 65th character, "=",
   is used to signify a special processing function.)
</t>
<t>
   The encoding process represents 24-bit groups of input bits as output
   strings of 4 encoded characters.  Proceeding from left to right, a
   24-bit input group is formed by concatenating 3 8-bit input groups.
   These 24 bits are then treated as 4 concatenated 6-bit groups, each
   of which is translated into a single character in the base 64
   alphabet.
</t>
<t>
   Each 6-bit group is used as an index into an array of 64 printable
   characters.  The character referenced by the index is placed in the
   output string.
</t>
<texttable title="The Base 64 Alphabet" style="none" anchor="the.base.64.alphabet" x:caption-side="top">
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <c> 0</c><c>A</c>
  <c>17</c><c>R</c>
  <c>34</c><c>i</c>
  <c>51</c><c>z</c>
  <c> 1</c><c>B</c>
  <c>18</c><c>S</c>
  <c>35</c><c>j</c>
  <c>52</c><c>0</c>
  <c> 2</c><c>C</c>
  <c>19</c><c>T</c>
  <c>36</c><c>k</c>
  <c>53</c><c>1</c>
  <c> 3</c><c>D</c>
  <c>20</c><c>U</c>
  <c>37</c><c>l</c>
  <c>54</c><c>2</c>
  <c> 4</c><c>E</c>
  <c>21</c><c>V</c>
  <c>38</c><c>m</c>
  <c>55</c><c>3</c>
  <c> 5</c><c>F</c>
  <c>22</c><c>W</c>
  <c>39</c><c>n</c>
  <c>56</c><c>4</c>
  <c> 6</c><c>G</c>
  <c>23</c><c>X</c>
  <c>40</c><c>o</c>
  <c>57</c><c>5</c>
  <c> 7</c><c>H</c>
  <c>24</c><c>Y</c>
  <c>41</c><c>p</c>
  <c>58</c><c>6</c>
  <c> 8</c><c>I</c>
  <c>25</c><c>Z</c>
  <c>42</c><c>q</c>
  <c>59</c><c>7</c>
  <c> 9</c><c>J</c>
  <c>26</c><c>a</c>
  <c>43</c><c>r</c>
  <c>60</c><c>8</c>
  <c>10</c><c>K</c>
  <c>27</c><c>b</c>
  <c>44</c><c>s</c>
  <c>61</c><c>9</c>
  <c>11</c><c>L</c>
  <c>28</c><c>c</c>
  <c>45</c><c>t</c>
  <c>62</c><c>+</c>
  <c>12</c><c>M</c>
  <c>29</c><c>d</c>
  <c>46</c><c>u</c>
  <c>63</c><c>/</c>
  <c>13</c><c>N</c>
  <c>30</c><c>e</c>
  <c>47</c><c>v</c>
  <c></c><c></c>
  <c>14</c><c>O</c>
  <c>31</c><c>f</c>
  <c>48</c><c>w</c>
  <c>(pad)</c><c>=</c>
  <c>15</c><c>P</c>
  <c>32</c><c>g</c>
  <c>49</c><c>x</c>
  <c></c><c></c>
  <c>16</c><c>Q</c>
  <c>33</c><c>h</c>
  <c>50</c><c>y</c>
  <c></c><c></c>
</texttable>
<t>
   Special processing is performed if fewer than 24 bits are available
   at the end of the data being encoded.  A full encoding quantum is
   always completed at the end of a quantity.  When fewer than 24 input
   bits are available in an input group, bits with value zero are added
   (on the right) to form an integral number of 6-bit groups.  Padding
   at the end of the data is performed using the '=' character.  Since
   all base 64 input is an integral number of octets, only the following
   cases can arise:
</t>
<t>
  <list style="format (%d)" counter="list-b34">
    <t>The final quantum of encoding input is an integral multiple of 24
       bits; here, the final unit of encoded output will be an integral
       multiple of 4 characters with no "=" padding.</t>
    <t>The final quantum of encoding input is exactly 8 bits; here, the
       final unit of encoded output will be two characters followed by
       two "=" padding characters.</t>
    <t>The final quantum of encoding input is exactly 16 bits; here, the
       final unit of encoded output will be three characters followed by
       one "=" padding character.</t>
  </list>
</t>
</section>
<section title="Base 64 Encoding with URL and Filename Safe Alphabet" anchor="base.64.encoding.with.url.and.filename.safe.alphabet">
<t>
   The Base 64 encoding with an URL and filename safe alphabet has been
   used in <xref target="P2PML"/>.
</t>
<t>
   An alternative alphabet has been suggested that would use "~" as the
   63rd character.  Since the "~" character has special meaning in some
   file system environments, the encoding described in this section is
   recommended instead.  The remaining unreserved URI character is ".",
   but some file system environments do not permit multiple "." in a
   filename, thus making the "." character unattractive as well.
</t>
<t>
   The pad character "=" is typically percent-encoded when used in an
   URI <xref target="RFC3986"/>, but if the data length is known implicitly, this can be
   avoided by skipping the padding; see section <xref target="padding.of.encoded.data" format="counter"/>.
</t>
<t>
   This encoding may be referred to as "base64url".  This encoding
   should not be regarded as the same as the "base64" encoding and
   should not be referred to as only "base64".  Unless clarified
   otherwise, "base64" refers to the base 64 in the previous section.
</t>
<t>
   This encoding is technically identical to the previous one, except
   for the 62:nd and 63:rd alphabet character, as indicated in <xref target="the.url.and.filename.safe.base64.alphabet"/>.
</t>
<texttable title='The "URL and Filename safe" Base 64 Alphabet' style="none" anchor="the.url.and.filename.safe.base64.alphabet" x:caption-side="top">
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <c> 0</c><c>A</c>
  <c>17</c><c>R</c>
  <c>34</c><c>i</c>
  <c>51</c><c>z</c>
  <c> 1</c><c>B</c>
  <c>18</c><c>S</c>
  <c>35</c><c>j</c>
  <c>52</c><c>0</c>
  <c> 2</c><c>C</c>
  <c>19</c><c>T</c>
  <c>36</c><c>k</c>
  <c>53</c><c>1</c>
  <c> 3</c><c>D</c>
  <c>20</c><c>U</c>
  <c>37</c><c>l</c>
  <c>54</c><c>2</c>
  <c> 4</c><c>E</c>
  <c>21</c><c>V</c>
  <c>38</c><c>m</c>
  <c>55</c><c>3</c>
  <c> 5</c><c>F</c>
  <c>22</c><c>W</c>
  <c>39</c><c>n</c>
  <c>56</c><c>4</c>
  <c> 6</c><c>G</c>
  <c>23</c><c>X</c>
  <c>40</c><c>o</c>
  <c>57</c><c>5</c>
  <c> 7</c><c>H</c>
  <c>24</c><c>Y</c>
  <c>41</c><c>p</c>
  <c>58</c><c>6</c>
  <c> 8</c><c>I</c>
  <c>25</c><c>Z</c>
  <c>42</c><c>q</c>
  <c>59</c><c>7</c>
  <c> 9</c><c>J</c>
  <c>26</c><c>a</c>
  <c>43</c><c>r</c>
  <c>60</c><c>8</c>
  <c>10</c><c>K</c>
  <c>27</c><c>b</c>
  <c>44</c><c>s</c>
  <c>61</c><c>9</c>
  <c>11</c><c>L</c>
  <c>28</c><c>c</c>
  <c>45</c><c>t</c>
  <c>62</c><c>62 - (minus)</c>
  <c>12</c><c>M</c>
  <c>29</c><c>d</c>
  <c>46</c><c>u</c>
  <c>63</c><c>63 _ (underline)</c>
  <c>13</c><c>N</c>
  <c>30</c><c>e</c>
  <c>47</c><c>v</c>
  <c></c><c></c>
  <c>14</c><c>O</c>
  <c>31</c><c>f</c>
  <c>48</c><c>w</c>
  <c>(pad)</c><c>=</c>
  <c>15</c><c>P</c>
  <c>32</c><c>g</c>
  <c>49</c><c>x</c>
  <c></c><c></c>
  <c>16</c><c>Q</c>
  <c>33</c><c>h</c>
  <c>50</c><c>y</c>
  <c></c><c></c>
</texttable>
</section>
<section title="Base 32 Encoding" anchor="base.32.encoding">
<t>
   The following description of base 32 is derived from <xref target="SASLGSSAPI"/> (with
   corrections).  This encoding may be referred to as "base32".
</t>
<t>
   The Base 32 encoding is designed to represent arbitrary sequences of
   octets in a form that needs to be case insensitive but that need not
   be human readable.
</t>
<t>
   A 33-character subset of US-ASCII is used, enabling 5 bits to be
   represented per printable character.  (The extra 33rd character, "=",
   is used to signify a special processing function.)
</t>
<t>
   The encoding process represents 40-bit groups of input bits as output
   strings of 8 encoded characters.  Proceeding from left to right, a
   40-bit input group is formed by concatenating 5 8bit input groups.
   These 40 bits are then treated as 8 concatenated 5-bit groups, each
   of which is translated into a single character in the base 32
   alphabet.  When a bit stream is encoded via the base 32 encoding, the
   bit stream must be presumed to be ordered with the most-significant-bit first.
   That is, the first bit in the stream will be the high-order bit in the first 8bit byte,
   the eighth bit will be the low-order bit in the first 8bit byte, and so on.
</t>
<t>
   Each 5-bit group is used as an index into an array of 32 printable
   characters.  The character referenced by the index is placed in the
   output string.  These characters, identified in <xref target="the.base.32.alphabet"/>, below, are
   selected from US-ASCII digits and uppercase letters.
</t>
<texttable title='The Base 32 Alphabet' style="none" anchor="the.base.32.alphabet" x:caption-side="top">
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <c> 0</c><c>A</c>
  <c> 9</c><c>J</c>
  <c>18</c><c>S</c>
  <c>27</c><c>3</c>
  <c> 1</c><c>B</c>
  <c>10</c><c>K</c>
  <c>19</c><c>T</c>
  <c>28</c><c>4</c>
  <c> 2</c><c>C</c>
  <c>11</c><c>L</c>
  <c>20</c><c>U</c>
  <c>29</c><c>5</c>
  <c> 3</c><c>D</c>
  <c>12</c><c>M</c>
  <c>21</c><c>V</c>
  <c>30</c><c>6</c>
  <c> 4</c><c>E</c>
  <c>13</c><c>N</c>
  <c>22</c><c>W</c>
  <c>31</c><c>7</c>
  <c> 5</c><c>F</c>
  <c>14</c><c>O</c>
  <c>23</c><c>X</c>
  <c/><c/>
  <c> 6</c><c>G</c>
  <c>15</c><c>P</c>
  <c>24</c><c>Y</c>
  <c>(pad)</c><c>=</c>
  <c> 7</c><c>H</c>
  <c>16</c><c>Q</c>
  <c>25</c><c>Z</c>
  <c/><c/>
  <c> 8</c><c>I</c>
  <c>17</c><c>R</c>
  <c>26</c><c>2</c>
  <c/><c/>
</texttable>
<t>
   Special processing is performed if fewer than 40 bits are available
   at the end of the data being encoded.  A full encoding quantum is
   always completed at the end of a body.  When fewer than 40 input bits
   are available in an input group, bits with value zero are added (on
   the right) to form an integral number of 5-bit groups.  Padding at
   the end of the data is performed using the "=" character.  Since all
   base 32 input is an integral number of octets, only the following
   cases can arise:
</t>
<t>
  <list style="format (%d)" counter="list-b32">
    <t>The final quantum of encoding input is an integral multiple of 40
       bits; here, the final unit of encoded output will be an integral
       multiple of 8 characters with no "=" padding.</t>
    <t>The final quantum of encoding input is exactly 8 bits; here, the
       final unit of encoded output will be two characters followed by
       six "=" padding characters.</t>
    <t>The final quantum of encoding input is exactly 16 bits; here, the
       final unit of encoded output will be four characters followed by
       four "=" padding characters.</t>
    <t>The final quantum of encoding input is exactly 24 bits; here, the
       final unit of encoded output will be five characters followed by
       three "=" padding characters.</t>
    <t>The final quantum of encoding input is exactly 32 bits; here, the
       final unit of encoded output will be seven characters followed by
       one "=" padding character.</t>
  </list>
</t>
</section>
<section title="Base 32 Encoding with Extended Hex Alphabet" anchor="base.32.encoding.with.extended.hex.alphabet">
<t>
   The following description of base 32 is derived from <xref target="RFC2938"/>.  This
   encoding may be referred to as "base32hex".  This encoding should not
   be regarded as the same as the "base32" encoding and should not be
   referred to as only "base32".  This encoding is used by, e.g.,
   NextSECure3 (NSEC3) <xref target="DNSSECHASH"/>.
</t>
<t>
   One property with this alphabet, which the base64 and base32
   alphabets lack, is that encoded data maintains its sort order when
   the encoded data is compared bit-wise.
</t>
<t>
   This encoding is identical to the previous one, except for the
   alphabet.  The new alphabet is found in <xref target="the.extended.hex.base.32.alphabet"/>.
</t>
<texttable title='The "Extended Hex" Base 32 Alphabet' style="none" anchor="the.extended.hex.base.32.alphabet" x:caption-side="top">
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <c> 0</c><c>0</c>
  <c> 9</c><c>9</c>
  <c>18</c><c>I</c>
  <c>27</c><c>R</c>
  <c> 1</c><c>1</c>
  <c>10</c><c>A</c>
  <c>19</c><c>J</c>
  <c>28</c><c>S</c>
  <c> 2</c><c>2</c>
  <c>11</c><c>B</c>
  <c>20</c><c>K</c>
  <c>29</c><c>T</c>
  <c> 3</c><c>3</c>
  <c>12</c><c>C</c>
  <c>21</c><c>L</c>
  <c>30</c><c>U</c>
  <c> 4</c><c>4</c>
  <c>13</c><c>D</c>
  <c>22</c><c>M</c>
  <c>31</c><c>V</c>
  <c> 5</c><c>5</c>
  <c>14</c><c>E</c>
  <c>23</c><c>N</c>
  <c/><c/>
  <c> 6</c><c>6</c>
  <c>15</c><c>F</c>
  <c>24</c><c>O</c>
  <c>(pad)</c><c>=</c>
  <c> 7</c><c>7</c>
  <c>16</c><c>G</c>
  <c>25</c><c>P</c>
  <c/><c/>
  <c> 8</c><c>8</c>
  <c>17</c><c>H</c>
  <c>26</c><c>Q</c>
  <c/><c/>
</texttable>
</section>
<section title="Base 16 Encoding" anchor="base.16.encoding">
<t>
   The following description is original but analogous to previous
   descriptions.  Essentially, Base 16 encoding is the standard case-insensitive
hex encoding and may be referred to as "base16" or "hex".
</t>
<t>
   A 16-character subset of US-ASCII is used, enabling 4 bits to be
   represented per printable character.
</t>
<t>
   The encoding process represents 8-bit groups (octets) of input bits
   as output strings of 2 encoded characters.  Proceeding from left to
   right, an 8-bit input is taken from the input data.  These 8 bits are
   then treated as 2 concatenated 4-bit groups, each of which is
   translated into a single character in the base 16 alphabet.
</t>
<t>
   Each 4-bit group is used as an index into an array of 16 printable
   characters.  The character referenced by the index is placed in the
   output string.
</t>
<texttable title='The Base 16 Alphabet' style="none" anchor="the.base.16.alphabet" x:caption-side="top">
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <ttcol align="right">Value</ttcol>
  <ttcol align="left">Encoding</ttcol>
  <c> 0</c><c>0</c>
  <c> 4</c><c>4</c>
  <c> 8</c><c>8</c>
  <c>12</c><c>C</c>
  <c> 1</c><c>1</c>
  <c> 5</c><c>5</c>
  <c> 9</c><c>9</c>
  <c>13</c><c>D</c>
  <c> 2</c><c>2</c>
  <c> 6</c><c>6</c>
  <c>10</c><c>A</c>
  <c>14</c><c>E</c>
  <c> 3</c><c>3</c>
  <c> 7</c><c>7</c>
  <c>11</c><c>B</c>
  <c>15</c><c>F</c>
</texttable>
<t>
   Unlike base 32 and base 64, no special padding is necessary since a
   full code word is always available.
</t>
</section>
<section title="Illustrations and Examples" anchor="illustrations.and.examples">
<t>
   To translate between binary and a base encoding, the input is stored
   in a structure, and the output is extracted.  The case for base 64 is
   displayed in the following figure, borrowed from <xref target="RFC2440"/>.
</t>
<figure><artwork type="example" x:indent-with="         ">
+--first octet--+-second octet--+--third octet--+
|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
+-----------+---+-------+-------+---+-----------+
|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|
+--1.index--+--2.index--+--3.index--+--4.index--+
</artwork></figure>
<t>
   The case for base 32 is shown in the following figure, borrowed from
   <xref target="RFC2938"/>.  Each successive character in a base-32 value represents 5
   successive bits of the underlying octet sequence.  Thus, each group
   of 8 characters represents a sequence of 5 octets (40 bits).
</t>
<figure><artwork type="example" x:indent-with="         "><![CDATA[
            1          2          3
 01234567 89012345 67890123 45678901 23456789
+--------+--------+--------+--------+--------+
|< 1 >< 2| >< 3 ><|.4 >< 5.|>< 6 ><.|7 >< 8 >|
+--------+--------+--------+--------+--------+
                                        <===> 8th character
                                  <====> 7th character
                             <===> 6th character
                       <====> 5th character
                 <====> 4th character
            <===> 3rd character
      <====> 2nd character
 <===> 1st character
]]></artwork></figure>
<t>             
   The following example of Base64 data is from <xref target="RFC2440"/>, with corrections.
</t>
<figure><artwork type="example" x:indent-with="   ">
Input data:  0x14fb9c03d97e
Hex:     1   4    f   b    9   c     | 0   3    d   9    7   e
8-bit:   00010100 11111011 10011100  | 00000011 11011001 01111110
6-bit:   000101 001111 101110 011100 | 000000 111101 100101 111110
Decimal: 5      15     46     28       0      61     37     62
Output:  F      P      u      c        A      9      l      +
</artwork></figure>
<figure><artwork type="example" x:indent-with="   ">
Input data:  0x14fb9c03d9
Hex:     1   4    f   b    9   c     | 0   3    d   9
8-bit:   00010100 11111011 10011100  | 00000011 11011001
                                                pad with 00
6-bit:   000101 001111 101110 011100 | 000000 111101 100100
Decimal: 5      15     46     28       0      61     36
                                                   pad with =
Output:  F      P      u      c        A      9      k      =
</artwork></figure>
<figure><artwork type="example" x:indent-with="   ">
Input data:  0x14fb9c03
Hex:     1   4    f   b    9   c     | 0   3
8-bit:   00010100 11111011 10011100  | 00000011
                                       pad with 0000
6-bit:   000101 001111 101110 011100 | 000000 110000
Decimal: 5      15     46     28       0      48
                                            pad with =      =
Output:  F      P      u      c        A      w      =      =
</artwork></figure>
</section>
<section title="Test Vectors" anchor="test.vectors">
<figure><artwork type="example">
   BASE64("") = ""

   BASE64("f") = "Zg=="

   BASE64("fo") = "Zm8="

   BASE64("foo") = "Zm9v"

   BASE64("foob") = "Zm9vYg=="

   BASE64("fooba") = "Zm9vYmE="

   BASE64("foobar") = "Zm9vYmFy"

   BASE32("") = ""

   BASE32("f") = "MY======"

   BASE32("fo") = "MZXQ===="

   BASE32("foo") = "MZXW6==="

   BASE32("foob") = "MZXW6YQ="

   BASE32("fooba") = "MZXW6YTB"

   BASE32("foobar") = "MZXW6YTBOI======"

   BASE32-HEX("") = ""

   BASE32-HEX("f") = "CO======"

   BASE32-HEX("fo") = "CPNG===="

   BASE32-HEX("foo") = "CPNMU==="

   BASE32-HEX("foob") = "CPNMUOG="

   BASE32-HEX("fooba") = "CPNMUOJ1"

   BASE32-HEX("foobar") = "CPNMUOJ1E8======"

   BASE16("") = ""

   BASE16("f") = "66"

   BASE16("fo") = "666F"

   BASE16("foo") = "666F6F"

   BASE16("foob") = "666F6F62"

   BASE16("fooba") = "666F6F6261"

   BASE16("foobar") = "666F6F626172"
</artwork></figure>
</section>
<section title="ISO C99 Implementation of Base64" anchor="iso.c99.implementation.of.base64">
<t>
   An ISO C99 implementation of Base64 encoding and decoding that is
   believed to follow all recommendations in this RFC is available from:
</t>
<aside>
  <t>
      <eref target="http://josefsson.org/base-encoding/">http://josefsson.org/base-encoding/</eref>
  </t>
</aside>
<t>
   This code is not normative.
</t>
<t>
   The code could not be included in this RFC for procedural reasons
   (<eref target="https://tools.ietf.org/html/rfc3978#section-5.4">RFC 3978 section 5.4</eref>).
</t>   
</section>
<section title="Security Considerations" anchor="security.considerations">
<t>
   When base encoding and decoding is implemented, care should be taken
   not to introduce vulnerabilities to buffer overflow attacks, or other
   attacks on the implementation.  A decoder should not break on invalid
   input including, e.g., embedded NUL characters (ASCII 0).
</t>
<t>
   If non-alphabet characters are ignored, instead of causing rejection
   of the entire encoding (as recommended), a covert channel that can be
   used to "leak" information is made possible.  The ignored characters
   could also be used for other nefarious purposes, such as to avoid a
   string equality comparison or to trigger implementation bugs.  The
   implications of ignoring non-alphabet characters should be understood
   in applications that do not follow the recommended practice.
   Similarly, when the base 16 and base 32 alphabets are handled case
   insensitively, alteration of case can be used to leak information or
   make string equality comparisons fail.
</t>
<t>
   When padding is used, there are some non-significant bits that
   warrant security concerns, as they may be abused to leak information
   or used to bypass string equality comparisons or to trigger
   implementation problems.
</t>
<t>
   Base encoding visually hides otherwise easily recognized information,
   such as passwords, but does not provide any computational
   confidentiality.  This has been known to cause security incidents
   when, e.g., a user reports details of a network protocol exchange
   (perhaps to illustrate some other problem) and accidentally reveals
   the password because she is unaware that the base encoding does not
   protect the password.
</t>
<t>
   Base encoding adds no entropy to the plaintext, but it does increase
   the amount of plaintext available and provide a signature for
   cryptanalysis in the form of a characteristic probability
   distribution.
</t>
</section>
<section title="Changes Since RFC 3548" anchor="changes.since.rfc.3548">
<t>
   Added the "base32 extended hex alphabet", needed to preserve sort
   order of encoded data.
</t>
<t>
   Referenced IMAP for the special Base64 encoding used there.
</t>
<t>
   Fixed the example copied from RFC 2440.
</t>
<t>
   Added security consideration about providing a signature for
   cryptoanalysis.
</t>
<t>
   Added test vectors.
</t>
<t>
   Fixed typos.
</t>
</section>
<section title="Acknowledgements" anchor="acknowledgements">
<t>
   Several people offered comments and/or suggestions, including John E.
   Hadstate, Tony Hansen, Gordon Mohr, John Myers, Chris Newman, and
   Andrew Sieber.  Text used in this document are based on earlier RFCs
   describing specific uses of various base encodings.  The author
   acknowledges the RSA Laboratories for supporting the work that led to
   this document.
</t>
<t>
   This revised version is based in parts on comments and/or suggestions
   made by Roy Arends, Eric Blake, Brian E Carpenter, Elwyn Davies, Bill
   Fenner, Sam Hartman, Ted Hardie, Per Hygum, Jelte Jansen, Clement
   Kent, Tero Kivinen, Paul Kwiatkowski, and Ben Laurie.
</t>
</section>
<section title="Copying Conditions" anchor="copying.conditions">
<t>
   Copyright (c) 2000-2006 Simon Josefsson
</t>
<t>
   Regarding the abstract and sections <xref target="introduction" format="counter"/>, <xref target="implementation.discrepancies" format="counter"/>, <xref target="base.16.encoding" format="counter"/>, <xref target="test.vectors" format="counter"/>, <xref target="security.considerations" format="counter"/>, <xref target="changes.since.rfc.3548" format="counter"/>, and <xref target="acknowledgements" format="counter"/> of
   this document, that were written by Simon Josefsson ("the author",
   for the remainder of this section), the author makes no guarantees
   and is not responsible for any damage resulting from its use.  The
   author grants irrevocable permission to anyone to use, modify, and
   distribute it in any way that does not diminish the rights of anyone
   else to use, modify, and distribute it, provided that redistributed
   derivative works do not contain misleading author or version
   information and do not falsely purport to be IETF RFC documents.
   Derivative works need not be licensed under similar terms.
</t>
</section>
</middle>
<back>
<references title="Normative References">
  <reference  anchor='RFC0020'>
    <front>
      <title>ASCII format for network interchange</title>
      <author initials='V.' surname='Cerf' fullname='V. Cerf'/>
      <date year='1969' month='October' />
    </front>
    <seriesInfo name='RFC' value='20'/>
  </reference>
  <reference  anchor='RFC2119'>
    <front>
      <title>Key words for use in RFCs to Indicate Requirement Levels</title>
      <author initials='S.' surname='Bradner' fullname='S. Bradner'/>
      <date year='1997' month='March' />
    </front>
    <seriesInfo name='BCP' value='14'/>
    <seriesInfo name='RFC' value='2119'/>
  </reference>
</references>

<references title="Informative References">
  <reference anchor='RFC1421'>
    <front>
      <title>Privacy Enhancement for Internet Electronic Mail: Part I: Message Encryption and Authentication Procedures</title>
      <author initials='J.' surname='Linn' fullname='J. Linn'/>
      <date year='1993' month='February' />
    </front>
    <seriesInfo name='RFC' value='1421'/>
  </reference>
  <reference anchor='RFC2045'>
    <front>
      <title>Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies</title>
      <author initials='N.' surname='Freed' fullname='N. Freed'/>
      <author initials='N.' surname='Borenstein' fullname='N. Borenstein'/>
      <date year='1996' month='November' />
    </front>
    <seriesInfo name='RFC' value='2045'/>
  </reference>
  <reference anchor='RFC2440'>
    <front>
      <title>OpenPGP Message Format</title>
      <author initials='J.' surname='Callas' fullname='J. Callas'/>
      <author initials='L.' surname='Donnerhacke' fullname='L. Donnerhacke'/>
      <author initials='H.' surname='Finney' fullname='H. Finney'/>
      <author initials='R.' surname='Thayer' fullname='R. Thayer'/>
      <date year='1998' month='November' />
    </front>
    <seriesInfo name='RFC' value='2440'/>
  </reference>
  <reference anchor='RFC4033'>
    <front>
      <title>DNS Security Introduction and Requirements</title>
      <author initials='R.' surname='Arends' fullname='R. Arends'/>
      <author initials='R.' surname='Austein' fullname='R. Austein'/>
      <author initials='M.' surname='Larson' fullname='M. Larson'/>
      <author initials='D.' surname='Massey' fullname='D. Massey'/>
      <author initials='S.' surname='Rose' fullname='S. Rose'/>
      <date year='2005' month='March' />
    </front>
    <seriesInfo name='RFC' value='4033'/>
  </reference>
  <reference  anchor='RFC2938'>
    <front>
      <title>Identifying Composite Media Features</title>
      <author initials='G.' surname='Klyne' fullname='G. Klyne'/>
      <author initials='L.' surname='Masinter' fullname='L. Masinter'/>
      <date year='2000' month='September' />
    </front>
    <seriesInfo name='RFC' value='2938'/>
  </reference>
  <reference  anchor='RFC3501'>
    <front>
      <title>INTERNET MESSAGE ACCESS PROTOCOL - VERSION 4rev1</title>
      <author initials='M.' surname='Crispin' fullname='M. Crispin'/>
      <date year='2003' month='March' />
    </front>
    <seriesInfo name='RFC' value='3501'/>
  </reference>
  <reference anchor='RFC3986'>
    <front>
      <title>Uniform Resource Identifier (URI): Generic Syntax</title>
      <author initials='T.' surname='Berners-Lee' fullname='T. Berners-Lee'/>
      <author initials='R.' surname='Fielding' fullname='R. Fielding'/>
      <author initials='L.' surname='Masinter' fullname='L. Masinter'/>
      <date year='2005' month='January' />
    </front>
    <seriesInfo name='STD' value='66'/>
    <seriesInfo name='RFC' value='3986'/>
  </reference>
  <reference anchor='DNSSECHASH'>
    <front>
      <title>DNSSEC Hash Authenticated Denial of Existence</title>
      <author initials='B.' surname='Laurie' fullname='B. Laurie'/>
      <author initials='G.' surname='Sisson' fullname='G. Sisson'/>
      <author initials='R.' surname='Arends' fullname='R. Arends'/>
      <author initials='D.' surname='Blacka' fullname='D. Blacka'/>
      <date year='2006' month='June' />
    </front>
    <refcontent>Work in Progress</refcontent>
  </reference>
  <reference anchor='SASLGSSAPI'>
    <front>
      <title>SASL GSSAPI mechanisms</title>
      <author initials='J.' surname='Myers' fullname='J. Myers'/>
      <date year='2000' month='May' />
    </front>
    <refcontent>Work in Progress</refcontent>
  </reference>
  <reference anchor='P2PML' target="http://zgp.org/pipermail/p2p-hackers/2001-September/000315.html">
    <front>
      <title>Post to P2P-hackers mailing list</title>
      <author initials='B.' surname="Wilcox-O'Hearn" fullname="B. Wilcox-O'Hearn"/>
      <date year='2001' month='September' />
    </front>
  </reference>
</references>
</back>
</rfc>



