frankmorgner/vsmartcard

vicc - No module named virtualsmartcard / No module named 'Crypto'

Closed this issue · 4 comments

Hello, when I try to use vicc, I have errors.

As I see, this problem already was there, but was closed unanswered - #217

I use
Ubuntu desktop 20.04
Python 3.8.8

whereis vicc : /usr/local/bin/vicc

So when I run vicc, I get

Traceback (most recent call last):
File "./vicc", line 125, in
from virtualsmartcard.VirtualSmartcard import VirtualICC
ModuleNotFoundError: No module named 'virtualsmartcard'

And I have already changed PYTHONPATH and PATH:

export PATH=/usr/local/lib/python3.8/site-packages/virtualsmartcard:$PATH
export PYTHONPATH=/usr/local/lib/python3.8/site-packages/virtualsmartcard:$PYTHONPATH

I tried to cheat : I copied package virtualsmartcard to /usr/local/bin/vicc, but then i get another errors:

Traceback (most recent call last):
File "/usr/local/bin/virtualsmartcard/CryptoUtils.py", line 29, in
from Crypto.Cipher import DES3, DES, AES, ARC4 # @UnusedImport
ModuleNotFoundError: No module named 'Crypto'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "vicc", line 117, in
from virtualsmartcard.VirtualSmartcard import VirtualICC
File "/usr/local/bin/virtualsmartcard/VirtualSmartcard.py", line 31, in
from virtualsmartcard.CardGenerator import CardGenerator
File "/usr/local/bin/virtualsmartcard/CardGenerator.py", line 34, in
from virtualsmartcard.SmartcardSAM import SAM
File "/usr/local/bin/virtualsmartcard/SmartcardSAM.py", line 24, in
import virtualsmartcard.CryptoUtils as vsCrypto
File "/usr/local/bin/virtualsmartcard/CryptoUtils.py", line 34, in
from hashlib import hmac as HMAC
ImportError: cannot import name 'hmac' from 'hashlib' (/home/user/anaconda3/lib/python3.8/hashlib.py)

What can I do?

Make sure PYTHONPATH points to the site-packages folder and you can do it without cheating

export PYTHONPATH=/usr/local/lib/python3.10/site-packages:$PYTHONPATH

The latter error is weird, but you can resolve it by installing PyCrypto, see the source

pip install pycrypto

Just to bump into another issue

Traceback (most recent call last):
  File "/usr/local/bin/vicc", line 116, in <module>
    from virtualsmartcard.VirtualSmartcard import VirtualICC
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/VirtualSmartcard.py", line 30, in <module>
    from virtualsmartcard.CardGenerator import CardGenerator
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/CardGenerator.py", line 34, in <module>
    from virtualsmartcard.SmartcardSAM import SAM
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/SmartcardSAM.py", line 24, in <module>
    import virtualsmartcard.CryptoUtils as vsCrypto
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/CryptoUtils.py", line 29, in <module>
    from Crypto.Cipher import DES3, DES, AES, ARC4  # @UnusedImport
  File "/opt/homebrew/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py", line 119, in <module>
    key_size = xrange(1,256+1)
NameError: name 'xrange' is not defined. Did you mean: 'range'?

So it seems PyCrypto is trying to use Python2, therefore I've added the following snippet (credit) to /opt/homebrew/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py and vicc seems to work now.

try:
    # Python 2
    xrange
except NameError:
    # Python 3, xrange is now named range
    xrange = range

Maybe @frankmorgner can help regarding the original query about ImportError: cannot import name 'hmac' from 'hashlib'.

HTH.

[...]

Traceback (most recent call last):
  File "/usr/local/bin/vicc", line 116, in <module>
    from virtualsmartcard.VirtualSmartcard import VirtualICC
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/VirtualSmartcard.py", line 30, in <module>
    from virtualsmartcard.CardGenerator import CardGenerator
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/CardGenerator.py", line 34, in <module>
    from virtualsmartcard.SmartcardSAM import SAM
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/SmartcardSAM.py", line 24, in <module>
    import virtualsmartcard.CryptoUtils as vsCrypto
  File "/usr/local/lib/python3.10/site-packages/virtualsmartcard/CryptoUtils.py", line 29, in <module>
    from Crypto.Cipher import DES3, DES, AES, ARC4  # @UnusedImport
  File "/opt/homebrew/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py", line 119, in <module>
    key_size = xrange(1,256+1)
NameError: name 'xrange' is not defined. Did you mean: 'range'?

So it seems PyCrypto is trying to use Python2, therefore I've added the following snippet (credit) to /opt/homebrew/lib/python3.10/site-packages/Crypto/Cipher/ARC4.py and vicc seems to work now.

As the backtrace suggests, this seems to be related to PyCrypto. I'm not seeing that when running without (with the PR mentioned below in place).
Given that the PyCrypto website itself says that PyCrypto is obsolete and no longer maintained, I'm wondering whether it might be a good idea to drop the PyCrypto-specific parts and use only the stdlib code path, or port to either Cryptography or the active fork PyCryptodome, as suggested on the PyCrypto website:

This software is no longer maintained.

PyCrypto 2.x is unmaintained, obsolete, and contains security vulnerabilities.

Please choose one of the following alternatives:
Cryptography

  • Recommended for new applications.
  • Newer API with fewer gotchas.
  • API docs
  • GitHub
  • PyPI

PyCryptodome

  • Recommended for existing software that depends on PyCrypto.
  • Fork of PyCrypto. Most applications should run unmodified.
  • API docs
  • GitHub
  • PyPI

wrt

Maybe @frankmorgner can help regarding the original query about ImportError: cannot import name 'hmac' from 'hashlib'.

I'm running into the ImportError: cannot import name 'hmac' from 'hashlib' as well and from what I can see, it looks to me as if the import was wrong. I have created PR #230 with a suggested fix.

@michaelweghorn feel free to open another PR with a switch to PyCryptodome and I'll try to have a look. thank you!

@michaelweghorn feel free to open another PR with a switch to PyCryptodome and I'll try to have a look. thank you!

Thanks, I have submitted #232 now.