<?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>Brandon Sterne &#187; Python</title>
	<atom:link href="http://brandon.sternefamily.net/posts/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://brandon.sternefamily.net/posts</link>
	<description>The Whole Brevity Thing</description>
	<lastBuildDate>Mon, 14 Mar 2011 22:52:08 +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>A-Star (A*) Algorithm in Python &#8211; Update</title>
		<link>http://brandon.sternefamily.net/posts/2010/01/a-star-a-algorithm-in-python-update/</link>
		<comments>http://brandon.sternefamily.net/posts/2010/01/a-star-a-algorithm-in-python-update/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 17:04:38 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=160</guid>
		<description><![CDATA[Since there have been many requests over the years for the source code referenced in my A-Star (A*) Algorithm post, I decided to share it. I did a bit of refactoring too, as I have learned some neat things about Python in the years since I wrote that post, like list comprehensions. A cautionary note [...]]]></description>
			<content:encoded><![CDATA[<p>Since there have been many requests over the years for the source code referenced in my <a href="/posts/2005/02/a-star-algorithm-in-python/">A-Star (A*) Algorithm</a> post, I decided to share it.  I did a bit of refactoring too, as I have learned some <a href="/posts/2006/11/8-reasons-why-python-rocks/">neat things about Python</a> in the years since I wrote that post, like <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehensions</a>.</p>
<p>A cautionary note to undergrad CS students (who I can only assume are the requestors): CS professors are pretty good at catching cheaters, so learn from others&#8217; code, but write your own.</p>
<div class="middleBlock center">
<p>Source: <a href="/files/astar.txt">astar.py</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2010/01/a-star-a-algorithm-in-python-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python AES Implementation &#8211; Update</title>
		<link>http://brandon.sternefamily.net/posts/2009/04/python-aes-update/</link>
		<comments>http://brandon.sternefamily.net/posts/2009/04/python-aes-update/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 15:55:16 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=115</guid>
		<description><![CDATA[I fixed two bugs in my AES implementation. The first was a padding bug which resulted in the loss of up to a block of data when decrypting certain ciphertexts. The second bug was a more serious security problem caused by the use of a static initialization vector.]]></description>
			<content:encoded><![CDATA[<p>I fixed two bugs  in my <a href="/posts/2007/06/aes-tutorial-python-implementation/">AES implementation</a>.  The first was a padding bug which resulted in the loss of up to a block of data when decrypting certain ciphertexts.  The second bug was a more serious security problem caused by the use of a static initialization vector.</p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2009/04/python-aes-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python CIDR Block Converter</title>
		<link>http://brandon.sternefamily.net/posts/2007/12/python-cidr-block-converter/</link>
		<comments>http://brandon.sternefamily.net/posts/2007/12/python-cidr-block-converter/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 15:26:40 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=103</guid>
		<description><![CDATA[I wrote a <a href="/posts/2007/12/python-cidr-block-converter/">Python script that converts a CIDR Block</a> into a list of individual IP addresses, one-per-line.]]></description>
			<content:encoded><![CDATA[<p>I wrote a Python script that converts a <a href="http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing">CIDR Block</a> into a list of individual IP addresses, one-per-line.  I found that I needed to repeat some network-related tasks across an entire subnet, and this script provides an easy way to automate these kinds of tasks in a <a href="http://en.wikipedia.org/wiki/Shell_%28computing%29">shell environment</a>.  The source code and sample usage for the script follow:</p>
<div style="border:1px dashed #000; background-color:rgb(240,240,240); padding:5px; width:90%; margin:1em auto;">
<p><b>Source Code: </b><a href="https://github.com/bsterne/bsterne-tools/blob/master/cidr/cidr.py">cidr.py</a></p>
<p><b>Sample Usage:</b></p>
<pre class="brush: bash; gutter: false">brandon@zodiac ~ $ cidr 192.168.1.5/30
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7</pre>
</div>
<p>One item to note is a key difference between the way this program computes a CIDR block and others I have seen.  The lazy way to convert a CIDR block to a list of IPs is to calculate the number of IP addresses in the subnet, <span class="code">(2^(32 &#8211; $subnetSize))</span>, and simply increment the base IP address that number of times.  This method is deficient because, as in the example usage above, the base IP address that is specified may fall somewhere in the middle of the range of IP addresses (not necessarily at the beginning).</p>
<p>In my script, I calculate the CIDR block members the correct way.  I am converting the base IP address to its binary form, zeroing-out the number of least significant bits as specified in the subnet size, and starting the enumeration of IP addresses at the bottom of that range.</p>
<p><script type="text/javascript" src="/posts/syntax/src/shCore.js"></script><br />
<script type="text/javascript" src="/posts/syntax/scripts/shBrushBash.js"></script></p>
<link rel="stylesheet" type="text/css" href="/posts/syntax/styles/shCore.css" />
<link type="text/css" rel="Stylesheet" href="/posts/syntax/styles/shThemeDefault.css"/>
<script type="text/javascript">SyntaxHighlighter.all()</script></p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2007/12/python-cidr-block-converter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AES Tutorial / Python Implementation</title>
		<link>http://brandon.sternefamily.net/posts/2007/06/aes-tutorial-python-implementation/</link>
		<comments>http://brandon.sternefamily.net/posts/2007/06/aes-tutorial-python-implementation/#comments</comments>
		<pubDate>Mon, 11 Jun 2007 04:51:50 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=91</guid>
		<description><![CDATA[I put together a series of slides as well as a <a href="/posts/2007/06/aes-tutorial-python-implementation/" title="AES Tutorial">Python implementation of AES</a>, the symmetric-key cryptosystem.]]></description>
			<content:encoded><![CDATA[<style type="text/css">
p.title {
	margin-top:0;
	text-align:center;
	font-weight:600;
	width: 100%;
	color:white;
	background-color:rgb(0,0,128);
}
.middleblock li {
	margin-bottom:.5em
}
ul li.indent0 {
	margin-left:0;
	margin-bottom:.5em
}
ul li.indent1 {
	margin-left:1em;
	list-style:circle;
	margin-bottom:.5em
}
ul li.indent2 {
	margin-left:1.5em;
	list-style:disc;
	margin-bottom:.5em
}
ul li.indent3 {
	margin-left:2em;
	list-style:none;
	margin-bottom:.5em
}
ol li.indent0 {
	margin:0;
	margin-bottom:.5em
}
.code {
	font-family:monospace;
	white-space:pre;
}
</style>
<div class="middleBlock">
<p><b>Update: 04-Apr-2009</b></p>
<p>I fixed two bugs in my AES implementation pointed out to me by <a href="http://www.ics.uci.edu/~jcarlson/">Josiah Carlson</a>.  First, I was failing to pad properly files whose length was an even multiple of the block size.  In those cases, bytes would be lost upon decrypting the file.  Josiah also pointed out that I was using a static <a href="http://en.wikipedia.org/wiki/Initialization_vector">IV</a>, which leaks information about messages which share common prefixes.  This is a serious security bug and I was glad to have it pointed out.</p>
<p>Feel free to check out the <a href="/files/pyAES_patch">changes</a> I made or simply download the <a href="/files/pyAES.txt">updated script</a>.</p>
</div>
<p>I&#8217;ve put together a series of slides as well as a Python implementation of AES, the symmetric-key cryptosystem.</p>
<div style="border: 1px dashed #000; background-color:rgb(240,240,240); padding:10px; margin:auto;">
<p style="text-align:center"><b>Source:</b> <a href="/files/pyAES.txt">pyAES.py</a></p>
<p><b>Sample Usage:</b> (color added for clarity)</p>
<p class="code" style="text-align:left">[brandon@zodiac pyAES]$ cat > testfile.txt<br />
<span style="color:#00b000">The sky was the color of television tuned to a dead channel.</span><br />
[brandon@zodiac pyAES]$ ./pyAES.py -e testfile.txt -o testfile_encrypted.txt<br />
Password:<br />
Encrypting file: testfile.txt<br />
Encryption complete.<br />
[brandon@zodiac pyAES]$ ./pyAES.py -d testfile_encrypted.txt -o testfile_decrypted.txt<br />
Password:<br />
Decrypting file: testfile_encrypted.txt<br />
Decryption complete.<br />
[brandon@zodiac pyAES]$ cat testfile_decrypted.txt<br />
<span style="color:#00b000">The sky was the color of television tuned to a dead channel.</span><br />
[brandon@zodiac pyAES]$ md5sum *<br />
19725cef7495fd55540728759a6262c8  pyAES.py<br />
<span style="color:#00b000">2fffc9072a7c09f4f97862c0bceb6021  testfile_decrypted.txt</span><br />
<span style="color:#b00000">3e57070eaf1b4adf7f43b38e1c5ee631  testfile_encrypted.txt</span><br />
<span style="color:#00b000">2fffc9072a7c09f4f97862c0bceb6021  testfile.txt</span>
</p>
</div>
<p>					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">Symmetric Key Cryptography</p>
<ul>
<li class="indent0">Identical keys used to encrypt/decrypt messages</li>
<li class="indent0">Can be implemented as block ciphers or stream ciphers</li>
</ul>
<p>					<b>Strengths:</b></p>
<ul>
<li class="indent0">Speed</li>
<li class="indent1">Much less computationally intensive than public-key crypto</li>
<li class="indent0">Easy to implement in hardware as well as software</li>
</ul>
<p>					<b>Weaknesses:</b></p>
<ul>
<li class="indent0">Key Management</li>
<li class="indent1"><em>n</em> users require <em>n</em>(<em>n</em>-1)/2 keys for all to communicate</li>
<li class="indent1">secure key distribution is a challenge</li>
<li class="indent0">Cannot be used (directly) for authentication or non-repudiation</li>
</ul></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES &#8211; The Advanced Encryption Standard</p>
<ul>
<li class="indent0">Rijndael algorithm invented by Joan Daemen and Vincent Rijmen and selected as AES winner by NIST in 2001</li>
<li class="indent0">AES uses fixed block size of 128-bits and key sizes of 128, 192 or 256 bits (though Rijndael specification allows for variable block and key sizes)</li>
<li class="indent0">Most of the calculations in AES are performed within a finite field</li>
<li class="indent1">There are a finite number of elements within the field and all operations on those elements result in an element also contained in the field</li>
</ul></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES Operations</p>
<ul>
<li class="indent0">AES operates on a 4&#215;4 matrix referred to as the <em>state</em></li>
<li class="indent1">16 bytes == 128 bits == block size</li>
<li class="indent0">All operations in a round of AES are invertible</li>
<li class="indent1">AddRoundKey &#8211; each byte of the round key is combined with the corresponding byte in the state using XOR</li>
<li class="indent1">SubBytes &#8211; each byte in the state is replaced with a different byte according to the S-Box lookup table</li>
<li class="indent1">ShiftRows &#8211; each row in the state table is shifted by a varying number of bytes</li>
<li class="indent1">MixColumns &#8211; each column in the state table is multiplied with a fixed polynomial</li>
</ul></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES Operation &#8211; AddRoundKey</p>
<ul>
<li class="indent0">Each byte of the round key is XORed with the corresponding byte in the state table</li>
<li class="indent0">Inverse operation is identical since XOR a second time returns the original values</li>
</ul>
<pre class="brush: python; gutter: false"># XOR each byte of the roundKey with the state table
def addRoundKey(state, roundKey):
    for i in range(len(state)):
        state[i] = state[i] ^ roundKey[i]</pre>
</p></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES Operation &#8211; SubBytes</p>
<ul>
<li class="indent0">Each byte of the state table is substituted with the value in the S-Box whose index is the value of the state table byte</li>
<li class="indent0">Provides non-linearity (algorithm not equal to the sum of its parts)</li>
<li class="indent0">Inverse operation is performed using the inverted S-Box</li>
</ul>
<pre class="brush: python; gutter: false"># do sbox transform on each of the values in the state table
def subBytes(state):
    for i in range(len(state)):
        state[i] = sbox[state[i]]

# sbox transformations are invertible
>>> sbox[237]
85
>>> sboxInv[85]
237
>>> sbox[55]
154
>>> sbox[154]
184
>>> sboxInv[184]
154
>>> sboxInv[154]
55</pre>
</p></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES Operation &#8211; ShiftRows</p>
<ul>
<li class="indent0">Each row in the state table is shifted left by the number of bytes represented by the row number</li>
<li class="indent0">Inverse operation simply shifts each row to the right by the number of bytes as the row number</li>
</ul>
<pre class="brush: python; gutter: false"># returns a copy of the word shifted n bytes (chars) positive
# values for n shift bytes left, negative values shift right
def rotate(word, n):
    return word[n:]+word[0:n]

# iterate over each "virtual" row in the state table
# and shift the bytes to the LEFT by the appropriate
# offset
def shiftRows(state):
    for i in range(4):
        state[i*4:i*4+4] = rotate(state[i*4:i*4+4],i)</pre>
</p></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES Operation &#8211; MixColumns</p>
<ul>
<li class="indent0">MixColumns is performed by multiplying each column (within the <a href="http://en.wikipedia.org/wiki/Rijndael_Galois_field">Galois finite field</a>) by the following matrix:</li>
</ul>
<p>					<img src="images/aes_mix_columns_matrix.png" alt="" /></p>
<ul>
<li class="indent0">The inverse operation is performed by multiplying each column by the following inverse matrix:</li>
</ul>
<p>					<img src="images/aes_mix_columns_inv_matrix.png" alt="" /></p>
<pre class="brush: python; gutter: false"># Galois Multiplication
def galoisMult(a, b):
    p = 0
    hiBitSet = 0
    for i in range(8):
        if b &amp; 1 == 1:
            p ^= a
        hiBitSet = a &amp; 0x80
        a &lt;&lt;= 1
        if hiBitSet == 0x80:
            a ^= 0x1b
        b &gt;&gt;= 1
    return p % 256

# mixColumn does Galois multiplication on a state column
def mixColumn(column):
    temp = copy(column)
    column[0] = galoisMult(temp[0],2) ^ galoisMult(temp[3],1) ^ \
                galoisMult(temp[2],1) ^ galoisMult(temp[1],3)
    column[1] = galoisMult(temp[1],2) ^ galoisMult(temp[0],1) ^ \
                galoisMult(temp[3],1) ^ galoisMult(temp[2],3)
    column[2] = galoisMult(temp[2],2) ^ galoisMult(temp[1],1) ^ \
                galoisMult(temp[0],1) ^ galoisMult(temp[3],3)
    column[3] = galoisMult(temp[3],2) ^ galoisMult(temp[2],1) ^ \
                galoisMult(temp[1],1) ^ galoisMult(temp[0],3)</pre>
</p></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES &#8211; Pulling It All Together</p>
<p>					The AES Cipher operates using a varying <b>number of rounds</b>, based on the size of the <b>cipher key</b>.</p>
<ul>
<li class="indent0">A <b>round</b> of AES consists of the four operations performed in succession: AddRoundKey, SubBytes, ShiftRows, and MixColumns (MixColumns is omitted in the final round)</li>
<li class="indent0">128-bit key &rarr; rounds, 192-bit key &rarr; 12 rounds, 256-bit key &rarr; 14 rounds</li>
<li class="indent0">The AES cipher key is expanded according to the Rijndael key schedule and a different part of the expanded key is used for each round of AES</li>
<li class="indent1">The expanded key will be of length <b>(block size * num rounds+1)</b></li>
<li class="indent1">128-bit cipher key expands to 176-byte key</li>
<li class="indent1">192-bit cipher key expands to 208-byte key</li>
<li class="indent1">256-bit cipher key expands to 240-byte key</li>
</ul></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES &#8211; Key Expansion Operations</p>
<p>					AES key expansion consists of several primitive operations:</p>
<ol>
<li class="indent0">Rotate &#8211; takes a 4-byte word and rotates everything one byte to the left, e.g. <span style="code">rotate([1,2,3,4]) &rarr; [2, 3, 4, 1]</span></li>
<li class="indent0">SubBytes &#8211; each byte of a word is substituted with the value in the S-Box whose index is the value of the original byte</li>
<li class="indent0">Rcon &#8211; the first byte of a word is XORed with the <b>round constant</b>.  Each value of the Rcon table is a member of the Rinjdael finite field.</li>
</ol>
<pre class="brush: python; gutter: false"># takes 4-byte word and iteration number
def keyScheduleCore(word, i):
    # rotate word 1 byte to the left
    word = rotate(word, 1)
    newWord = []
    # apply sbox substitution on all bytes of word
    for byte in word:
        newWord.append(sbox[byte])
    # XOR the output of the rcon[i] transformation with the first part
    # of the word
    newWord[0] = newWord[0]^rcon[i]
    return newWord</pre>
</p></div>
<p>					<!-- end Slide --></p>
<p>					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES &#8211; Key Expansion Algorithm (256-bit)</p>
<p>					Pseudo-code for AES Key Expansion:</p>
<ol>
<li class="indent0 code"><b>expandedKey</b>[0:32] &rarr; <b>cipherKey</b>[0:32]  # copy first 32 bytes of cipher key to expanded key</li>
<li class="indent0 code"><b>i</b> &rarr; 1  # Rcon iterator</li>
<li class="indent0 code"><b>temp</b> = byte[4]  # 4-byte container for temp storage</li>
<li class="indent0 code">while size(<b>expandedKey</b>) &lt; 240<br />
    <b>temp</b> &rarr; last 4 bytes of <b>expandedKey</b></p>
<p>    # every 32 bytes apply core schedule to temp<br />
    if size(<b>expandedKey</b>)%32 == 0<br />
        <b>temp</b> = keyScheduleCore(<b>temp</b>, <b>i</b>)<br />
        <b>i</b> &rarr; <b>i</b> + 1<br />
    # since 256-bit key -> add an extra sbox transformation to each new byte<br />
    for <b>j</b> in range(4):<br />
        <b>temp</b>[<b>j</b>] = <b>sbox</b>[<b>temp</b>[<b>j</b>]]<br />
    # XOR temp with the 4-byte block 32 bytes before the end of the current expanded key.<br />
    # These 4 bytes become the next bytes in the expanded key<br />
    <b>expandedKey</b>.append( <b>temp</b> XOR <b>expandedKey</b>[size(<b>expandedKey</b>)-32:size(<b>expandedKey</b>)-28]</li>
</ol>
<p>					Another function to note&#8230;</p>
<pre class="brush: python; gutter: false"># returns a 16-byte round key based on an expanded key and round number
def createRoundKey(expandedKey, n):
    return expandedKey[(n*16):(n*16+16)]</pre>
</p></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES &#8211; Encrypting a Single Block</p>
<ol>
<li class="indent0"><b>state</b> &rarr; block of plaintext    # 16 bytes of plaintext are copied into the state</li>
<li class="indent0"><b>expandedKey</b> = expandKey(<b>cipherKey</b>)    # create 240-bytes of key material to be used as round keys</li>
<li class="indent0"><b>roundNum</b> &rarr; 0    # counter for which round number we are in</li>
<li class="indent0"><b>roundKey</b> &rarr; createRoundKey(<b>expandedKey</b>, <b>roundNum</b>)</li>
<li class="indent0">addRoundKey(<b>state</b>, <b>roundKey</b>)    # each byte of state is XORed with the present roundKey</li>
<li class="indent0">while <b>roundNum</b> &lt; 14   # 14 rounds in AES-256<br />
    <b>roundKey</b> &rarr; createRoundKey(<b>expandedKey</b>, <b>roundNum</b>)<br />
    # round of AES consists of 1. subBytes, 2. shiftRows, 3. mixColumns, and 4. addRoundKey<br />
    aesRound(<b>state</b>, <b>roundKey</b>)<br />
    <b>roundNum</b> &rarr; <b>roundNum</b> + 1</li>
<li class="indent0"># for the last round leave out the mixColumns operation<br />
    <b>roundKey</b> = createRoundKey(<b>expandedKey</b>, <b>roundNum</b>)<br />
    subBytes(<b>state</b>)<br />
    shiftRows(<b>state</b>)<br />
    addRoundKey(<b>state</b>)</li>
<li class="indent0">return <b>state</b> as block of ciphertext</li>
</ol></div>
<p>					<!-- end Slide --><br />
					<!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">AES &#8211; Encrypting a Single Block (Demo)</p>
<pre class="brush: python; gutter: false">
>>> key = passwordToKey("s0m3_p@ssw0rD")
>>> key
[62, 142, 78, 2, 164, 231, 18, 196, 148, 177, 82, 186, 240, 44, 136, 242,
23, 13, 20, 169, 248, 69, 163, 79, 13, 155, 97, 200, 241, 15, 76, 15]
>>> plaintext = textToBlock("Hiro Protagonist")
>>> plaintext
[72, 105, 114, 111, 32, 80, 114, 111, 116, 97, 103, 111, 110, 105, 115, 116]
>>> blockToText(plaintext)
'Hiro Protagonist'
>>> ciphertext = aesEncrypt(plaintext, key)

*** aesMain ***
initial state:
[72, 105, 114, 111, 32, 80, 114, 111, 116, 97, 103, 111, 110, 105, 115, 116]
state after adding roundKey0:
[118, 231, 60, 109, 132, 183, 96, 171, 224, 208, 53, 213, 158, 69, 251, 134]

*** AES Round1 ***
state after subBytes:
[56, 148, 235, 60, 95, 169, 208, 98, 225, 112, 150, 3, 11, 110, 15, 68]
state after shiftRows:
[56, 148, 235, 60, 169, 208, 98, 95, 150, 3, 225, 112, 68, 11, 110, 15]
state after mixColumns:
[66, 80, 228, 230, 148, 33, 121, 29, 106, 95, 226, 146, 255, 98, 121, 117]
state after addRoundKey:
[85, 93, 240, 79, 108, 100, 218, 82, 103, 196, 131, 90, 14, 109, 53, 122]

&lt;-- SNIP --&gt;

*** AES Round 14 (final) ***
state after subBytes:
[0, 229, 171, 70, 93, 137, 135, 251, 99, 182, 88, 166, 228, 229, 251, 97]
state after shiftRows:
[0, 229, 171, 70, 137, 135, 251, 93, 88, 166, 99, 182, 97, 228, 229, 251]
state after addRoundKey:
[195, 123, 205, 183, 213, 202, 50, 223, 223, 164, 99, 86, 126, 34, 107, 142]

>>> ciphertext
[195, 123, 205, 183, 213, 202, 50, 223, 223, 164, 99, 86, 126, 34, 107, 142]

>>> blockToText(ciphertext)
'\xc3{\xcd\xb7\xd5\xca2\xdf\xdf\xa4cV~"k\x8e'
>>> cleartext = aesDecrypt(ciphertext, key)

*** aesMainInv ***
initial state:
[195, 123, 205, 183, 213, 202, 50, 223, 223, 164, 99, 86, 126, 34, 107, 142]

*** AES Round 14 ***
state after addRoundKey:
[0, 229, 171, 70, 137, 135, 251, 93, 88, 166, 99, 182, 97, 228, 229, 251]
state after shiftRowsInv:
[0, 229, 171, 70, 93, 137, 135, 251, 99, 182, 88, 166, 228, 229, 251, 97]
state after subBytesInv:
[82, 42, 14, 152, 141, 242, 234, 99, 0, 121, 94, 197, 174, 42, 99, 216]

&lt;-- SNIP --&gt;

*** AES Round 0 (final) ***
state after adding roundKey0:
[72, 105, 114, 111, 32, 80, 114, 111, 116, 97, 103, 111, 110, 105, 115, 116]
>>> cleartext
[72, 105, 114, 111, 32, 80, 114, 111, 116, 97, 103, 111, 110, 105, 115, 116]
>>> blockToText(cleartext)
'Hiro Protagonist'</pre>
</p></div>
<p>					<!-- end Slide --></p>
<p><script type="text/javascript" src="/posts/syntax/src/shCore.js"></script><br />
<script type="text/javascript" src="/posts/syntax/scripts/shBrushPython.js"></script></p>
<link rel="stylesheet" type="text/css" href="/posts/syntax/styles/shCore.css" />
<link type="text/css" rel="Stylesheet" href="/posts/syntax/styles/shThemeDefault.css"/>
<script type="text/javascript">SyntaxHighlighter.all()</script></p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2007/06/aes-tutorial-python-implementation/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>8 Reasons Why Python Rocks</title>
		<link>http://brandon.sternefamily.net/posts/2006/11/8-reasons-why-python-rocks/</link>
		<comments>http://brandon.sternefamily.net/posts/2006/11/8-reasons-why-python-rocks/#comments</comments>
		<pubDate>Tue, 28 Nov 2006 02:21:16 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=75</guid>
		<description><![CDATA[Since I find myself giving this pitch to anyone who will listen, I decided to formalize my argument and put together some slides illustrating just a few of the many reasons <a href="/posts/2006/11/8-reasons-why-python-rocks/">why Python rocks</a>.]]></description>
			<content:encoded><![CDATA[<style type="text/css">
p.title {
  margin-top:0;
  text-align:center;
  font-weight:600;
  width: 100%;
  color:white;
  background-color:rgb(0,0,128);
}
.middleblock li {
  margin-bottom:.5em
}
ul li.indent0 {
  margin-left:0;
  margin-bottom:.5em
}
ul li.indent1 {
  margin-left:1em;
  list-style:circle;
  margin-bottom:.5em
}
ul li.indent2 {
  margin-left:1.5em;
  list-style:disc;
  margin-bottom:.5em
}
ul li.indent3 {
  margin-left:2em;
  list-style:none;
  margin-bottom:.5em
}
ol li.indent0 {
 margin:0;
  margin-bottom:.5em
}
.code {
  font-family:monospace;
  white-space:pre;
}
</style>
<p>            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">First Things First</p>
<ul>
<li class="indent0">Where can I download Python?</li>
<li class="indent1"><a href="http://python.org">http://python.org</a> &#8211; Windows, OS X, Unix (ships with almost every distro)</li>
<li class="indent0">Any good references?</li>
<li class="indent1"><a href="http://docs.python.org/tut/tut.html">http://docs.python.org/tut/tut.html</a> &#8211; tutorial by Python creator</li>
<li class="indent1"><a href="http://diveintopython.org">http://diveintopython.org</a> &#8211; comprehensive Python reference</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">8 Reasons to Use Python</p>
<ol>
<li class="indent0">Lists, Tuples, and Dictionaries</li>
<li class="indent0">Intuitive Looping Techniques</li>
<li class="indent0">Text Processing and String Operations</li>
<li class="indent0">Exception Handling</li>
<li class="indent0">Sockets</li>
<li class="indent0">Tons of Standard Libraries</li>
<li class="indent0">Working with Files</li>
<li class="indent0">Simple HTTP/Web Services</li>
</ol></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">1. Lists, Tuples, Dictionaries</p>
<ul>
<li class="indent0"><b>Lists</b> &#8211; basically arrays (actually linked lists), can contain multiple data types, accessible by index, can be used as a stack or queue, can be sliced/concatenated</li>
<li class="indent1 code">append, insert, remove, pop, count, sort, reverse</li>
<li class="indent0"><b>Tuple</b> &#8211; statically defined, can be accessed by index but cannot be modified after instantiation, can be used as dictionary keys</li>
<li class="indent0"><b>Dictionary</b> &#8211; accessed by key, value pairs, constant lookup time, unordered in memory</li>
<li class="indent1 code">keys, items, del</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">2. Intuitive Looping Techniques</p>
<ul>
<li class="indent0 code">for i in range(10):</li>
<li class="indent0 code">for name in ['karl', 'lenny', 'barney', 'moe']:</li>
<li class="indent0 code">for i, j in enumerate(myList):<br />
    print i, j</li>
<li class="indent0 code">for k, v in myDictionary.iteritems():<br />
    print k + &#8220;&#8216;s value is&#8221;, v</li>
<li class="indent0 code">questions = ['who', 'what', 'when', 'where']<br />
answers = ['Mr. Green', 'revolver', '11:00', 'Lounge']<br />
for q, a in zip(questions, answers):<br />
    print q + &#8220;:&#8221;, a</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">3. Text Processing and String Operations</p>
<ul>
<li class="indent0">Strings can be indexed like C, but substrings can also be specified with slice notation</li>
<li class="indent1 code">s[0], s[:2], s[4:], s[-3:],&#8230;</li>
<li class="indent0">Standard string library provides dozens of useful functions</li>
<li class="indent1 code">split, join, lower, upper, replace, title, isalnum, isdigit,<br />
whitespace, punctuation, printable, etc., etc.</li>
<li class="indent0">Very easy to implement &#8220;filter-type&#8221; programs like grep</li>
<li class="indent1 code">input = sys.stdin.readlines()</li>
<li class="indent1">makes parsing formatted text files trivial, e.g. Apache log parsing</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">4. Exception Handling</p>
<p>Very easy to catch exceptions and run separate code for error handling routines</p>
<p class="code">try:<br />
    something = that_could + produce_an_exception<br />
except:<br />
    this_code = gets_run( only_if_exception_thrown )</p>
</p></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">5. Simple Sockets</p>
<ul>
<li class="indent0">Very simple to implement TCP/IP sockets</li>
<li class="indent1">Easy to add networking support to your Python scripts</li>
<li class="indent1">Inexpensive way to communicate between programs, even locally</li>
<li class="indent2 code"><b><a href="/files/server.txt">Server</a>:</b></li>
<li class="indent3 code">1. bind</li>
<li class="indent3 code">2. listen</li>
<li class="indent3 code">3. accept</li>
<li class="indent3 code">4. recv/send</li>
<li class="indent2 code"><b><a href="/files/client.txt">Client</a>:</b></li>
<li class="indent3 code">1. connect</li>
<li class="indent3 code">2. send/recv</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">6. Tons of Standard Libraries</p>
<ul>
<li class="indent0">Python ships with over 300 standard libraries for programmers to use:</li>
<li class="indent1 code">>>> help()<br />
help> modules</li>
<li class="indent0">Just to name a few:</li>
<li class="indent1 code">calendar, Cookie, cookielib, datetime, distutils, htmllib,<br />
HTMLParser, httplib, math, md5, optparse, os, random, re,<br />
smtplib, socket, string, struct, sys, time, urllib, urlparse,<br />
xml, xmllib</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">7. Working with Files</p>
<ul>
<li class="indent0">Reading and writing files is very simple in Python</li>
<li class="indent1 code">open(), read(), write(), close()</li>
<li class="indent0"><span class="code">os</span> library provides many useful function calls for filesystem access</li>
<li class="indent1 code">chdir, chmod, execv, getcwd, listdir, mkdir, remove, rmdir,<br />
rename, walk</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">8. Simple HTTP / Web Services</p>
<ul>
<li class="indent0">Almost everything provided to the programmer by httplib and other HTTP/URL libraries</li>
<li class="indent1 code"><b>httplib.HTTPConnection</b></li>
<li class="indent2 code">request</li>
<li class="indent2 code">getresponse</li>
<li class="indent2 code">getheaders</li>
<li class="indent2 code">status</li>
<li class="indent2 code">reason</li>
<li class="indent2 code">read</li>
<li class="indent2 code">close</li>
</ul></div>
<p>            <!-- end Slide --><br />
            <!-- begin Slide --></p>
<div class="middleBlock">
<p class="title">Putting it all Together</p>
<ul>
<li class="indent0">Easy to code &rArr; rapid prototyping of new functions/programs</li>
<li class="indent0">Easy to read &rArr; whitespace dependence leads to highly readable and reusable code</li>
<li class="indent0">Let&#8217;s write a couple of Python scripts</li>
<li class="indent1 code"><a href="/files/factorial.txt">factorial.py</a></li>
<li class="indent1 code"><a href="/files/primefactors.txt">primefactors.py</a></li>
<li class="indent1 code"><a href="/files/feedMonitor.txt">feedMonitor.py</a></li>
</ul></div>
<p>            <!-- end Slide --></p>
<p>I am definitely open to discussion on this topic as it&#8217;s one that&#8217;s close to my heart.  If you&#8217;re a Perl advocate or would otherwise like to make a case against the use of Python, I strongly encourage you to <a href="/contact.php">send me an email</a>.  I am more than happy to try to defend it <img src='http://brandon.sternefamily.net/posts/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2006/11/8-reasons-why-python-rocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RSS Feed Monitor &#8211; Update</title>
		<link>http://brandon.sternefamily.net/posts/2006/09/rss-feed-monitor-update/</link>
		<comments>http://brandon.sternefamily.net/posts/2006/09/rss-feed-monitor-update/#comments</comments>
		<pubDate>Sun, 24 Sep 2006 18:59:11 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=54</guid>
		<description><![CDATA[I updated the RSS Feeds Monitor I wrote in Python last month to enable database logging as well as a debug mode which prints output to the console. Both the new code and the old code are available.]]></description>
			<content:encoded><![CDATA[<p>I updated the <a href="/posts/2006/08/rss-feed-monitor/">RSS Feeds Monitor</a> I wrote in Python last month to enable database logging as well as a debug mode which prints output to the console.  Both the <a href="/files/feedMonitor.txt" title="Python RSS Feed Monitor">new code</a> and the <a href="/files/feedMonitor_old.txt">old code</a> are available.</p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2006/09/rss-feed-monitor-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSS Feed Monitor</title>
		<link>http://brandon.sternefamily.net/posts/2006/08/rss-feed-monitor/</link>
		<comments>http://brandon.sternefamily.net/posts/2006/08/rss-feed-monitor/#comments</comments>
		<pubDate>Wed, 23 Aug 2006 02:23:37 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=51</guid>
		<description><![CDATA[I wrote a Python <a href="/files/feedMonitor.txt">script</a> that <a href="/posts/2006/08/rss-feed-monitor/">monitors RSS feeds</a> for specified keywords and then sends emails to notify you of new posts.]]></description>
			<content:encoded><![CDATA[<div class="middleBlock">
<p><b>24-Sep-2006: Update</b></p>
<p>I&#8217;ve added <a href="/files/feedMonitor.txt">code</a> that logs each alert to a local database so that 1) duplicate alerts aren&#8217;t sent, even if the the stories are posted days or weeks apart, and 2) you can easily check which alerts have been sent historically.</p>
<p>Feel free to download the <a href="/files/feedMonitor.txt">new code</a> or the <a href="/files/feedMonitor_old.txt">old code</a> and <a href="/contact.php">tell me what you think</a>.</p>
<p>Additionally I added a debug mode such that if you run</p>
<p><code>> ./feedMonitor.py --console</code></p>
<p>the script prints to the console only and doesn&#8217;t send the email or log to the DB, so that you can see what types of alerts might be sent with the present script options.</p>
<p>Lastly, a <a href="/files/alertlist.txt">simple PHP script</a> can be used to display the alerts which have been sent in a <a href="/files/alertlist.php">HTML table that looks like this</a>.</p>
</div>
<p>I recently wrote a <a href="/files/feedMonitor.txt">Python script which monitors RSS feeds</a> for user-specified keywords.  The need for this script arose from the large number of security websites and mailing lists I monitor for work-related posts.  And you know how big a fan of automation I am&#8230;</p>
<p>All you need to run this script is <a href="http://python.org">Python</a> and a <a href="http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol">SMTP</a> server to send messages.  If you know of an <a href="http://en.wikipedia.org/wiki/Open_relay">open SMTP relay</a> feel free to use that, but you may find it easier to simply install <a href="http://www.sendmail.org/">Sendmail</a> locally.  The script is configured by default to send messages to <code>localhost</code>, so if you&#8217;re going to go the open relay route, you&#8217;ll need to configure the script accordingly.</p>
<p>For now, it is probably best to set the script up to run as a <a href="http://en.wikipedia.org/wiki/Crontab">cron job</a> and have the script run automatically every 10-30 minutes or so, depending on how urgently you need the alerts to come.  Eventually, I will add to the script the ability to log which alerts were mailed in a SQL database so that 1) you don&#8217;t receive duplicate alerts, and 2) you can have a record of which alerts have been mailed historically.</p>
<p>Stay tuned for updates to the script, and feel free to <a href="/contact.php">contact me</a> if you have any questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2006/08/rss-feed-monitor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyRSA &#8211; RSA in Python</title>
		<link>http://brandon.sternefamily.net/posts/2005/06/pyrsa-rsa-in-python/</link>
		<comments>http://brandon.sternefamily.net/posts/2005/06/pyrsa-rsa-in-python/#comments</comments>
		<pubDate>Sat, 18 Jun 2005 18:27:09 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=31</guid>
		<description><![CDATA[I implemented the public key cryptosystem <a href="/posts/2005/06/pyrsa-rsa-in-python/">RSA in Python</a>.  Check out the article I wrote about <a href="/posts/2005/05/rsa-algorithm/">RSA</a> or download the <a href="/files/pyrsa.txt">source code</a>.]]></description>
			<content:encoded><![CDATA[<p>PyRSA is a command line utility that allows users to digitally encrypt and sign messages using the public key encryption scheme, <a href="/posts/2005/05/rsa-algorithm/">RSA</a>. There are three basic functions that PyRSA performs: encryption, decryption, and key generation.</p>
<p><strong>Downloads:</strong></p>
<p>Source: <a href="/files/pyrsa.txt">pyrsa.py</a></p>
<p><strong>Sample Use:</strong></p>
<p>1. Generate a public and private key. In this example, we will specify a key of length 1024 bits. Allow several seconds of CPU time for the generation of the keys.</p>
<pre class="brush: plain; gutter: false; light: true">pyrsa.py -g 1024 Enter fil&#101; identifier (i.e. first name): brandon</pre>
<p>2. Now the files
<pre class="brush: bash; gutter: false">brandon_privateKey.txt</pre>
<p> and
<pre class="brush: bash; gutter: false">brandon_publicKey.txt</pre>
<p> are in the current directory. Next place the text we want to encrypt in a text file.</p>
<pre class="brush: bash; gutter: false">echo "The sky above the port was the color of television, tuned
to a dead channel." > message.txt</pre>
<p>3. Encrypt the message using the public key and redirect the output to a text file.</p>
<pre class="brush: bash; gutter: false">pyrsa.py -e message.txt -k brandon_publicKey.txt > ciphertext.txt</pre>
<p>4. At this point the file ciphertext.txt contains the encrypted message. The file can safely be sent to a recipient, i.e. as an email attachment, the contents utterly unreadable to anyone without the private key.</p>
<pre class="brush: bash; gutter: false">cat ciphertext.txt 32464047998704731086703458860763720628883125201
840735448292781611869424600546740055592235111171870058664751326891
416030992911165222195048303846516331939189036032662981573683210672
785053735077400433222553780571914729993485153779710689497701348386
214277988780913721453283666357504772556433129612632786845350983</pre>
<p>5. Next we will assume the message has been sent to the individual who possesses the corresponding private key and he wants to decrypt the message.</p>
<pre class="brush: bash; gutter: false">pyrsa.py -d ciphertext.txt -k brandon_privateKey.txt
Decrypted text:
The sky above the port was the color of television, tuned to a dead channel.</pre>
<p><script type="text/javascript" src="/posts/syntax/src/shCore.js"></script><br />
<script type="text/javascript" src="/posts/syntax/scripts/shBrushBash.js"></script><br />
<script type="text/javascript" src="/posts/syntax/scripts/shBrushPlain.js"></script></p>
<link rel="stylesheet" type="text/css" href="/posts/syntax/styles/shCore.css" />
<link type="text/css" rel="Stylesheet" href="/posts/syntax/styles/shThemeDefault.css"/>
<script type="text/javascript">SyntaxHighlighter.all()</script></p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2005/06/pyrsa-rsa-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSA Algorithm</title>
		<link>http://brandon.sternefamily.net/posts/2005/05/rsa-algorithm/</link>
		<comments>http://brandon.sternefamily.net/posts/2005/05/rsa-algorithm/#comments</comments>
		<pubDate>Thu, 05 May 2005 13:50:42 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=28</guid>
		<description><![CDATA[I wrote an article on <a href="/posts/2005/05/rsa-algorithm/">RSA</a>, a popular public key encryption scheme.  Eventually, I am going to implement <a href="/posts/2005/06/pyrsa-rsa-in-python/">RSA in Python</a>.]]></description>
			<content:encoded><![CDATA[<h3 class="center">Overview</h3>
<p>We have spent the last several weeks learning about encryption in my <a href="http://www.cs.ucr.edu/cs165">computer security class</a> so I thought I&#8217;d share what I&#8217;ve learned on public key cryptography.</p>
<p>There is a very good description of RSA on <a href="http://en.wikipedia.org/wiki/RSA">Wikipedia</a>, so I don&#8217;t want to simply restate what they have. The focus here will be the generation of public and private keys as I feel many of the RSA tutorials on the web are lacking a bit in that department. Computing the multiplicative inverse to get <em>d</em> from <em>e</em> is a little tricky, but we will walk through it step-by-step.</p>
<p>First, a brief overview of RSA, for those not familiar with it already. A message <em>M</em> is encrypted by raising it to the power of <em>e</em> and then taking the result modulo some number <em>N</em>. To decrypt the message, you simply raise the value of the encrypted message <em>C</em> to the power of <em>d</em> and again mod by <em>N</em>. The beauty of RSA is that <em>e</em> and <em>N</em> can be published publicly. Together they, in fact, comprise the <strong>public key</strong>. The <strong>private key</strong>, which is not be published, is comprised of <em>d</em> and <em>N</em>.</p>
<p><span style="font-family:monospace">C = M<sup>e</sup> mod N <br/>M = C<sup>d</sup> mod N</span></p>
<p>If you&#8217;re like me, then you are astonished at 1) how simple this system is, and 2) that you can exponentiate messages twice (modulo some number) and leave the original message unaltered. The main question that my skeptical mind came up with when presented with this powerful encryption tool was, &#8220;wouldn&#8217;t it be easy to compute <em>d</em> if you have the values of <em>e</em> and <em>N</em>?&#8221; The answer is, of course, no. It turns out that it is very hard to do so. We shall see later that it is easy to compute <em>d</em> only when we have the factors of <em>N</em>. If we choose <em>N</em> to be arbitrarily large, factoring <em>N</em> can take an arbitrarily long period of time. Currently, there are no known polynomial-time algorithms which can perform this task. Factorization has, in fact, been shown to be in the set of problems known as <a href="http://en.wikipedia.org/wiki/NP_%28complexity%29">NP</a>. So the security of RSA is essentially provided by the hardness of the factorization problem. If someone figures out a way to factor large numbers fast, then RSA is out of business.</p>
<h3 class="center">Key Generation</h3>
<p>As was mentioned above, RSA&#8217;s security is rooted in the fact that <em>N</em> is hard to factor. Therefore, we should choose <em>N</em> to be the product of two large primes, <em>p</em> and <em>q</em>. For clarity in this example, we will choose relatively small values for <em>p</em> and <em>q</em>, but later we will discuss the proper choices for these coefficients given a desired level of security.</p>
<ol>
<li>For this example, let <em>P</em> = 647 and <em>Q</em> = 1871. This means that the modulus, <em>N</em> = 1210537. (Incidentally, factoring this value of <em>N</em> took 0.056 seconds on UCR&#8217;s mainframe).</li>
<li>Compute the <a href="http://en.wikipedia.org/wiki/Euler%27s_totient_function">totient</a> of <em>N</em>, &#966;(<em>N</em>) = (<em>P</em> &#8211; 1)(<em>Q</em> &#8211; 1) = 1208020.</li>
<li>Now we choose a number <em>e</em> which should be <a href="http://en.wikipedia.org/wiki/Coprime">coprime</a> to &#966;(<em>N</em>). The easiest way to do this is to simply choose a prime number. For this example, let <em>e</em> = 1127.</li>
<li>The next step is to compute <em>d</em> such that (<em>d * e</em>) mod &#966;(<em>N</em>) = 1. If this is confusing, that is okay. This property is important because it ensures that (<em>M</em><sup>e</sup>)<sup>d</sup> (mod <em>n</em>) = <em>M</em>. It may help to have a look at <a href="http://en.wikipedia.org/wiki/Euler%27s_theorem">Euler&#8217;s Theorem</a> if you are still confused.</li>
</ol>
<p>The best way to compute the multiplicative inverse, <em>d</em> from <em>e</em> and &#966;(<em>N</em>) is to use the <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">Extended Euclidean Algorithm</a>. Here is Euclid&#8217;s algorithm for our example:</p>
<table cellspacing="20">
<tr>
<td>1127</td>
<td>1208020</td>
<td>(1, 0)</td>
<td>(0, 1)</td>
<td rowspan="8" valign="top">We start with unit vectors (1, 0) and (0, 1) which correspond to the values of <em>e</em> and &#966;(<em>N</em>), respectively.<br/><br/>For each operation we perform on the left two columns, we perform the same operation on the right two columns.<br/><br/><br />
For example, in the first step, 1127 divides 1208020 1071 times and leaves a remainder of 1003. The corresponding operation in columns 3 and 4 is to subtract (1, 0) from (0, 1) 1071 times yielding (-1071, 1).<br/><br/><br />
The algorithm terminates when we have 1 and 0, not necessarilly in that order, in the first two columns. The value for <em>d</em> is in the column that corresponds to the 1 in the first two columns.<br/><br/><br />
<strong>*Note</strong>: it is worth mentioning that it is possible for the extended Euclidean algorithm to yield a negative result for <em>d</em>. Obviously, this is not a suitable decryption exponent because raising an integer to a negative number results in a fraction. The simple fix here is to mod the negative value of <em>d</em> by &#966;(<em>N</em>), giving us a positive value of <em>d</em> between 0 and &#966;(<em>N</em>).</td>
</tr>
<tr>
<td>1127</td>
<td>1003</td>
<td>(1, 0)</td>
<td>(-1071, 1)</td>
</tr>
<tr>
<td>124</td>
<td>1003</td>
<td>(1072, -1)</td>
<td>(-1071, 1)</td>
</tr>
<tr>
<td>124</td>
<td>11</td>
<td>(1072, -1)</td>
<td>(-9647, 9)</td>
</tr>
<tr>
<td>3</td>
<td>11</td>
<td>(107189, -100)</td>
<td>(-9647, 9)</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>(107189, -100)</td>
<td>(-331214, 309)</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>(438403, -409)</td>
<td>(-331214, 309)</td>
</tr>
<tr>
<td>1</td>
<td>0</td>
<td>(438403, -409)</td>
<td>(-1208020, 1127)</td>
</tr>
</table>
<p>From the above calculations we know that <em>d</em> = 438403. So we have both the public and private keys for this user:</p>
<pre>public key = (1127, 1210537)
private key = (438403, 1210537)</pre>
<p>To prove that this system works, observe the following computations. Let our message <em>M</em> = 247. The first step is to compute C = 247<sup>1127</sup> mod 1210537.</p>
<p><em>A brief aside:<br/></em>This exponentiation can be computed easily because we are using relatively small values for <em>e</em> and <em>d</em>. However, real world implementations of RSA often use 1024 bit encryption, meaning the exponent is 1024 bits long. That is roughly equivalent to a 300 decimal digit number. To compute an exponent of that order of magnitude in the conventional way, multiplying the base by itself <em>e</em> times would be prohibitively expensive. Even if we could compute 1 billion multiplications per second, the computation would take longer than the current <a href="http://en.wikipedia.org/wiki/Age_of_the_Universe">age of the universe</a>. So it is useful to use an alternative method like <a href="http://en.wikipedia.org/wiki/Exponentiation_by_squaring">exponentiation by squaring</a>. Here is a <a title="Python Exponentiation by Squaring" href="/files/power.txt">script</a> that computes large exponents fast. Another consideration is the storage of a very large number such as <em>C<sup>d</sup></em>. Rather than keeping the value in main memory as we exponentiate, we can simply keep the value modulo <em>N</em>. And now back to our example&#8230;</p>
<p>247<sup>1127</sup> mod 1210537 = 611545. This number was easily obtained with the Python interpreter in a fraction of a second. Raising this number, however to the value of <em>d</em>, 438403, should not be done the conventional way. On the school&#8217;s mainframe this calculation took 11 minutes, 23.65 seconds. This is a situation where we can see the power of divide-and-conquer algorithms. Using our recursive exponentiation function we show that 611545<sup>438403</sup> mod 1210537 = 247. Voil&aacute;, out pops our original message. Additionally, the exponentiation took only 31.16 seconds on the same machine with the repeated squaring method. This can be vastly improved, too, once we develop a non-recursive function. That will be critical when we want to provide real security via RSA and we don&#8217;t want to wait 10 minutes to decrypt the message.</p>
<p><em><a href="/posts/2005/06/pyrsa-rsa-in-python/">PyRSA</a> now available.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2005/05/rsa-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nearest Neighbor Classifier</title>
		<link>http://brandon.sternefamily.net/posts/2005/03/nearest-neighbor-classifier/</link>
		<comments>http://brandon.sternefamily.net/posts/2005/03/nearest-neighbor-classifier/#comments</comments>
		<pubDate>Thu, 24 Mar 2005 05:10:30 +0000</pubDate>
		<dc:creator>bsterne</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://brandon.sternefamily.net/posts/?p=21</guid>
		<description><![CDATA[I implemented the <a href="/posts/2005/03/nearest-neighbor-classifier/">Nearest Neighbor Algorithm</a> in Python.  Nearest Neighbor has application in intelligent systems and is used to classify objects based on selected criteria.  Here's the <a href="/files/nearestneighbor.txt">source</a> if you're interested.]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction:</strong><br/></p>
<p>The Nearest Neighbor Classifier, while robust and capable of handling streaming data, is sensitive to outlying data points and to irrelevant features. One critical part of designing a good nearest neighbor classifier is deciding which feature set to use in classifying new data points.  One great way to choose the correct subset is through search. Exhaustive search isn&#8217;t realistic, though, for data sets with large numbers of features as the number of possible subsets of features is exponential: <em>n</em> = 2^<em>F</em> where <em>F</em> is the number of features in the data.</p>
<p>The purpose of this program is to search through the space of possible subsets of features in a faster way, that is, polynomial or better, without sacrificing too much accuracy in the classification. The first two methods we use are fairly straightforward: Forward Selection, and Backward Elimination. The former method begins with the empty set of features and adds one feature at a time while the latter method begins with all the features and removes one feature at a time.</p>
<p>The third, original, method to search for a good subset of features requires some explanation.  By relaxing our criteria for what constitutes the &#8220;nearest neighbor&#8221;, we are able to avoid some of the calculations that make searching this space expensive. In other words, we sacrifice some accuracy of the classifier in order to gain a great deal of speed in computing the subset. The algorithm works as follows:</p>
<ol>
<li>All of the data is normalized so that every feature&#8217;s value falls between 0 and 1.</li>
<li>The user is prompted to enter a value I call &#8220;delta&#8221; to be used during the nearest-neighbor computation. To be accurate, it is no longer the &#8220;nearest&#8221; neighbor in the set that we are interested in, only a &#8220;pretty good&#8221; one. So the modified nearest neighbor selector returns the first data point that falls within delta units distance from the given point.</li>
<li>I run the Forward Selection Search using the modified nearest neighbor algorithm to return a &#8220;pretty good&#8221; subset of features.</li>
</ol>
<p>Here I&#8217;ve omitted a sample trace of the program because it is lengthy, but you could <a href="/files/nearestneighbor.txt" title="Nearest Neighbor in Python">download the source</a> and run it in the <a href="http://www.python.org">Python Interpreter</a> just as easiliy.</p>
<p>Running these three algorithms on various data sets yielded the following statistics:</p>
<table cellpadding="3">
<tr>
<td colspan="4"><b><a href="/files/cs170_NN_big_test_53.txt">Large Data Set</a> &#8211; 1000 points, 30 Features</b></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Best Set</td>
<td>Acc. %</td>
<td>Time (s)</td>
</tr>
<tr>
<td>Forward</td>
<td>{7, 9, 12}</td>
<td>87.40</td>
<td>14255.89</td>
</tr>
<tr>
<td>Backward</td>
<td>{7, 9, 17, 25}</td>
<td>93.00</td>
<td>20507.24</td>
</tr>
<tr>
<td>Special( 1 )</td>
<td>{0, 1, 4, 7, 9, 10, 14, 15, 16, 17, 18, 19, 20, 27, 28}</td>
<td>71.60</td>
<td>4909.35</td>
</tr>
<tr>
<td>Special( 2 )</td>
<td>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28}</td>
<td>69.60</td>
<td>647.04</td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td colspan="4"><b><a href="/files/cs170_NN_small_test_53.txt">Small Data Set</a> &#8211; 600 points, 16 Features</b></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Best Set</td>
<td>Acc. %</td>
<td>Time (s)</td>
</tr>
<tr>
<td>Forward</td>
<td>{2, 4, 9}</td>
<td>89.66</td>
<td>1120.73</td>
</tr>
<tr>
<td>Backward</td>
<td>{2, 4}</td>
<td>88.17</td>
<td>1519.89</td>
</tr>
<tr>
<td>Special(.25)</td>
<td>{2, 4, 5, 11, 12, 13, 14}</td>
<td>81.66</td>
<td>449.00</td>
</tr>
<tr>
<td>Special( .5 )</td>
<td>{1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14}</td>
<td>78.83</td>
<td>221.63</td>
</tr>
<tr>
<td>Special( 1 )</td>
<td>{0, 1, 2, 4, 5, 6, 7, 9, 10, 13, 14}</td>
<td>73.66</td>
<td>47.09</td>
</tr>
<tr>
<td>Special( 2 )</td>
<td>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}</td>
<td>75.33</td>
<td>8.27</td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td colspan="4"><b><a href="/files/cs170_NN_small_test_special.txt">Small Special Set</a> &#8211; 1000 points, 19 Features</b></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Best Set</td>
<td>Acc. %</td>
<td>Time (s)</td>
</tr>
<tr>
<td>Forward</td>
<td>{0, 1, 2, 3}</td>
<td>86.60</td>
<td>2205.22</td>
</tr>
<tr>
<td>Backward</td>
<td>{0, 1, 2, 3}</td>
<td>86.60</td>
<td>3806.77</td>
</tr>
<tr>
<td>Special(.25)</td>
<td>{0, 1, 2, 3, 5, 7, 13, 14, 15}</td>
<td>67.20</td>
<td>1025.89</td>
</tr>
<tr>
<td>Special( .5 )</td>
<td>{0, 1, 4, 8, 12, 14, 16}</td>
<td>61.10</td>
<td>590.12</td>
</tr>
<tr>
<td>Special( 1 )</td>
<td>{0, 1, 2, 3, 4, 7, 8, 10, 11, 12, 14, 15, 16, 17}</td>
<td>61.10</td>
<td>128.71</td>
</tr>
<tr>
<td>Special( 2 )</td>
<td>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 14, 15, 16, 17, 18}</td>
<td>60.00</td>
<td>14.13</td>
</tr>
</table>
<p><br/><br />
<strong>Analysis:</strong><br/></p>
<p>It is worth noting that running Special Search with delta = 0 is equivalent to running Forward Selection. As delta increases beyond a certain threshold (data set dependent), Special Search degenerates rapidly. This is because for every data point in the set, we will simply choose the next point we look at as the nearest neighbor. With two classes of points, this method will essentially yield 50% accuracy. Hence, it is possible to wind up with a feature set that is less accurate than &#8220;leave-one-out&#8221; evaluation with all the features.</p>
<p>Therefore, the selection of delta is important to achieve good performance in Special Search. There are some things we can say, quantitatively, about the selection of delta. First, it must by definition fall between 0 and F where F is the number of features in the data set. The lower bound is set because two points cannot be closer than 0 units in Euclidean space. The upper bound comes from the fact that when the data are normalized to values between 0 and 1, each point can differ by no more than 1 unit per feature (the reason it is not is because the distance function I am using doesn&#8217;t compute the square root part to save time).</p>
<p>It is apparent that the &#8220;proper&#8221; choice for delta will vary greatly according to the data set. If the data had great separation a large delta value would suffice and lead to faster computing time. Conversely, a data set with tightly grouped instances of opposite classes will require a smaller delta value to preserve a high degree of accuracy.</p>
<p>It is possible to do some simple preprocessing of the data before we begin the search in order to give us some idea of the data points&#8217; separation. In fact, I used the following strategy to give me a ballpark figure for a reasonable delta value: First, I selected an arbitrary number of points at random from the set. For each point, I then recorded the class of that point, the distance to each other point in the set, and the class of each other point. I then sorted these data by class and ascending distance.</p>
<p>From this data, I got an idea for what value, on average, would allow us to correctly classify points without searching every point for the nearest neighbor. For the above example, a delta value of 1.75 would not sacrifice any accuracy because the closest point of opposite class was at a distance of 1.757 units. It must be mentioned though, that randomly sampling the data in the above way will not give us perfect data to use for the delta choice, only a subjective &#8220;feel&#8221; for the data&#8217;s separation.</p>
<p>It is up to the user to weigh the accuracy of the classifier versus the speed at which it classifies new points. Some data sets will be very conducive to Special Search while others will perform very poorly.</p>
]]></content:encoded>
			<wfw:commentRss>http://brandon.sternefamily.net/posts/2005/03/nearest-neighbor-classifier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

