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'
845 questions
185
votes
12 answers

Encrypt & Decrypt using PyCrypto AES 256

I'm trying to build two functions using PyCrypto that accept two parameters: the message and the key, and then encrypt/decrypt the message. I found several links on the web to help me out, but each one of them has flaws: This one at codekoala uses…
Cyril N.
  • 35,568
  • 34
  • 119
  • 213
147
votes
19 answers

How do I install PyCrypto on Windows?

I've read every other google source and SO thread, with nothing working. Python 2.7.3 32bit installed on Windows 7 64bit. Download, extracting, and then trying to install PyCrypto results in "Unable to find vcvarsall.bat". So I install MinGW and…
Coldblackice
  • 3,302
  • 4
  • 17
  • 21
135
votes
26 answers

ImportError: No module named Crypto.Cipher

When I try to run app.py (Python 3.3, PyCrypto 2.6) my virtualenv keeps returning the error listed above. My import statement is just from Crypto.Cipher import AES. I looked for duplicates and you might say that there are some, but I tried the…
Graham Smith
  • 1,565
  • 2
  • 9
  • 11
64
votes
3 answers

Installing PyCrypto on Ubuntu - fatal error on build

Having looked at other similar threads, I still can't get pycrypto running. I'm trying to get it working on my Ubuntu laptop - but I couldn't manage it on my Windows PC either. I downloaded pycrypto-2.6, extracted it, and ran python setup.py…
OJFord
  • 8,132
  • 7
  • 52
  • 88
63
votes
10 answers

AttributeError: module 'time' has no attribute 'clock' in Python 3.8

I have written code to generate public and private keys. It works great at Python 3.7 but it fails in Python 3.8. I don't know how it fails in the latest version. Help me with some solutions. Here's the Code: from Crypto.PublicKey import RSA def…
user11576444
56
votes
10 answers

Pip error: Microsoft Visual C++ 14.0 is required

I just ran the following command: pip install -U steem and the installation worked well until it failed to install pycrypto. Afterwards I did the pip install cryptography command because I thought it was the missing package. So my question is, how…
Studentu
  • 735
  • 1
  • 5
  • 11
56
votes
6 answers

How to decrypt OpenSSL AES-encrypted files in Python?

OpenSSL provides a popular (but insecure – see below!) command line interface for AES encryption: openssl aes-256-cbc -salt -in filename -out filename.enc Python has support for AES in the shape of the PyCrypto package, but it only provides the…
Thijs van Dien
  • 6,123
  • 1
  • 26
  • 44
46
votes
5 answers

Pycrypto install fatal error: gmp.h file not found

It seems like there are a number of people who have had a similar problem, however, after much searching I haven't been able to find a solution that works with my particular architecture. I'm trying to install Pycrypto (as a subsidiary of Fabric)…
cosmosis
  • 5,117
  • 3
  • 28
  • 27
45
votes
4 answers

How to suppress a third-party warning using warnings.filterwarnings

I am using Paramiko in my python code (for sftp). Everything works fine except that everytime I import or call a paramiko function. This warning would show up: C:\Python26\lib\site-packages\Crypto\Util\randpool.py:40: RandomPool_Deprecation Warning:…
Tom Nguyen
  • 451
  • 1
  • 4
  • 3
40
votes
8 answers

Microsoft Windows Python-3.6 PyCrypto installation error

pip install pycrypto works fine with python3.5.2 but fails with python3.6 with the following error: inttypes.h(26): error C2061: syntax error: identifier 'intmax_t'
user1602
  • 1,542
  • 2
  • 14
  • 23
39
votes
6 answers

RSA encryption and decryption in Python

I need help using RSA encryption and decryption in Python. I am creating a private/public key pair, encrypting a message with keys and writing message to a file. Then I am reading ciphertext from file and decrypting text using key. I am having…
user4866424
29
votes
2 answers

Broken Pipe error when using pip to install pycrypto on Mac OS X

I am attempting to install pycrypto (version 2.3) on OS X via pip. I am getting a "Broken pipe" error when the compiler attempts to compile MD2.c. I get a very similar error when using easy_install. Here is the error that I am getting: bash-3.2$…
Mark Tomlin
  • 503
  • 1
  • 7
  • 9
29
votes
3 answers

PyCrypto not fully installed on Windows XP

I ran python setup.py install in a Windows XP console, and it reported as follows: running install running build running build_py running build_ext warning: GMP library not found; Not building Crypto.PublicKey._fastmath. building…
jay
  • 994
  • 2
  • 12
  • 20
28
votes
2 answers

No module named 'winrandom' when using pycrypto

I already spent 2 days trying to install pyCrypto for Paramiko module. So, first issue I had faced was this: >>> import paramiko Traceback (most recent call last): File "", line 1, in File "C:\Program…
vedburtruba
  • 1,069
  • 1
  • 9
  • 10
28
votes
1 answer

How to read a RSA public key in PEM + PKCS#1 format

I have a RSA public key in PEM format + PKCS#1(I guess): -----BEGIN RSA PUBLIC…
Mr.Teen
  • 571
  • 1
  • 6
  • 17
1
2 3
56 57