Giter Club home page Giter Club logo

xxtea's Introduction

xxtea Github Actions Status Latest Version Supported Python versions License

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().

>>> 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
True

encrypt_hex() and decrypt_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)
True

Padding

Padding 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 to False. 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. Otherwise ValueError will be raised:

>>> xxtea.encrypt_hex('', key, padding=False)
ValueError: Data length must be a multiple of 4 bytes and must not be less than 8 bytes
>>> xxtea.encrypt_hex('xxtea is good', key, padding=False)
ValueError: Data length must be a multiple of 4 bytes and must not be less than 8 bytes
>>> xxtea.encrypt_hex('12345678', key, padding=False)
b'64f4e969ba90d386'
>>> xxtea.decrypt_hex(_, key, padding=False)
b'12345678'

Rounds

By default xxtea manipulates the input data for 6 + 52 / n rounds, where n denotes how many 32-bit integers the input data can fit in. We can change this by setting rounds parameter.

Do note that the more rounds it is, the more time will be consumed.

>>> import xxtea
>>> import string
>>> 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 integer
23
>>> xxtea.encrypt_hex(data, key, rounds=23)
b'5b80b08a5d1923e4cd992dd5'
>>> xxtea.encrypt_hex(data, key, rounds=1024)
b'1577bbf28c43ced93bd50720'

Catching Exceptions

When calling decrypt() and decrypt_hex(), it is possible that a ValueError or a TypeError is raised:

>>> 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.

xxtea's People

Contributors

black-sliver avatar cgohlke avatar ifduyue avatar msabramo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

xxtea's Issues

Let users specify rounds

Currently, rounds is calculated by rounds = 6 + 52 / n, where n is the length of the array of 32-bit integers converted from the input bytes.

Error installing xxtea on py 3.6+

Installing collected packages: xxtea
Running setup.py install for xxtea ... error
Exception:
Traceback (most recent call last):
File "c:\python37\lib\site-packages\pip\compat_init_.py", line 73, in console_to_str
return s.decode(sys.stdout.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 31: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\python37\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "c:\python37\lib\site-packages\pip\commands\install.py", line 342, in run
prefix=options.prefix_path,
File "c:\python37\lib\site-packages\pip\req\req_set.py", line 784, in install
**kwargs
File "c:\python37\lib\site-packages\pip\req\req_install.py", line 878, in install
spinner=spinner,
File "c:\python37\lib\site-packages\pip\utils_init_.py", line 676, in call_subprocess
line = console_to_str(proc.stdout.readline())
File "c:\python37\lib\site-packages\pip\compat_init_.py", line 75, in console_to_str
return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 31: invalid start byte

How to install?

Value Error: Invalid data

When I try to use this script on data I get the following error:

ValueError : Invalid data, illegal PKCS#7 padding. Could be using a wrong key.

What does the format need to be of the key and data coming into the xxtea.decrypt function? As of right now, the key is 16 bytes and the data is 256 bytes.

Thanks,
Chris

Compilation failure on Windows for ARM

Not my system, just forwarding an error log from a user:

Running setup.py install for xxtea ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\maximilianenders\AppData\Local\Programs\Python\Python39\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\maximilianenders\\AppData\\Local\\Temp\\pip-install-cl3w4g_b\\xxtea_5e4e2a25f420427b8d73a31f5687f4db\\setup.py'"'"'; __file__='"'"'C:\\Users\\maximilianenders\\AppData\\Local\\Temp\\pip-install-cl3w4g_b\\xxtea_5e4e2a25f420427b8d73a31f5687f4db\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\maximilianenders\AppData\Local\Temp\pip-record-1x9cieac\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\maximilianenders\AppData\Local\Programs\Python\Python39\Include\xxtea'
         cwd: C:\Users\maximilianenders\AppData\Local\Temp\pip-install-cl3w4g_b\xxtea_5e4e2a25f420427b8d73a31f5687f4db\
    Complete output (9 lines):
    running install
    running build
    running build_ext
    building 'xxtea' extension
    creating build
    creating build\temp.win-amd64-3.9
    creating build\temp.win-amd64-3.9\Release
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Users\maximilianenders\AppData\Local\Programs\Python\Python39\include -IC:\Users\maximilianenders\AppData\Local\Programs\Python\Python39\include -IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE -IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE -IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt /Tcxxtea.c /Fobuild\temp.win-amd64-3.9\Release\xxtea.obj
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit code 3221225595
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Users\maximilianenders\AppData\Local\Programs\Python\Python39\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\maximilianenders\\AppData\\Local\\Temp\\pip-install-cl3w4g_b\\xxtea_5e4e2a25f420427b8d73a31f5687f4db\\setup.py'"'"'; __file__='"'"'C:\\Users\\maximilianenders\\AppData\\Local\\Temp\\pip-install-cl3w4g_b\\xxtea_5e4e2a25f420427b8d73a31f5687f4db\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\maximilianenders\AppData\Local\Temp\pip-record-1x9cieac\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\maximilianenders\AppData\Local\Programs\Python\Python39\Include\xxtea' Check the logs for full command output.

Wrong padding

Hello!

Thanx for the good work!
There's a little issue : if the block is already a multiple of 8 bytes (2 32-bits integers) and the user don't pass the option padding=False then you pad its buffer with 8 additional bytes. Which results in a non compatible enc with the reference version.
Maybe you should check if inlen is multiple of 8, and if yes skip the padding? Or warn the user in the doc that by default there is padding even if its buffer is a multiple a 8 bytes?

Anyway thanx for the good work :-)

