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
0
votes
0 answers

Struggling with AES in python

To keep it short: I am sending an encrypted message (AES with CBC) to another service, and it returns me an encrypted response, but I can't decrypt it because they are not using padding? (to be honest, I don't know much about encryption and its…
0
votes
0 answers

Firebase in Python venv not working due to 'No module named 'Crypto'

I am trying to use Firebase in my python web app, but when I installed firebase in my venv, everytime I run the python file I get a dependency error. I just kept pip installing them until I got to "No module named 'Crypto'." I have tried doing: pip…
Tory
  • 109
  • 11
0
votes
1 answer

ValueError: MAC check failed when using PyCryptodome to decrypt data coming from NiFi

Can someone help me with this problem: I am encrypting a JSON in NiFi with AES_GCM algorithm and using a KDF PBKDF2. The idea is to decrypt this JSON with a python script using PyCryptodome. The following code is an attempt to see if the NiFi…
0
votes
0 answers

Padding is incorrect for AES.MODE_GCM mode in python

I'm trying to decrypt a value which I'm reading from a CSV file. (I've encrypted the value of an integer and wrote it to a CSV file separately). Encryption: from Crypto.Random import get_random_bytes from Crypto.Cipher import AES from…
rishab ajain445
  • 155
  • 1
  • 10
0
votes
0 answers

Pycryptodome's Shamir Secret Sharing API not working

I am trying to use SSS to encrypt my private keys, using a modified version of the example code shown in the docs. # encrypt.py from binascii import hexlify from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from…
disguisedtoast
  • 149
  • 1
  • 4
  • 15
0
votes
0 answers

UnicodeDecodeError: 'utf-8' codec can't decode byte - AES.MODE_EAX Encryption - Pycryptodome

I'm trying to encrypt and decrypt values, using python pycryptodome, saved to my Postgresql database on the client side based on example 2 found here; SymmetricEncryptionClientSide. However, I keep running into UnicodeDecodeErrors, such as the…
Kakedis
  • 172
  • 8
0
votes
0 answers

Decrypting cipher text with rsa public key in python

I need to decrypt a message get using a public key in python. I know it is a bad practice and public key shouldn't be used for decryption, but a client demands it! I tried using pycryptodome, but it doesn't support decryption with public key. Is…
razieh babaee
  • 298
  • 5
  • 19
0
votes
1 answer

PyCryptodome- Hex values change when put into a byte array for encryption

I'm trying to encrypt hex values as an input for an AES encryption. However, to use .encrypt(), only bytes, bytearrays or memoryview is the accept data types. So, I've been using bytearray.fromhex() to convert my hex values into a byte array. My…
ImKeelan
  • 25
  • 5
0
votes
0 answers

Python module not installed in Spyder but in all other IDEs

I have pip'd pycryptodome and criptography today to use in the Spyder IDE but keep getting the error at the bottom of this page when I try to import them. However, If I try to import them in Sublime Text or IDLE they seem to be working fine. I am…
0
votes
0 answers

ConnectionAbortedError: [WinError 10053] using socket and rsa encryption with pycryptodome

server.py from socket import * from threading import * from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP length = 4096 key = RSA.generate(2048) public_key = key.publickey() host = '127.0.0.1' port = int(input("Enter a port:…
KeinDubai
  • 11
  • 3
0
votes
1 answer

ValueError: Data must be aligned to block boundary in ECB mode (or additional backslashes from encoding of encrypted text)

I have this code: from Crypto.Cipher import DES # Encryption part key = b'abcdefgh' def pad(text): while len(text) % 8 != 0: text += b' ' return text des = DES.new(key, DES.MODE_ECB) text =…
0
votes
0 answers

How to make your own RSA key from bitstream?

I have a task to generate private and public RSA keys. However, for private key I am supposed to use a bitstream generated from random generator that I implemented. I don't know how to transform a bitstream into a Cryto.PublicKey.RSA.RsaKey object.…
Gre1234
  • 11
  • 5
0
votes
1 answer

Troubles with decryption in a socket chat

I am trying to make a socket chat in Python for my coursework in university. I need to encrypt messages for communication between multiple clients and send it to them. My encryption and message sending scheme is: I generate a packet using the pickle…
Artemiy
  • 1
  • 1
0
votes
0 answers

Is it possible to reverse a HMAC hash with Python?

I discovered the Hash-based Message Authentication Code (HMAC) and how to use it with a SHA-256 hash for example. I follow that link https://pycryptodome.readthedocs.io/en/latest/src/hash/hmac.html. So I gave it a try and understood how to generate…
Julien
  • 699
  • 3
  • 14
  • 30
0
votes
0 answers

ValueError: MAC check failed pycryptodom

Can anyone help me correct this error.I am using python 3.9 and pycryptodome to make a pyqt5 gui project. Here is the code: from Crypto.Cipher import AES import scrypt import os class AESCipher: def __init__(self, password): …
ntc
  • 1