Questions tagged [pycryptodome]

Questions about the usage of the PyCryptodome Python package in programming. PyCryptodome is a self-contained Python package of low-level cryptographic primitives. It is a fork of of the PyCrypto project and it is designed to replace it, since PyCrypto is not being maintained anymore. The package contains a wide collection of secure hash functions and various encryption algorithms, and it also provides support for random generation.

About PyCryptodome

PyCryptodome is a self-contained Python package of low-level cryptographic primitives, created as a fork of PyCrypto.

PyCryptodome exposes almost the same API as the old PyCrypto so that most applications will run unmodified. See this page from the official website for more details.

It supports Python 2.6 or newer, all Python 3 versions and PyPy.

Official resources

Installation

From the PyCryptodome repository:

The installation procedure depends on the package you want the library to be in.
PyCryptodome can be used as:

  • an almost drop-in replacement for the old PyCrypto library.
    You install it with:

    pip install pycryptodome   
    

    In this case, all modules are installed under the Crypto package. One must avoid having both PyCrypto and PyCryptodome installed at the same time, as they will interfere with each other.

    This option is therefore recommended only when you are sure that
    the whole application is deployed in a virtualenv.

  • a library independent of the old PyCrypto. You install it with::

    pip install pycryptodomex   
    

    In this case, all modules are installed under the Cryptodome package. PyCrypto and PyCryptodome can coexist.

For faster public key operations in Unix, you should install GMP in your system.

Differences from PyCrypto

From the PyCryptodome repository:

It brings the following enhancements with respect to the last official version of PyCrypto (2.6.1):

  • Authenticated encryption modes (GCM, CCM, EAX, SIV, OCB)
  • Accelerated AES on Intel platforms via AES-NI
  • First class support for PyPy
  • Elliptic curves cryptography (NIST P-256 curve only)
  • Better and more compact API (nonce and iv attributes for ciphers, automatic generation of random nonces and IVs, simplified CTR cipher mode, and more)
  • SHA-3 (including SHAKE XOFs), SHA-512/t and BLAKE2 hash algorithms
  • Salsa20 and ChaCha20 stream ciphers
  • scrypt and HKDF
  • Deterministic (EC)DSA
  • Password-protected PKCS#8 key containers
  • Shamir's Secret Sharing scheme
  • Random numbers get sourced directly from the OS (and not from a CSPRNG in userspace)
  • Simplified install process, including better support for Windows
  • Cleaner RSA and DSA key generation (largely based on FIPS 186-4)
  • Major clean ups and simplification of the code base

PyCryptodome is not a wrapper to a separate C library like OpenSSL. To the largest possible extent, algorithms are implemented in pure Python. Only the pieces that are extremely critical to performance (e.g. block ciphers) are implemented as C extensions.

270 questions
25
votes
1 answer

What is pycryptodomex and how does it differ from pycryptodome?

Today I saw PySNMP installing pycryptodomex. The x in that name looked suspicious and surprising. I tried to track it down, but it looks like both pycryptodome and pycryptodomex are owned by the same account and point back to the same Github…
Jason R. Coombs
  • 41,115
  • 10
  • 83
  • 93
14
votes
6 answers

ModuleNotFoundError: No module named 'Crypto' Error

I have installed pycryptodomex module on python 3.6.5 but when i try to execute the below call, i get the error mentioned in the headline from Crypto.Cipher import AES I want to encrypt a file using AES. How to proceed now ?
achilles59
  • 145
  • 1
  • 1
  • 5
10
votes
1 answer

Decrypting AES CBC in python from OpenSSL AES

I need to decrypt a file encrypted on OpenSSL with python but I am not understanding the options of pycrypto. Here what I do in OpenSSL openssl enc -aes-256-cbc -a -salt -pbkdf2 -iter 100000 -in "clear.txt" -out "crypt.txt" -pass…
gmmo
  • 2,577
  • 3
  • 30
  • 56
7
votes
1 answer

RSA sign a string with private key in python

I am communicating with our clients server. For an api I need to sign a string with my private key. They have the following condition to follow User SHA 256 algorithm to calculate the hash of the string Use the private key and RSA (PKCS1_PADDING)…
varad
  • 7,309
  • 20
  • 60
  • 112