encrypt_hex should be encrypt, decrypt_hex should be decryt

With the C-based version of XXTEA, the methods "encrypt" and "decrypt" were replaced with "encrypt_hex" and "decrypt_hex", respectively. However, concerning future development, it would probably make a lot sense not to introduce new method names for the default behavior.

I'd suggest to rename the current "encrypt" into "encrypt_raw", and "encrypt_hex" back into "encrypt". The same with decrypt. This also maintains backwards compatibility and consistency concerning method names.

Massiv CPU load on heavy usage sites

Used on a Django site powered by NGINX/uWSGI with massive traffic (> 60.000 requests per second), the new C-based XXTEA version caused an extremely high server load. CPUs were constantly kept at nearly 100% load. By reverting to the non-C-version of XXTEA, the problem was solved.

Not being a C developer, I cannot debug the XXTEA code. But this is basically the error from our server log. I hope it helps tracking down the culprit:

!!! uWSGI process 16809 got Segmentation Fault !!!

  • backtrace of 16809
    uwsgi(uwsgi_backtrace+0x2e) [0x468a9e]
    uwsgi(uwsgi_segfault+0x21) [0x468e61]
    /lib/x86_64-linux-gnu/libc.so.6(+0x36d40) [0x7f8fc0fccd40]
    [...]

We kept getting lots of "segmentation errors", which pointed to incorrect memory allocation in a C library. Without having any clue of what the C code does exactly, I simply guess, XXTEA might not be thread safe as it is.

Let me know if I can give you some more information concerning the error(s).

decrypt not working ,return a blank string.

decrypt a base64 encode data fail,
Original Base64 Data:
'AwCAAAAAAAD/////AIAAALAFAC120z+he5wg0Hyu5WKtwk75YUr6b/0DasM3Ut8cMkFlZ5YmvcLqUBKNEwRhcEJV0Zc='

data after Base64 Decode:
'\x03\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x80\x00\x00\xb0\x05\x00-v\xd3?\xa1{\x9c\xd0|\xae\xe5b\xad\xc2N\xf9aJ\xfao\xfd\x03j\xc37R\xdf\x1c2Aeg\x96&\xbd\xc2\xeaP\x12\x8d\x13\x04apBU\xd1\x97'

data part to decrypt:
data = 'v\xd3?\xa1{\x9c\xd0|\xae\xe5b\xad\xc2N\xf9aJ\xfao\xfd\x03j\xc37R\xdf\x1c2Aeg\x96&\xbd\xc2\xeaP\x12\x8d\x13\x04apBU\xd1\x97'

key='WMfc9i0Kq9cTdhgv'
but the decrypt(data,key) gave me a blank string,is this a compatibility problem,the original data is from java platform?

Readme comparisons are incorrect for python 3.5.

I think the behavior of the (still in beta) 3.5 version of python has changed. Comparisons of input and output values don't work without decoding to utf.

For example:

s == dec
False
s == dec.decode('utf-8')
True

Build Failiure on windows

