Questions tagged [pycrypto]

PyCrypto - The Python Cryptography Toolkit is a package that contains various cryptographic modules for the Python programming language.

Warning: PyCrypto seems not maintained anymore

It seems that the PyCrypto project has not been maintained since 2014. The developers are not doing any activity on the official repository during these years and they are not providing support about issues. This is why this post suggests to replace it with the PyCryptodome library, which still creates a Crypto package with an almost identical API and can be used with most software, although there are some exceptions. For more details about compatibility between these two packages visit this page.

About PyCrypto

From the PyCrypto PyPi page:

This is a collection of both secure hash functions (such as SHA256 and RIPEMD160), and various encryption algorithms (AES, DES, RSA, ElGamal, etc.). The package is structured to make adding new modules easy.

And from the PyCrypto GitHub repository:

One possible application of the modules is writing secure administration tools. Another application is in writing daemons and servers. Clients and servers can encrypt the data being exchanged and mutually authenticate themselves; daemons can encrypt private data for added security. Python also provides a pleasant framework for prototyping and experimentation with cryptographic algorithms; thanks to its arbitrary-length integers, public key algorithms are easily implemented.

Official resources

Installation

As the PyCrypto PyPi page suggests, an easy way to install PyCrypto is by using the following command.

pip install pycrypto

Examples

From the PyCrypto GitHub repository:

An example usage of the SHA256 module is:

>>> from Crypto.Hash import SHA256
>>> hash = SHA256.new()
>>> hash.update('message')
>>> hash.digest()
'\xabS\n\x13\xe4Y\x14\x98+y\xf9\xb7\xe3\xfb\xa9\x94\xcf\xd1\xf3\xfb"\xf7\x1c\xea\x1a\xfb\xf0+F\x0cm\x1d'

An example usage of an encryption algorithm (AES, in this case) is:

>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'
889 questions
13
votes
2 answers

Why does PyCrypto not use the default IV?

I am trying to figure out why my Python client and the Ruby server are having a disagreement about how to encrypt data. The only difference I see in the Ruby code and my code is that they are not specifying the Initialization Vector, therefore its…
zenWeasel
  • 1,889
  • 2
  • 22
  • 27
12
votes
3 answers

How to store a crypto key securely?

I'm looking at using a crypto lib such as pycrypto for encrypting/decrypting fields in my python webapp db. But encryption algorithms require a key. If I have an unencrypted key in my source it seems silly to attempt encryption of db fields as on my…
citronic
  • 9,868
  • 14
  • 51
  • 74
12
votes
1 answer

python pycrypto installation error

Possible Duplicate: PyCrypto and GMP library not found error [Mac OS 10.6.3] I'm trying to install pycrypto on ubuntu, but it throws error hom@PC71:~/Desktop/pycrypto-2.3$ sudo python setup.py build running build running build_py running…
shivg
  • 742
  • 1
  • 8
  • 28
12
votes
1 answer

IV must be 16 bytes long error in AES encryption

I am using pycrypto module for AES encryption. And using documentation I have write down the below function but it al;ways gives error IV must be 16 bytes long but I am using 16 byte long IV. def aes_encrypt(plaintext): """ """ key =…
user5594493
  • 1,050
  • 3
  • 10
  • 24
12
votes
5 answers

PyCrypto problem using AES+CTR

I'm writing a piece of code to encrypt a text using symmetric encryption. But it's not coming back with the right result... from Crypto.Cipher import AES import os crypto = AES.new(os.urandom(32), AES.MODE_CTR, counter = lambda :…
xster
  • 6,269
  • 9
  • 55
  • 60
12
votes
1 answer

AES decryption padding with PKCS5 Python

I have been trying to implement AES CBC decryption in Python. Since the ciphered text is not a multiple of 16bytes, padding was necessary. Without padding, this error surfaced "TypeError: Odd-length string" But I could not find a proper reference…
empyreanphoenix
  • 301
  • 2
  • 3
  • 8
11
votes
4 answers

How to verify in pycrypto signature created by openssl?

I've created private/public key in openssl, and signed some data: openssl genrsa -out private.pem 1024 openssl rsa -in private.pem -out public.pem -outform PEM -pubout echo 'data to sign' > data.txt openssl dgst -md5 < data.txt > hash openssl rsautl…
Ivan
  • 111
  • 1
  • 1
  • 4
11
votes
5 answers

Install paramiko on Windows

OK. I read installing paramiko on Windows. All mentioned methods simply do not work. Authors have different environments with different components/libraries installed. Or may be they don't test their solutions before posting an answer :) I removed…
tmporaries
  • 1,523
  • 8
  • 25
  • 39
11
votes
4 answers

Python's pycrypto library for random number generation vs os.urandom

I was trying to understand and figure out if I should use os.urandom() or Crypto.Random.new() for cryptographically secure pseudo-random numbers. The following website seems to suggest to use…
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
11
votes
2 answers

PyCrypto : AssertionError("PID check failed. RNG must be re-initialized after fork(). Hint: Try Random.atfork()")

I am creating various processes that do different tasks. One of them and only one of them, has a security module that creates the PyCrypto objects. So my program starts, creates the various processes, the process that handles messages uses the…
unixsnob
  • 1,685
  • 2
  • 19
  • 45
10
votes
3 answers

Implementing full RSA in Python

I am currently working on a project using python to implement p2p communication between two (or more) computers. Although I am pretty proficient with python, I am by no means an expert; programming and encryption are by no means my profession,…
Kevin C
  • 337
  • 3
  • 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
10
votes
2 answers

How do you extract N and E from a RSA public key in python?

I have a RSA public key such that it looks like -----BEGIN PUBLIC…
Konstantino Sparakis
  • 2,862
  • 3
  • 18
  • 30
10
votes
3 answers

ImportError: No module named 'Crypto'

I am working with pycrypto. It works fine on my local windows machine, but when I move it to my python box I get an error with importing the module: from Crypto.Cipher import ARC4 ImportError: No module named 'Crypto' The output of python3.3 -c…
crobject
  • 103
  • 1
  • 1
  • 4
10
votes
5 answers

Different Results in Go and Pycrypto when using AES-CFB

I am adding a go application to an already existing python codebase. I've been having trouble dealing with encryption between the languages. This is using go 1.2.1 and Python 2.7.x / PyCrypto 2.7a1. Here is the Python sample: import…
Daniel Drexler
  • 527
  • 4
  • 14