<?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>Softcore software development &#187; python</title>
	<atom:link href="http://tea.cesaroliveira.net/archives/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://tea.cesaroliveira.net</link>
	<description>It&#039;s all about the cycles</description>
	<lastBuildDate>Sun, 01 Aug 2010 02:04:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AES and CBC</title>
		<link>http://tea.cesaroliveira.net/archives/156</link>
		<comments>http://tea.cesaroliveira.net/archives/156#comments</comments>
		<pubDate>Tue, 22 Dec 2009 05:21:06 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[crypto]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://tea.cesaroliveira.net/?p=156</guid>
		<description><![CDATA[If you ever want to use a crypto library in Python, you&#8217;ll be sad to note that there isn&#8217;t one built into Python impressive repertoire of modules. In fact, you&#8217;ll most likely hit pycrypt on your Google search to find one. And there is some dirty work you&#8217;ll have to do if you want to [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever want to use a crypto library in Python, you&#8217;ll be sad to note that there isn&#8217;t one built into Python impressive repertoire of modules. In fact, you&#8217;ll most likely hit <a href="http://www.dlitz.net/software/pycrypto/" onclick="pageTracker._trackPageview('/outgoing/www.dlitz.net/software/pycrypto/?referer=');">pycrypt</a> on your Google search to find one. And there is some dirty work you&#8217;ll have to do if you want to use symmetric cryptography using this library. And one of the hard/easy parts is knowing the difference between ECB and CBC.</p>
<p>Here, we start initializing the AES object using CBC mode:</p>
<p><code>&gt;&gt;&gt; from Crypto.Cipher import AES;<br />
&gt;&gt;&gt; aes = AES.new('some key here', AES.MODE_CBC, 'INIT_VECTOR')<br />
Traceback (most recent call last):<br />
  File "&lt;console&gt;", line 1, in &lt;module&gt;<br />
ValueError: IV must be 16 bytes long</code></p>
<p>oops. You&#8217;ll have to make you&#8217;re initialization vector 16 bytes long. Also, your key has to be 16, 24, or 32 bytes long as well. Let&#8217;s do something better :</p>
<p><code>&gt;&gt;&gt; aes = AES.new('J2-+sfd%932mIt:{', AES.MODE_CBC, 'wir&amp;/&gt;H54mgd9a";')</code></p>
<p>ah! much better. Even if it was me smashing my hand against the keyboard. Now let&#8217;s encrypt/decrypt something important.</p>
<p><code>&gt;&gt;&gt; aes.encrypt('the answer to life the universe and everything is 42')<br />
Traceback (most recent call last):<br />
  File "&lt;console&gt;", line 1, in &lt;module&gt;<br />
ValueError: Input strings must be a multiple of 16 in length</code></p>
<p>You&#8217;ll have to do the dirty work remember:</p>
<p><code>>&gt;&gt;&gt; ciphertext = aes.encrypt('the answer to life the universe and everything is 42195479204957')<br />
>>> ciphertext<br />
'f0\xa9\xf9f&#038;X)\x0e\x08=\x06\x97\xcbF\xddK\x1a\xa6i\x1d\x02"}\xd9\\\xaa\xb6\xd9J\xe3Q\x07\xaev\x012\xbf\rPN\xd2\xf9\xf7$\x93\xe0/\xcb\xae9\x91K\xd01\xab\xb7\xdb\reR\xff\xef\x1c'</code></p>
<p>Much better. Now lets decrypt it:</p>
<p><code>&gt;&gt;&gt; aes.decrypt(ciphertext)<br />
'\xc8\xaf.\x97\x05\x80\n\xe9\xe6\xc4Ju\x04\xbe\xa1Nfe the universe and everything is 42195479204957'</code></p>
<p>Woah! That isn&#8217;t the whole message! So what&#8217;s going on?</p>
<p>Remember that initialization vector you set in the beginning? That sets the stage for the first block. But each block becomes the initialization vector for the second block, and so on. So when you decrypt, it is using the initialization vector from the block before. That&#8217;s why the first 16 bytes are screwed up. This is a feature of CBC, but not ECB:</p>
<p><code>&gt;&gt;&gt; aes = AES.new('J2-+sfd%932mIt:{', AES.MODE_ECB, 'wir&amp;/&gt;H54mgd9a";')<br />
&gt;&gt;&gt; ciphertext = aes.encrypt('the answer to life the universe and everything is 42195479204957')<br />
&gt;&gt;&gt; aes.decrypt(ciphertext)'the answer to life the universe and everything is 42195479204957'</code></p>
<p>And yes, this is a feature. Read the <a href="http://en.wikipedia.org/wiki/Cipher_block_chaining" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Cipher_block_chaining?referer=');">block cipher modes wikipedia article</a> for a better explination. So what&#8217;s the answer? Simply, to call aes.new() again before calling decrypt!</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/156/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The many ways around a problem</title>
		<link>http://tea.cesaroliveira.net/archives/17</link>
		<comments>http://tea.cesaroliveira.net/archives/17#comments</comments>
		<pubDate>Wed, 28 May 2008 16:11:13 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[intern]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[seneca]]></category>

		<guid isPermaLink="false">http://www.cesaroliveira.net/?p=17</guid>
		<description><![CDATA[I came across a bug in the zipfile python module yesterday that I had to fix today. The problem occurs when you try to create a ZipFile object and passing it a corrupt zip file. It doesn&#8217;t handle it gracefully like returning None or throwing an exception. Rather it heads into an infinite loop. This [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a bug in the zipfile python module yesterday that I had to fix today. The problem occurs when you try to create a ZipFile object and passing it a corrupt zip file. It doesn&#8217;t handle it gracefully like returning None or throwing an exception. Rather it heads into an infinite loop.</p>
<p>This is rather unfortunate for me. How would I get around this problem? The first thing I did was check for an updated python. Which there was a minor version upgrade. I found the changelog (why do they hide these things?) and noticed a few bugs resolved with the zipfile module. So I installed. Unfortunately, this didn&#8217;t solve my problem.</p>
<p>I managed to find a <a href="http://bugs.python.org/issue1622" onclick="pageTracker._trackPageview('/outgoing/bugs.python.org/issue1622?referer=');">bug number</a> in the python bug tracking software about people having similar problems. There was a patch, but hasn&#8217;t landed. I downloaded the latest stable version, but the patch wouldn&#8217;t go through. So I had to cvs checkout trunk and apply it. Once installed, I tried it and it worked! Success.</p>
<p>However, it broke other library I was using (PyXML). Unfortunate for me, the recent trunk build didn&#8217;t seem to fair any better.</p>
<p>At this point, I wasn&#8217;t in the mood for debugging. I had a few options at my disposal :</p>
<ol>
<li>Ignore this particular file</li>
<li>Suck it up and debug it.</li>
<li>Find a whacky work-around</li>
</ol>
<p>Option 1 isn&#8217;t an option. Option 2 I tried for a fair while, but nothing worked. So Option 3 was my only option!</p>
<p>I tried using a lower level library to see if I can fix the problem (zlib library), but that didn&#8217;t work well at all.</p>
<p>I finally thought I had no choice but to initiate a thread to try and unzip the xpi, and if it took longer than 10 seconds, to kill the thread somehow. While seriously looking into this, and fighting the temptation to take tequelia shots at work. I came across signals (which I thought I could use to send to the thread. I&#8217;m so naive). It turns out, you can throw a signal after a specific number of seconds and it throws the SIGALRM. This was <strong>exactly</strong> what I needed without the extra complexity. The <a href="http://docs.python.org/lib/node545.html" onclick="pageTracker._trackPageview('/outgoing/docs.python.org/lib/node545.html?referer=');">example provided</a> was almost exactly what I did too! Here is my solution to the problem :<br />
<code>
<pre>
		signal.signal(signal.SIGALRM, signal_handler)
		signal.alarm(10)
		try:
			zippy = zipfile.ZipFile(io, 'r')
			signal.alarm(0)
		except:
			print "\tZipFile Timeout"
			continue
</pre>
<p></code></p>
<p>Maybe python isn&#8217;t just for programming sissies after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/17/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