Collecting xxtea
Using cached xxtea-2.0.0.post0.tar.gz (7.4 kB)
Preparing metadata (setup.py) ... done
Installing collected packages: xxtea
DEPRECATION: xxtea is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at pypa/pip#8559
Running setup.py install for xxtea ... error
error: subprocess-exited-with-error

× Running setup.py install for xxtea did not run successfully.
│ exit code: 1
╰─> [18 lines of output]
running install
C:\Users\Nuwuy\AppData\Local\Programs\Python\Python311\Lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build
running build_ext
building 'xxtea' extension
creating build
creating build\temp.win-amd64-cpython-311
creating build\temp.win-amd64-cpython-311\Release
"D:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\Nuwuy\AppData\Local\Programs\Python\Python311\include -IC:\Users\Nuwuy\AppData\Local\Programs\Python\Python311\Include "-ID:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\include" "-ID:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\ATLMFC\include" "-ID:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /Tcxxtea.c /Fobuild\temp.win-amd64-cpython-311\Release\xxtea.obj
xxtea.c
xxtea.c(60): warning C4018: '<': signed/unsigned mismatch
xxtea.c(194): warning C4244: '=': conversion from 'Py_ssize_t' to 'int', possible loss of data
xxtea.c(195): warning C4244: '=': conversion from 'Py_ssize_t' to 'int', possible loss of data
xxtea.c(290): warning C4244: '=': conversion from 'Py_ssize_t' to 'int', possible loss of data
xxtea.c(291): warning C4244: '=': conversion from 'Py_ssize_t' to 'int', possible loss of data
xxtea.c(340): error C2106: '=': left operand must be l-value
error: command 'D:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe' failed with exit code 2
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> xxtea

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

Error when compiling on Windows / Py3

To compile xxtea on Windows / 32 bit / MSVS 10 Express, the declarations on line 165 (int i, j, top, bot;) need to be moved to the first line of the function definition. Don't know if this is a problem on other systems, as well.

Error installing on Windows 10 / Python 2.7.12 32 bit

Related logs are as belows:

C:\WINDOWS\system32>pip install xxtea -U
Collecting xxtea
Using cached xxtea-1.0.2.tar.gz
Installing collected packages: xxtea
Running setup.py install for xxtea ... error
Complete output from command c:\python27\python.exe -u -c "import setuptools, tokenize;file='c:\users\pla\appdata\local\temp\pip-build-rkwgrr\xxtea\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record c:\users\pla\appdata\local\temp\pip-bm2_qm-record\install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_ext
building 'xxtea' extension
creating build
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -DVERSION=1.0.2 -Ic:\python27\include -Ic:\python27\PC /Tcxxtea.c /Fobuild\temp.win32-2.7\Release\xxtea.obj
xxtea.c
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(92) : warning C4005: '_CRT_STRINGIZE' : macro redefinition
C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\crtdefs.h(180) : see previous definition of '_CRT_STRINGIZE'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(95) : warning C4005: '_CRT_WIDE' : macro redefinition
C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\crtdefs.h(185) : see previous definition of '_CRT_WIDE'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(156) : warning C4005: '__CRTDECL' : macro redefinition
C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\crtdefs.h(650) : see previous definition of '__CRTDECL'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(210) : error C2061: syntax error : identifier '__vcrt_bool'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(210) : error C2059: syntax error : ';'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vcruntime.h(293) : warning C4005: '_CRT_WARNING' : macro redefinition
C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\crtwrn.h(24) : see previous definition of '_CRT_WARNING'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\stdint.h(111) : warning C4005: 'WCHAR_MIN' : macro redefinition
C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\wchar.h(43) : see previous definition of 'WCHAR_MIN'
xxtea.c(70) : warning C4018: '<' : signed/unsigned mismatch
error: command 'C:\Users\PLA\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe' failed with exit status 2

----------------------------------------

Command "c:\python27\python.exe -u -c "import setuptools, tokenize;file='c:\users\pla\appdata\local\temp\pip-build-rkwgrr\xxtea\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record c:\users\pla\appdata\local\temp\pip-bm2_qm-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\pla\appdata\local\temp\pip-build-rkwgrr\xxtea\

Error installing on Windows 10 / Python 2.7.10 32 bit

Related log as follows:

xxtea.c(29) : fatal error C1083: Cannot open include file: 'stdint.h': No such file or directory
  error: command 'C:\\Users\\Joker Qyou\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.