Module xtea
XTEA Block Encryption Algorithm
Author: Paul Chakravarti (paul_dot_chakravarti_at_mac_dot_com)
License: Public Domain
Modified by Christian Heimes to add an endian switch.
This module provides a Python implementation of the XTEA block
encryption algorithm (http://www.cix.co.uk/~klockstone/xtea.pdf).
The module implements the basic XTEA block encryption algortithm
(`xtea_encrypt`/`xtea_decrypt`) and also provides a higher level `crypt`
function which symmetrically encrypts/decrypts a variable length string
using XTEA in OFB mode as a key generator. The `crypt` function does not
use `xtea_decrypt` which is provided for completeness only (but can be
used to support other stream modes - eg CBC/CFB).
This module is intended to provide a simple 'privacy-grade' Python
encryption algorithm with no external dependencies. The implementation is
relatively slow and is best suited to small volumes of data. Note that
the XTEA algorithm has not been subjected to extensive analysis (though
is believed to be relatively secure - see
http://en.wikipedia.org/wiki/XTEA). For applications requiring 'real'
security please use a known and well tested algorithm/implementation.
The security of the algorithm is entirely based on quality (entropy)
and secrecy of the key. You should generate the key from a known random
source and exchange using a trusted mechanism. In addition, you should
always use a random IV to seed the key generator (the IV is not sensitive
and does not need to be exchanged securely)
>>> import os
>>> iv = 'ABCDEFGH'
>>> z = crypt('0123456789012345','Hello There',iv)
>>> z.encode('hex')
'fe196d0a40d6c222b9eff3'
>>> crypt('0123456789012345',z,iv)
'Hello There'
|
crypt(key,
data,
iv='\00\00\00\00\00\00\00\00',
n=32,
endian="!")
Encrypt/decrypt variable length string using XTEA cypher as key
generator (OFB mode) * key = 128 bit (16 char) / iv = 64 bit (8 char)
* data = string (any length)
|
|
xtea_encrypt(key,
block,
n=32,
endian="!")
Encrypt 64 bit data block using XTEA block cypher * key = 128 bit
(16 char) / block = 64 bit (8 char)
|
|
xtea_decrypt(key,
block,
n=32,
endian="!")
Decrypt 64 bit data block using XTEA block cypher * key = 128 bit
(16 char) / block = 64 bit (8 char)
|
crypt(key,
data,
iv='\00\00\00\00\00\00\00\00',
n=32,
endian="!")
| |
Encrypt/decrypt variable length string using XTEA cypher as key
generator (OFB mode) * key = 128 bit (16 char) / iv = 64 bit (8 char) *
data = string (any length)
>>> import os
>>> key = os.urandom(16)
>>> iv = os.urandom(8)
>>> data = os.urandom(10000)
>>> z = crypt(key,data,iv)
>>> crypt(key,z,iv) == data
True
-
|
xtea_encrypt(key,
block,
n=32,
endian="!")
| |
Encrypt 64 bit data block using XTEA block cypher * key = 128 bit (16
char) / block = 64 bit (8 char)
>>> xtea_encrypt('0123456789012345','ABCDEFGH').encode('hex')
'b67c01662ff6964a'
-
|
xtea_decrypt(key,
block,
n=32,
endian="!")
| |
Decrypt 64 bit data block using XTEA block cypher * key = 128 bit (16
char) / block = 64 bit (8 char)
>>> xtea_decrypt('0123456789012345','b67c01662ff6964a'.decode('hex'))
'ABCDEFGH'
-
|