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
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
  • 1,032
  • 2
  • 13
  • 20
27
votes
1 answer

How to set CFLAGS and LDFLAGS to compile pycrypto

I am trying to install the fabric library to an old machine. There are some legacy libraries in /usr/lib, such as libgmp. (py27)[qrtt1@hcservice app]$ ls /usr/lib|grep…
qrtt1
  • 7,746
  • 8
  • 42
  • 62
27
votes
3 answers

Signing and verifying data using pycrypto (RSA)

I am trying to familiarize myself with the pycrypto module, but the lack of clear documentation makes things difficult. To start with, I would like to understand signing and verifying data. Could someone please provide an example for how this would…
Noah McIlraith
  • 14,122
  • 7
  • 31
  • 35
27
votes
4 answers

PyCrypto on python 3.5

I found some PyCrypto installers for Python 3.3 and 3.4, but nothing for Python 3.5. When I try to install PyCrypton using pip install, it says: warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath. Is there any way…
Trsak
  • 303
  • 1
  • 4
  • 8
24
votes
2 answers

How do I use a X509 certificate with PyCrypto?

I want to encrypt some data in python with PyCrypto. However I get an error when using key = RSA.importKey(pubkey): RSA key format is not supported The key was generated with: openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key…
eshizhan
  • 4,235
  • 2
  • 23
  • 23
22
votes
8 answers

How do I create a user and set a password using ansible?

The documentation refers us to the github example, but this is a bit sparse and mysterious. It says this: # created with: # crypt.crypt('This is my Password', '$1$SomeSalt') password: $1$SomeSalt$UqddPX3r4kH3UL5jq5/ZI. but crypt.crypt doesn't emit…
Chris Sattinger
  • 4,446
  • 3
  • 30
  • 29
21
votes
5 answers

LINK : fatal error LNK1104: cannot open file 'python27.lib'

I was trying to compile pycrypto-2.6.1 from source for Python 2.7.10 64-Bit Windows Version and facing the following error. Processing pycrypto-2.6.1.tar.gz Writing…
Siva Arunachalam
  • 7,582
  • 15
  • 79
  • 132
19
votes
4 answers

ImportError: No module named Crypto

I am just starting to explore Python. I am trying to run an AES algorithm code and I am facing the: ImportError: No module named Crypto. How do you solve this?
AK1992
  • 211
  • 1
  • 2
  • 5
19
votes
8 answers

Using pycrypto, how to import a RSA public key and use it to encrypt a string?

The RSA public key: pubkey =…
Romstar
  • 1,139
  • 3
  • 14
  • 21
19
votes
2 answers

AES - Encryption with Crypto (node-js) / decryption with Pycrypto (python)

I'm writing this question + answer because I struggled a lot (maybe because of a lack of experience), got lost in many different ways of encrypting/decrypting things with node or python. I thought maybe my case could help people in the future. What…
nnaelle
  • 892
  • 1
  • 8
  • 22
19
votes
4 answers

Python Encrypting with PyCrypto AES

I just found pycrypto today, and I've been working on my AES encryption class. Unfortunately it only half-works. self.h.md5 outputs md5 hash in hex format, and is 32byte. This is the output. It seems to decrypt the message, but it puts random…
if __name__ is None
  • 11,083
  • 17
  • 55
  • 71
18
votes
14 answers

Trying to install pycrypto on Mac OSX mavericks

I am currently trying to install pycrypto and when I execute python setup.py build I receive this following error: cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.9-intel-2.7/src/_fastmath.o -lgmp -o…
user1798733
18
votes
5 answers

How can I create an encrypted django field that converts data when it's retrieved from the database?

I have a custom EncryptedCharField, which I want to basically appear as a CharField when interfacing UI, but before storing/retrieving in the DB it encrypts/decrypts it. The custom fields documentation says to: add __metaclass__ =…
Oved D
  • 7,132
  • 10
  • 47
  • 69
17
votes
3 answers

Saving RSA keys to a file, using pycrypto

I’m using PyCrypto 2.3 and I would like to save the keys I have generated into a file, so as to distribute them to the client and server. I can’t seem to find a way to print the keys correctly, neither can I find examples on the internet. def…
qdii
  • 12,505
  • 10
  • 59
  • 116
17
votes
3 answers

PyCrypto for Python3 in Alpine?

Is there a package for Alpine which allows me to install PyCrypto for Python 3? After encountering problems with pip3 install pycrypto, I stumbled upon this post which explains how to install numpy in Alpine using apk add py-numpy@testing. PyCrypto…
R J
  • 4,473
  • 2
  • 22
  • 29
1
2
3
59 60