Package: xxtea
XXTEAimplemented as a Python extension module, licensed under 2-clause BSD.TheXXTEAalgorithm 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 bylongs2bytesandbytes2longs.PKCS#7padding 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 theXXTEAalgorithm). As a result of these measures,
you can encrypt not only texts, but also any binary bytes of any length.Installation$ pip install xxtea -UUsageThis module provides four functions:encrypt(),decrypt(),encrypt_hex(), anddecrypt_hex().>>>importos>>>importxxtea>>>importbinascii>>>>>>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==decTrue>>>>>>hexenc=xxtea.encrypt_hex(s,key)>>>hexencb'7ad85672d770fb5cf636c49d57e732ae'>>>s==xxtea.decrypt_hex(hexenc,key)True>>>>>>binascii.hexlify(enc)==hexencTrueencrypt_hex()anddecrypt_hex()operate on ciphertext in a hexadecimal
representation. They are exactly equivalent to:>>>hexenc=binascii.hexlify(xxtea.encrypt(s,key))>>>s==xxtea.decrypt(binascii.unhexlify(hexenc),key)TruePaddingPadding is enabled by default, in this case you can encode any bytes of any length.>>>xxtea.encrypt_hex('',key)b'd63256eb59134f1f'>>>xxtea.decrypt_hex(_,key)b''>>>xxtea.encrypt_hex(' ',key)b'97009bd24074a7a5'>>>xxtea.decrypt_hex(_,key)b' 'You can disable padding by setting padding parameter toFalse.
In this case data will not be padded, so data length must be a multiple of 4 bytes and must not be less than 8 bytes.
OtherwiseValueErrorwill be raised:>>>xxtea.encrypt_hex('',key,padding=False)ValueError:Datalengthmustbeamultipleof4bytesandmustnotbelessthan8bytes>>>xxtea.encrypt_hex('xxtea is good',key,padding=False)ValueError:Datalengthmustbeamultipleof4bytesandmustnotbelessthan8bytes>>>xxtea.encrypt_hex('12345678',key,padding=False)b'64f4e969ba90d386'>>>xxtea.decrypt_hex(_,key,padding=False)b'12345678'RoundsBy default xxtea manipulates the input data for6 + 52 / nrounds,
where n denotes how many 32-bit integers the input data can fit in.
We can change this by settingroundsparameter.Do note that the more rounds it is, the more time will be consumed.>>>importxxtea>>>importstring>>>data=string.digits>>>key=string.ascii_letters[:16]>>>xxtea.encrypt_hex(data,key)b'5b80b08a5d1923e4cd992dd5'>>>6+52//((len(data)+(4-1))//4)# 4 means 4 bytes, size of a 32-bit integer23>>>xxtea.encrypt_hex(data,key,rounds=23)b'5b80b08a5d1923e4cd992dd5'>>>xxtea.encrypt_hex(data,key,rounds=1024)b'1577bbf28c43ced93bd50720'Catching ExceptionsWhen callingdecrypt()anddecrypt_hex(), it is possible that aValueErroror aTypeErroris raised:>>>from__future__importprint_function>>>importxxtea>>>>>>deftry_catch(func,*args,**kwargs):...try:...func(*args,**kwargs)...exceptExceptionase:...print(e.__class__.__name__,':',e).........>>>try_catch(xxtea.decrypt,'',key='')ValueError:Needa16-bytekey.>>>try_catch(xxtea.decrypt,'',key=' '*16)ValueError:Invaliddata,datalengthisnotamultipleof4,orlessthan8.>>>try_catch(xxtea.decrypt,' '*8,key=' '*16)ValueError:Invaliddata,illegalPKCS#7 padding. Could be using a wrong key.>>>try_catch(xxtea.decrypt_hex,' '*8,key=' '*16)TypeError:Non-hexadecimaldigitfound>>>try_catch(xxtea.decrypt_hex,'abc',key=' '*16)TypeError:Odd-lengthstring>>>try_catch(xxtea.decrypt_hex,'abcd',key=' '*16)ValueError:Invaliddata,datalengthisnotamultipleof4,orlessthan8.