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
2
votes
1 answer

Can't verify signature with PyCryptodome ECC (ASN1?)

I'm curently working on a small program to automate decryption in python using PyCryptodome i have a shell version of my test that work just fine but i can't figure out why it won't verify in python (maybe an encode/decode problem?) private ECC key…
Bastien B
  • 1,018
  • 8
  • 25
2
votes
0 answers

Generating rsa key pairs then using copy.deepcopy() on the object generates pickling error !! Any help please?

Here, I generate a key pair for the transaction then I deepcopy it later, which generates the pickling error: from Crypto.PublicKey import RSA import random import sys from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 from…
2
votes
2 answers

AES CTR decryption: Cryptography and Cryptodome give different results

The below code snippet that AES-decrypts some bytes using Cryptodome works as I expect: from Crypto.Cipher import AES from Crypto.Util import Counter key = b'\x12' * 32 decryptor = AES.new(key, AES.MODE_CTR,counter=Counter.new(nbits=128,…
Michal Charemza
  • 25,940
  • 14
  • 98
  • 165
2
votes
1 answer

AttributeError: 'bytes' object has no attribute 'n'

I am trying to encode a piece of text using the Crypto method. I need to encode a piece of string using the RSA method with a given public key, and this is the code I have currently written, after referring to this link. My code... from…
Najaaz
  • 380
  • 5
  • 16
2
votes
1 answer

Python import of ECC private key in PEM encoding fails

I'm running Python version 3.8.2 and using pycryptodome version 3.9.9 to import an ECC private key in PEM encoding for later signing some data. The following EC private key is a sample key, and I'm using it for several cross-platform projects [e.g.…
Michael Fehr
  • 5,827
  • 2
  • 19
  • 40
2
votes
1 answer

Encrypt a big file that does not fit in RAM with AES-GCM

This code works for a file myfile which fits in RAM: import Crypto.Random, Crypto.Cipher.AES # pip install pycryptodome nonce = Crypto.Random.new().read(16) key = Crypto.Random.new().read(16) # in reality, use a key derivation function, etc. ouf…
Basj
  • 41,386
  • 99
  • 383
  • 673
2
votes
1 answer

Encryption and decryption using python3 and node js

I am trying to create a multi-platform encryption-decryption mechanism, so far I have been able to encrypt in python and decrypt in C and vice versa, now I am trying to do the same using the python script and a node js script. I am able to encrypt a…
phoenix
  • 49
  • 7
2
votes
3 answers

PyCharm says No Module named Crypto

I keep getting an import error saying: ModuleNotFoundError: No module named 'Crypto' I have gone over various other answers about how to resolve this, but it does not mention how I can resolve the issue in PyCharm. I originally tried to pip3…
lionheart
  • 333
  • 2
  • 11
2
votes
1 answer

PyCryptoDome/cryptography inequality with AES-CFB in python

While running a test to make sure that the two different libraries give the same output, I found out that they don't with CFB. Code to replicate the problem is: from cryptography.hazmat.backends import default_backend from…
Legorooj
  • 2,646
  • 2
  • 15
  • 35
2
votes
1 answer

Trouble with AES Cryptography (specifically encrypting with CryptoSwift and decrypting with PyCryptodome

I am currently trying to use AES cryptography to encrypt and decrypt a string that always has a length of 9 characters. What I am trying to do is to encrypt the string in swift and then decrypt that encrypted string in python. I am using AES…
2
votes
1 answer

Cannot decrypt given cyphertext twice with same AES Object

I am using an AES object (aesDecryptObj ) to decrypt a cyphertext that was encrypted using a seperate AES object (aesEncryptObj ). def aesInit(): global aesEncryptObj global aesDecryptObj aesKey = aesEncryptObj =…
2
votes
2 answers

RSA code written with PyCrypto doesn't work with PyCryptodome

I'm trying to run two simple functions that I wrote for PyCrypto with PyCryptodome. Here are the functions with the related class member definitions: import Crypto.PublicKey.RSA as RSA class MyRSA(): n = "123..." # these are actually very…
Gad82
  • 91
  • 8
2
votes
3 answers

ImportError: cannot import name 'RSA' from 'Crypto.PublicKey'

Hi I am trying to connect my python script to the google firebase. I have run into some issues with installing pyrebase. when i install using pip install pyrebase i get this error message: image 1: However i have tried several way to fix this, one…
scubasteve7
  • 57
  • 2
  • 2
  • 5
2
votes
3 answers

Python AES-CTR is not compatible with Golang

I'm using Python2.7 with pycryptodome 3.6.6 and Golang1.10.4 on Ubuntu 16.04. The encrypt algorithm I choose is AES-CTR-128. But the data encrypted by Python and Golang have different result. So there's a problem to communicate between the apps…
Wizmann
  • 839
  • 1
  • 9
  • 14
2
votes
2 answers

Python3 Cryptodome - how to decrypt pem?

I am trying to decrypt my private key in python using cryptodome. Under raw_cipher_data is my password to encrypt the private key. But I get the error message "ValueError: PEM is encrypted, but no passphrase available" MY IMPORT from…
TeslaXba
  • 347
  • 4
  • 22
1 2
3
17 18