24

I cannot find anything on this. I'm getting error:

Traceback (most recent call last):
  File "/path/to/pwdb.py", line 265, in <module>
    password_db()
  File "/path/to/pwdb.py", line 73, in __init__
    self.cipher = AES.new(key,AES.MODE_ECB)
  File "/home/STACKOVERFLOW/.local/lib/python3.10/site-packages/Crypto/Cipher/AES.py", line 95, in new
    return AESCipher(key, *args, **kwargs)
  File "/home/STACKOVERFLOW/.local/lib/python3.10/site-packages/Crypto/Cipher/AES.py", line 59, in __init__
    blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
  File "/home/STACKOVERFLOW/.local/lib/python3.10/site-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

I'm pretty sure the line it's getting the error from is:

self.cipher = AES.new(key,AES.MODE_ECB)

The script was working not too long ago. Did PyCrypto update its formatting or something ? And does anyone have any idea on how to fix this ? I can provide more of the code if need be.

6 Answers 6

28

uninstall pycryto and install pycryptodome

pip uninstall pycrypto
pip install pycryptodome
5
  • 11
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 17, 2022 at 21:33
  • 3
    pycrypto is no longer maintained: see pycrypto.org pycryptodome is the modern maintained replacement for pycrypto Commented Jan 18, 2023 at 16:35
  • Downvoted because: No explanation and didn’t change anything on my system. Commented Jul 7, 2023 at 15:45
  • ERROR: Cannot uninstall 'pycrypto'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
    – Fermi-4
    Commented Jul 12, 2023 at 3:03
  • 1
    actually the sequence might be pip uninstall pycrypto pip uninstall pycryptodome pip install pycryptodome Commented Apr 18 at 13:55
18

It sounds like the extension was not updated for python 3.10.

On 3.10 any module(s) that use the # variant when parsing arguments need to have a #define PY_SSIZE_T_CLEAN before including Python.h.

From the docs:

For all # variants of formats (s#, y#, etc.), the macro PY_SSIZE_T_CLEAN must be defined before including Python.h. On Python 3.9 and older, the type of the length argument is Py_ssize_t if the PY_SSIZE_T_CLEAN macro is defined, or int otherwise.

See https://docs.python.org/3/c-api/arg.html#strings-and-buffers

2
  • 1
    what value should I set PY_SSIZE_T_CLEAN?
    – Rocketq
    Commented May 1, 2023 at 7:57
  • If you add #define PY_SSIZE_T_CLEAN, you should also go through all formats containing s#, etc., and make sure the length argument is a Py_ssize_t, not int.
    – user102008
    Commented Sep 22, 2023 at 23:33
9

You can use pycryptodome python package instead of pycrypto. Pycryptodome seems to have replaced the currently not maintained pycrypto package and works as is with the python 3.10 related changes as well.

2
  • 2
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 17, 2022 at 14:01
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Emi OB
    Commented May 19, 2022 at 10:05
0

I might have fixed it for AES, pretty sure I fixed it for RIPEMD160, as my slotmachine code is now running on termux, and it wasn't before. The code changes are at https://github.com/jcomeauictx/pycrypto/commit/38e5ebbf98fea96cf67b354e13fef7d1959ec34a, and you can install it using pip install git+https://github.com/jcomeauictx/pycrypto. Note that I haven't made any attempt to fix any security issues in the pycrypto code, I only addressed this PY_SSIZE_T_CLEAN problem.

5
  • you might want to mention that this is not release and still unmaintained so unuseable for mere mortals Commented Apr 18 at 13:45
  • how is it "unusable for mere mortals"? Commented Apr 19 at 16:06
  • 1
    I have several systems that choke unexpectedly with the dreadful PY-SIZE_T_CLEN message coming out of blue air if pycrypto is installled. The uninstall procedure is quite awkward (see this question and answer). If i do not have a one line pip install solution that won't help to solve my issue with dozens of sites and docker containers having this issue. The code changes seem to never have made it to pypi. Commented Apr 19 at 16:40
  • The "one-line pip install solution" I provided in my answer doesn't work for you? I tested it before posting. Commented Apr 20 at 17:34
  • i didn't even try it since it is not released on pypi Commented Apr 21 at 7:46
0

Got the same error trying to run bsdiff4 with python 3.10, installed using conda.

Turns out conda has an old version of bsdiff4 (1.1.5).

Uninstalling the conda version of bsdiff4 and re-installing the latest (1.2.4) using pip fixed the issue.

0

In my case, python was updated on my environment, and pycrypto seemed not to work with it.

It worked with the python 3.9.18 installed in my Virtual Env.

Check the python version you are working with and change it.

If you are using Visual Studio you can check on the bottom-right corner.

python version seen on visual studio footer

Not the answer you're looking for? Browse other questions tagged or ask your own question.