<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Intrepid Blog &#187; base64</title>
	<atom:link href="http://blog.affien.com/archives/tag/base64/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.affien.com</link>
	<description>A few thoughts</description>
	<lastBuildDate>Mon, 23 Jan 2012 08:47:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Base64 encoding/decoding algorithm</title>
		<link>http://blog.affien.com/archives/2004/12/27/base64-encodingdecoding-algorithm/</link>
		<comments>http://blog.affien.com/archives/2004/12/27/base64-encodingdecoding-algorithm/#comments</comments>
		<pubDate>Mon, 27 Dec 2004 16:58:14 +0000</pubDate>
		<dc:creator>Bas Westerbaan</dc:creator>
				<category><![CDATA[Code sniplets]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[base64]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.w-nz.com/archives/2004/12/27/base64-encodingdecoding-algorithm/</guid>
		<description><![CDATA[I've made some python functions to encode/decode base64 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve made some python functions to encode/decode <a href="http://www.ietf.org/rfc/rfc2045.txt">base64</a>. I&#8217;ve been trying to develop my own algorithm for base64 for the <a href="http://blog.w-nz.com/archives/2004/12/22/update-on-the-anti-email-harvester-mailto-links/">email protection script</a> which can be found <a href="http://w-nz.com/tools/secureemail.php">here</a>.</p>
<p>Python again has proved itself again to be a great language for quickly developing stuff.</p>
<blockquote><pre>def tobase64(s, padd = False):
    b64s = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;
    b64p = &quot;=&quot;
    ret = &quot;&quot;
    left = 0
    for i in range(0, len(s)):
        if left == 0:
            ret += b64s[ord(s[i]) &gt;&gt; 2]
            left = 2
        else:
            if left == 6:
                ret += b64s[ord(s[i - 1]) &amp; 63]
                ret += b64s[ord(s[i]) &gt;&gt; 2]
                left = 2
            else:
                index1 = ord(s[i - 1]) &amp; (2 ** left - 1)
                index2 = ord(s[i]) &gt;&gt; (left + 2)
                index = (index1 &lt;&lt; (6 - left)) | index2
                ret += b64s[index]
                left += 2
    if left != 0:
        ret += b64s[(ord(s[len(s) - 1]) &amp; (2 ** left - 1)) &lt;&lt; (6 - left)]
    if(padd):
        for i in range(0, (4 - len(ret) % 4) % 4):
            ret += b64p
    return ret
def frombase64(s):
    b64s = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;
    b64p = &quot;=&quot;
    ret = &quot;&quot;
    s2 = s.replace(b64p, &quot;&quot;)
    left = 0
    for i in range(0, len(s2)):
        if left == 0:
            left = 6
        else:
            value1 = b64s.index(s2[i - 1]) &amp; (2 ** left - 1)
            value2 = b64s.index(s2[i]) &gt;&gt; (left - 2)
            value = (value1 &lt;&lt; (8 - left)) | value2
            ret += chr(value)
            left -= 2
    return ret</pre>
</blockquote>
<p>The algorithm doesn&#8217;t automaticly add the required <code>=</code>&#8216;s while encoding, nor does it require while deencoding.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.affien.com/archives/2004/12/27/base64-encodingdecoding-algorithm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

