from Crypto.Util.number import getPrime, isPrimeのエラーを解消する

目的

SECCON Beginners CTF 2024で

from Crypto.Util.number import getPrime, isPrime

が組み込まれているソースで

D:\Users\so2akt\Desktop\Safe Prime>python3 chall.py
Traceback (most recent call last):
  File "D:\Users\so2akt\Desktop\Safe Prime\chall.py", line 2, in <module>
    from Crypto.Util.number import getPrime, isPrime
ModuleNotFoundError: No module named 'Crypto'

というエラーにしかならなかった.それをなんとか,エラーなく実行できるようになったので記載しておく.

名称 規格 製造会社 備考
オペレーティングシステム Windows 10 Pro 64ビット,22H2 マイクロソフト
Python2 2.7.12 Python Software Foundation python --versionにて確認
Python3 3.12.4 Python Software Foundation python3 --versionにて確認
package installer for Python(pip) 24.0 Python Software Foundation pip listおよびpython3 -m pip listにて確認
Cryptographic library for Python(pycryptodome) 3.20.0

結論

python3 -m pip install pycryptodome

でインストールすると問題なくfrom Crypto.Util.number import getPrime, isPrimeが実行できた.from PIL import Imageのエラーを解消するにてpip install foo-barで失敗していたので,pip install pycryptodomeは試していない.

なお,他にインストールされているパッケージは以下のとおり:

D:\>python3 -m pip list
Package            Version
------------------ --------
certifi            2024.6.2
charset-normalizer 3.3.2
idna               3.7
Naked              0.1.32
numpy              1.26.4
opencv-python      4.9.0.80
pillow             10.2.0
pip                24.0
pycryptodome       3.20.0
PyYAML             6.0.1
requests           2.32.3
shellescape        3.8.1
urllib3            2.2.1

その他

Pythonは,バージョン(?)などの環境差分があるようで,調べたとおりにパッケージをインストールしても動作しなかった.pycryptoのインストールでOKという情報を得たが,そもそもインストールできなかった.pycryptoは失敗したが,pycryptodomeで成功した.

以下はインストール実施のコンソール生データで,uninstallをしているのは,調べた結果に従ったものである.

pip install pycrypto

D:\Users\so2akt\Desktop\Safe Prime>pip uninstall Crypto
WARNING: Skipping Crypto as it is not installed.

D:\Users\so2akt\Desktop\Safe Prime>pip uninstall pycrypto
WARNING: Skipping pycrypto as it is not installed.

D:\Users\so2akt\Desktop\Safe Prime>pip install pycrypto
Collecting pycrypto
  Downloading pycrypto-2.6.1.tar.gz (446 kB)
     ---------------------------------------- 446.2/446.2 kB 9.3 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pycrypto
  Building wheel for pycrypto (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for pycrypto (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [28 lines of output]
      warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
      winrand.c
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(31): error C2061: syntax error: identifier 'intmax_t'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(32): error C2061: syntax error: identifier 'rem'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(32): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(33): error C2059: syntax error: '}'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(35): error C2061: syntax error: identifier 'imaxdiv_t'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(35): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(45): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(46): error C2146: syntax error: missing ')' before identifier '_Number'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(46): error C2061: syntax error: identifier '_Number'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(46): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(47): error C2059: syntax error: ')'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(50): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2146: syntax error: missing ')' before identifier '_Numerator'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2061: syntax error: identifier '_Numerator'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2059: syntax error: ','
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(53): error C2059: syntax error: ')'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(55): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(61): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(68): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(74): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(81): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(87): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(94): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(100): error C2143: syntax error: missing '{' before '__cdecl'
      error: command 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.38.33130\\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: Failed building wheel for pycrypto
Failed to build pycrypto
ERROR: Could not build wheels for pycrypto, which is required to install pyproject.toml-based projects

python3 -m pip install pycrypto……失敗

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip uninstall Crypto
WARNING: Skipping Crypto as it is not installed.

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip uninstall pycrypto
WARNING: Skipping pycrypto as it is not installed.

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip install pycrypto
Defaulting to user installation because normal site-packages is not writeable
Collecting pycrypto
  Using cached pycrypto-2.6.1.tar.gz (446 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pycrypto
  Building wheel for pycrypto (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for pycrypto (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [28 lines of output]
      warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
      winrand.c
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(31): error C2061: syntax error: identifier 'intmax_t'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(32): error C2061: syntax error: identifier 'rem'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(32): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(33): error C2059: syntax error: '}'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(35): error C2061: syntax error: identifier 'imaxdiv_t'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(35): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(45): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(46): error C2146: syntax error: missing ')' before identifier '_Number'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(46): error C2061: syntax error: identifier '_Number'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(46): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(47): error C2059: syntax error: ')'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(50): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2146: syntax error: missing ')' before identifier '_Numerator'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2061: syntax error: identifier '_Numerator'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2059: syntax error: ';'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(51): error C2059: syntax error: ','
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(53): error C2059: syntax error: ')'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(55): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(61): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(68): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(74): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(81): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(87): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(94): error C2143: syntax error: missing '{' before '__cdecl'
      C:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt\inttypes.h(100): error C2143: syntax error: missing '{' before '__cdecl'
      error: command 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.38.33130\\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: Failed building wheel for pycrypto
Failed to build pycrypto
ERROR: Could not build wheels for pycrypto, which is required to install pyproject.toml-based projects

python3 -m pip install pycryptodome……成功

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip uninstall Crypto
Found existing installation: crypto 1.4.1
Uninstalling crypto-1.4.1:
  Would remove:
    c:\users\so2akt\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\scripts\crypto.exe
    c:\users\so2akt\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\scripts\decrypto.exe
    c:\users\so2akt\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages\crypto-1.4.1.dist-info\*
    c:\users\so2akt\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages\crypto\*
Proceed (Y/n)?
  Successfully uninstalled crypto-1.4.1

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip uninstall pycrypto
WARNING: Skipping pycrypto as it is not installed.

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip uninstall pycryptodome
WARNING: Skipping pycryptodome as it is not installed.

D:\Users\so2akt\Desktop\Safe Prime>python3 -m pip install pycryptodome
Defaulting to user installation because normal site-packages is not writeable
Collecting pycryptodome
  Downloading pycryptodome-3.20.0-cp35-abi3-win_amd64.whl.metadata (3.4 kB)
Downloading pycryptodome-3.20.0-cp35-abi3-win_amd64.whl (1.8 MB)
   ---------------------------------------- 1.8/1.8 MB 10.2 MB/s eta 0:00:00
Installing collected packages: pycryptodome
Successfully installed pycryptodome-3.20.0

参考サイト

  1. ModuleNotFoundError: No module named 'Crypto'(2024/06/16現在)
  2. ModuleNotFoundError: No module named 'Crypto'(2024/06/16現在)