7
votes
1 answer

RSA decryption of AES Session key fails with 'AttributeError: 'bytes' object has no attribute 'n'

I'm working on implementing a public key encryption from PyCryptodome on Python 3.6. When I try to create a symmetric encryption key and encrypt/decrypt variables, it all works fine. But the minute I introduce RSA (and PKCS1_OAEP), it all goes down…
kilokahn
  • 1,136
  • 2
  • 18
  • 38
6
votes
1 answer

Pycryptodome RSA decryption causes massive performance downgrade (RPS)

I am testing my flask application endpoint and measuring the performance on a single machine. That particular endpoint has a decorator called decrypt_request. The implementation of this decorator looks like this: 1. Read X-Session-Key header…
Ahmed Dhanani
  • 841
  • 1
  • 11
  • 32
6
votes
1 answer

Padding is incorrect. AES Python encryption

I'm trying to put together a simple encryption using python. This is the encrypt: from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.Padding import unpad BLOCK_SIZE = 32 def encrypt(message): obj = AES.new(b'This…
Seif
  • 701
  • 4
  • 13
  • 32
5
votes
1 answer

TypeError: decrypt() cannot be called after encrypt()

I am writing a simple code of AES encryption and I got stuck at a part where it says: TypeError: decrypt() cannot be called after encrypt() I tried changing the sequence of these lines, but it did not help: encrypted = encrypt(message) decrypted =…
Sami
  • 466
  • 1
  • 5
  • 12
5
votes
1 answer

pycryptodome : MAC Check Failed (using decrypt_and_verify)

I am working on an encryption program with Pycryptodome in Python 3.6 I am trying to encrypt a file and then decrypt it and verify the MAC tag. When I get to verify it, an error is thrown import os from Crypto.Cipher import AES bib Cryptodome…
jeff tran
  • 51
  • 1
  • 3
5
votes
2 answers

Pycrypto AES GCM encryption and Java decryption

I'm using Pycryptodome (a PyCrypto fork) to create AES-GCM ciphertexts. I use the following Python code to encrypt: cek = os.urandom(16) nonce = os.urandom(12) cipher = AES.new(cek, AES.MODE_GCM, nonce=nonce, mac_len=16) ciphertext =…
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
5
votes
1 answer

PyCryptodome Error: MAC Check Failed

I am working on an encryption program with Pycryptodome in Python 3. I am trying to encrypt a (byte) string and then decrypt it and verify the MAC tag. When I get to verify it, an error is thrown. This is the code: from Crypto.Cipher import AES from…
purple_dot
  • 106
  • 2
  • 9
5
votes
1 answer

Pycryptodome official example unclear

I have an issue with https://www.pycryptodome.org/en/latest/src/examples.html#encrypt-data-with-rsa from Crypto.PublicKey import RSA from Crypto.Random import get_random_bytes from Crypto.Cipher import AES, PKCS1_OAEP file_out =…
HCLivess
  • 1,015
  • 1
  • 13
  • 21
4
votes
1 answer

What is the difference between Crypto.Signature.PKCS1_v1_5 and Crypto.Signature.pkcs1_15?

I was wondering which is the difference between Crypto.Signature.PKCS1_v1_5 and Crypto.Signature.pkcs1_15? In the documentation they use this function Crypto.Signature.pkcs1_15 but sometimes I've seen that Crypto.Signature.PKCS1_v1_5 was used. What…
Emanuele
  • 174
  • 13
4
votes
1 answer

ERROR: Failed building wheel for pycryptodome

I was trying to install pycryptodome, python-jose-cryptodome using pip within anaocnda3 environment. I got this error: ERROR: Failed building wheel for pycryptodome I have tried many versions many solutions(latest versions, specified version,…
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
4
votes
2 answers

pycryptodome setup.py causing an error when installing pyrebase

I am trying to install Pyrebase, but I keep getting an error that I believe is caused by setup.py for pycryptodome. I have searched all over but cannot find a solution, does anyone know how to solve this? My output is below - I added in (...) in…
rafi
  • 129
  • 3
  • 9
1
2 3
17 18