Skip to main content

xxtea

Project description

XXTEA implemented as a Python extension module, licensed under 2-clause BSD.

The XXTEA algorithm takes a 128-bit key and operates on an array of 32-bit integers (at least 2 integers), but it doesn’t define the conversions between bytes and array. Due to this reason, many XXTEA implementations out there are not compatible with each other.

In this implementation, the conversions between bytes and array are taken care of by longs2bytes and bytes2longs. PKCS#7 padding is also used to make sure that the input bytes are padded to multiple of 4-byte (the size of a 32-bit integer) and at least 8-byte long (the size of two 32-bit integer, which is required by the XXTEA algorithm). As a result of these measures, you can encrypt not only texts, but also any binary bytes of any length.

Installation

$ pip install xxtea -U

Usage

This module provides four functions: encrypt(), decrypt(), encrypt_hex(), and decrypt_hex().

Python 2:

>>> import os
>>> import xxtea
>>>
>>> key = os.urandom(16)  # Key must be a 16-byte string.
>>> s = "xxtea is good"
>>>
>>> enc = xxtea.encrypt(s, key)
>>> dec = xxtea.decrypt(enc, key)
>>> s == dec
True
>>>
>>> hexenc = xxtea.encrypt_hex(s, key)
>>> hexenc
'd1d8e82461dd5828397c32ad265ee225'
>>> s == xxtea.decrypt_hex(hexenc, key)
True
>>>
>>> enc.encode('hex') == hexenc
True

Python 3:

>>> import os
>>> import xxtea
>>> import binascii
>>>
>>> key = os.urandom(16)  # Key must be a 16-byte string.
>>> s = b"xxtea is good"
>>>
>>> enc = xxtea.encrypt(s, key)
>>> dec = xxtea.decrypt(enc, key)
>>> s == dec
True
>>>
>>> hexenc = xxtea.encrypt_hex(s, key)
>>> hexenc
b'7ad85672d770fb5cf636c49d57e732ae'
>>> s == xxtea.decrypt_hex(hexenc, key)
True
>>>
>>> binascii.hexlify(enc) == hexenc

encrypt_hex() and decrypt_hex() operate on ciphertext in a hexadecimal representation. They are exactly equivalent to:

Python 2:

>>> hexenc = xxtea.encrypt(s, key).encode('hex')
>>> s == xxtea.decrypt(hexenc.decode('hex'), key)
True

Python 3:

>>> hexenc = binascii.hexlify(xxtea.encrypt(s, key))
>>> s == xxtea.decrypt(binascii.unhexlify(hexenc), key)
True

Catching Exceptions

It is possible to throw a ValueError or a TypeError during calling decrypt() and decrypt_hex(). Better to catch them, or your program would exit.

>>> from __future__ import print_function
>>> import xxtea
>>>
>>> def try_catch(func, *args, **kwargs):
...     try:
...         func(*args, **kwargs)
...     except Exception as e:
...         print(e.__class__.__name__, ':', e)
...
...
...
>>> try_catch(xxtea.decrypt, '', key='')
ValueError : Need a 16-byte key.
>>> try_catch(xxtea.decrypt, '', key=' '*16)
ValueError : Invalid data, data length is not a multiple of 4, or less than 8.
>>> try_catch(xxtea.decrypt, ' '*8, key=' '*16)
ValueError : Invalid data, illegal PKCS#7 padding. Could be using a wrong key.
>>> try_catch(xxtea.decrypt_hex, ' '*8, key=' '*16)
TypeError : Non-hexadecimal digit found
>>> try_catch(xxtea.decrypt_hex, 'abc', key=' '*16)
TypeError : Odd-length string
>>> try_catch(xxtea.decrypt_hex, 'abcd', key=' '*16)
ValueError : Invalid data, data length is not a multiple of 4, or less than 8.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

xxtea-1.0.1.zip (10.3 kB view details)

Uploaded Source

xxtea-1.0.1.tar.gz (6.2 kB view details)

Uploaded Source

xxtea-1.0.1.tar.bz2 (6.8 kB view details)

Uploaded Source

File details

Details for the file xxtea-1.0.1.zip.

File metadata

  • Download URL: xxtea-1.0.1.zip
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xxtea-1.0.1.zip
Algorithm Hash digest
SHA256 cf6b56420cb0fa9ef255232e0411cb38f92e96a67a6d656306fd83c9e8e2bcbe
MD5 2ded2e5abce10cdc5260c61a4df8c35d
BLAKE2b-256 cd2c62ad13fccffebef96dcdb959ff3b79026ebc36b705ec968f299a8c1cea92

See more details on using hashes here.

File details

Details for the file xxtea-1.0.1.tar.gz.

File metadata

  • Download URL: xxtea-1.0.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xxtea-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4a5e79978c01de343d34497db01f4143e2146f9dad0cbc0416e04489f2bc1de1
MD5 1ba9bc744e483d5a3b380cad4c8ed393
BLAKE2b-256 4ccca735c70356c236e613aaaf1d3e3f24d22bb17765ae556eb0f5d5404c98e0

See more details on using hashes here.

File details

Details for the file xxtea-1.0.1.tar.bz2.

File metadata

  • Download URL: xxtea-1.0.1.tar.bz2
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xxtea-1.0.1.tar.bz2
Algorithm Hash digest
SHA256 7bdf819c2cb5de63ae470c08ae7521867c9e00618ec4232096fc1c79ae1740ef
MD5 1f3fed145315845961fa4be279762a4e
BLAKE2b-256 4b9abf47d80da2965393166dd78e78ddf3789e6861f852be3b57f1bdf6db6bcd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page