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

Wrong CMAC generation from Pycryptodome

As per the Example given in the documentation of PyCryptodome >>> from Crypto.Hash import CMAC >>> from Crypto.Cipher import AES >>> secret = b'Sixteen byte key' >>> cobj = CMAC.new(secret, ciphermod=AES) >>> cobj.update(b'Hello') >>> print…
NiK8
  • 19
  • 6
0
votes
0 answers

Python3 Incorrect AES key length (256 bytes)

I want to write a script that encrypts the content of a text file (key.k3y). When i execute it, i get the error: Incorrect AES key length (256 Bytes). Obviously that should not be invalid for AES. here is my code: import base64 import…
0
votes
0 answers

Experimenting with AES-128 symmetric key cipher.using python

I am trying to find remaining part of the key by brute-forcing the remaining 4 characters. Then we decrypt using each key and check if the decrypted message has "Salam" at the starting. CODE: from Cryptodome.Cipher import AES def revpad(s): …
Abbas
  • 59
  • 7
0
votes
1 answer

Convert hex() to pycryptodome ciphertext format

from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA new_file = open("md5_rehashed.txt", "w") file_with_hashes = open('md5_hashed.txt', 'r') key = RSA.importKey(open(received_message.decode('utf-8')).read()) cipher =…
Rhaigtz
  • 3
  • 1
0
votes
1 answer

PyCryptoDome cipher.encrypt saving blank text to encrypted file

I am having much trouble encrypting the contents of the file at path accountfile. The encryption does indeed work, as the decryption successfully output the path of accountfile of a version where the path was being saved to the file. The code runs…
atek
  • 3
  • 2
0
votes
1 answer

installing pycryptodome on Linux VM

Im got ubuntu 18.04.4 VM and trying to install pycryptodome via pip3 install pycryptodome The error I get is THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes.…
Ben
  • 129
  • 9
0
votes
0 answers

Pycryptodome AES_GCM Encryption to Java Decryption

I've been trying to do AES_GCM encryption in Python and decryption in Java, below is the relevant code snippet for what I'm trying to accomplish, I have also checked the question at: Pycrypto AES GCM encryption and Java decryption this is similar to…
BlueHam
  • 37
  • 4
0
votes
0 answers

No module named Crypto.Cipher Open EdX

Tried to import this code to our edx site encrypt data in python but it requires a package which is pycryptodome. Tried installing it using: pip install pycryptodome but it still shows an error everytime I call from Crypto.Cipher import…
Kelvin Barsana
  • 824
  • 12
  • 28
0
votes
1 answer

How to read all zipfile in python

I have a zipfile for example, i changed some files in zip and i need to encrypt it and after that save def encrypt(self, zipfile: ZipFile): import base64 with open(self.__db_path, 'wb') as db_file: decrypted_data = zipfile.read() …
dangost
  • 13
  • 2
0
votes
1 answer

AES decryption - java to python code conversion

I am trying to do integration with a Bank API. Here is the Java sample code provided by the bank to do encryption/decryption. package com.example.restservice; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import…
Ranjith Ramachandra
  • 10,399
  • 14
  • 59
  • 96
0
votes
1 answer

Python cryptodome AES-CBC / subprocess large command output issues

I have the following code for a simple client/server reverse shell in python3. it will connect fine, and any command with a small output it will work great. commands like "whoami" and listing the contents of a directory with one or two files. The…
K.Meleo
  • 25
  • 1
  • 1
  • 8
0
votes
0 answers

Error installing pycryptodome 3.4.3 using pip

I'm running python 3.7.4 and trying to install pycryptodome 3.4.3 using pip. I getting the following error. /usr/bin/ld: cannot find -lpython3.7m Full output Collecting pycryptodome==3.4.3 Using cached…
Bharath Kumar
  • 71
  • 1
  • 5
0
votes
1 answer

why is rsa key size_in_bytes calculated this way

I'm trying to understand the code that returns the size_in_bytes for a RSA key (I'm looking at PyCryptoDomex). They calculate it: def size_in_bytes(self): return (self.size_in_bits() - 1) // 8 + 1 I would have thought that it would simply…
Tim
  • 1,013
  • 3
  • 17
  • 36
0
votes
1 answer

Migrate RSA decryption from PyCrypto to PyCryptodome

Using the PyCrypto library, the following code prints 127: from Crypto.PublicKey import RSA import base64 # Private key in tuple form (obscured for privacy) key = [1, 1, 1] bk = "zJuG60z9Iv..." # (obscured for privacy) privatekey =…
dstaley
  • 1,002
  • 1
  • 12
  • 32
0
votes
1 answer

How can I place a pkcs signature using a RSA key in a transaction on a Block to put into Blockchain?

I'm currently working on a very simple project of a blockchain, mostly to understand the concept better by implementing it. I am using python do develop it so to implement a signature I used this function def sign(self): key =…
Gonçalo Correia
  • 1,595
  • 1
  • 10
  • 14