## Python and building xml’s

I’m working on a little Project called “leMensa”. Maybe later I’ll tell you more about it.
While working on it I needed to build some xml documents with pyxml.
Here’s what I found. A little help from Matt Croydon was all I needed to know.

http://www.postneo.com/projects/pyxml/

## fast calculation of fibonacci numbers in python

I needed this for a challenge. If you need to reuse the values, it’s much faster with the memo.

```#!/usr/bin/python
memo = {0:0, 1:1}
def fib(n):
if not n in memo:
memo[n] = fib(n-1) + fib(n-2)
return memo[n]
print fib(100)
```
```\$./fib.py
354224848179261915075
```

Here’s are 2 little scripts I wrote today for encoding/decoding XOR encrypted text.

Script 1 (HexXorEncode.py) takes a string/text and a integer key value. Then it preforms and xor encryption on the string with the given key.

```#!/usr/bin/python
import sys
#Copyleft m.puchalla 2010
#Preforms a XOR Encoding with a specific string and returns a hexadecimal representation of it

if(len(sys.argv)!=3):
print "Usage:",sys.argv," [String] [integer key]"

def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and  "0" ) or ( baseN(num // b, b).lstrip("0") + numerals[num % b])

s=sys.argv
key=baseN(int(sys.argv),2)
sol=""
for i in range(0,len(s)):
sol=sol+baseN(int(str(baseN(ord(s[i]),2)),2)^int(key,2),16)
print sol
```

Example:

```\$ ./HexXorEncode.py "this is a little test" 25
6d71706a39706a39783975706d6d757c396d7c6a6d
```

Here’s script number 2 (HexXorDecode.py) which simply reverses the process.

```#!/usr/bin/python
import sys
#Copyleft m.puchalla 2010
#Decrypt a hex xor coded string with a key

if(len(sys.argv)<3):
print "Usage:",sys.argv," [XOR Code String] [integer key]"
sys.exit(1)

def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and  "0" ) or ( baseN(num // b, b).lstrip("0") + numerals[num % b])

s=sys.argv
key=baseN(int(sys.argv),2)
sol=""
for i in xrange(0,len(s)-1,2):
sol=sol+chr(int(str(baseN(int(s[i]+s[i+1],16),2)),2)^int(key,2))
print sol
```

Example:

```\$ ./HexXorDecode.py "6d71706a39706a39783975706d6d757c396d7c6a6d" 25
this is a little test
```

If you like it, use it.