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
10
votes
5 answers

_fastmath error in python: HAVE_DECL_MPZ_POWM_SEC

I am running python 2.6 on Red Hat 6.4 I had a script running on one machine using paramiko sftp. When I transferred it to another machine I got an exception: SFTP file has no attribute __exit__ (something along those lines). I decided to upgrade…
whomaniac
  • 1,258
  • 4
  • 15
  • 22
10
votes
2 answers

SignedJwtAssertionCredentials on AppEngine doesn't recognize PEM key

SignedJwtAssertionCredentials on appengine (with pycrypto 2.6) doesn't support the PKCS12 format, therefore I'm trying to use PEM keys instead, as suggested everywhere.. this is my code: f = file(os.path.join(os.path.dirname(__file__), KEY_FILE),…
luca
  • 12,311
  • 15
  • 70
  • 103
10
votes
5 answers

Chmod not recognized as internal or external command

Trying to get pycrypto installed in Windows. At the command prompt, I type python setup.py build. Things start out ok, but when it gets to 'running build_configure', I get this error message and things come to a halt: chmod not recognized as…
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
10
votes
1 answer

PyCrypto generates bad signatures

I'm having major problems with PyCrypto, as the code below demonstrates. One issue is that the test case does not fail in a repeatable way, but has different causes on different platforms using different keys. Note that the test case provides two…
simonhaines
  • 481
  • 7
  • 22
9
votes
3 answers

No module named Crypto.Cipher on local mac AppEngine

No module named Crypto.Cipher when I try to import from Crypto.Cipher import AES My folder structure looks like this: test/ test/main.py test/pycrypto-2.3 I ran the build and install inside of the pycrypto folder but I keep getting the error, any…
Chris
  • 2,739
  • 4
  • 29
  • 57
9
votes
3 answers

python: module 'Crypto.Cipher.AES' has no attribute 'MODE_CCM' even though pycrypto installed

pycrypto is installed (when I run pip list one of the result is pycrypto (2.6.1)) and it works but when I would like to use the MODE_CCM it returns: module 'Crypto.Cipher.AES' has no attribute 'MODE_CCM' My Python version: Python 3.5.2 :: Anaconda…
John
  • 4,786
  • 8
  • 35
  • 44
8
votes
2 answers

Fatal error when trying to install PyCrypto on OS X El Capitan

I am trying to install PyCrypto on OS X 10.11.3 (El Capitan). I am using Python 3.5.1. I downloaded the gzip file from https://pypi.python.org/pypi/pycrypto and decompressed it. Then I ran python setup.py build like the instructions said and it…
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
8
votes
3 answers

Encrypt in python - decrypt in Javascript

I have need to simply encrypt some text in python and being able to decrypt in JavaScrypt. So far I have in python: from Crypto import Random from Crypto.Cipher import AES import base64 BLOCK_SIZE = 16 key = "1234567890123456" # want to be 16…
bensiu
  • 24,660
  • 56
  • 77
  • 117
8
votes
2 answers

Why can I encrypt data with one DES key and successfully decrypt with another?

I tried to implement DES algorithm using pyDes and Crypto.Cipher.DES modules. I found a problem that when I encrypt with 82514145 key and then decrypt the cipher with 93505044 I can retrieve the decrypted text. I found 256 keys behaving like this.…
ceasif
  • 345
  • 2
  • 14
8
votes
3 answers

I have modulus and private exponent. How to construct RSA private key and sign a message?

I am newbie in cryptography and pycrypto. I have modulus n and private exponent d. From what I understand after reading some docs private key consists of n and d. I need to sign a message and I can't figure out how to do that using pycrypto.…
Maxim
  • 1,783
  • 2
  • 14
  • 24
7
votes
1 answer

PyCrypto: Generate RSA key protected with DES3 password

I have been able to create a RSA key protected by password with DES3 (well... I think because I'm very new to this encryption world) by using the command: openssl genrsa -out "/tmp/myKey.pem" -passout pass:"f00bar" -des3 2048 Now, I would like to…
Savir
  • 17,568
  • 15
  • 82
  • 136
7
votes
3 answers

SHA1 hash differ between openssl and hashlib/pycrypto

Why does the hash from using openssl differ from the ones I get in python? $ echo "Lorem ipsum" | openssl dgst -sha1 -hex (stdin)= d0c05753484098c61e86f402a2875e68992b5ca3 $ python >>> from hashlib import sha1 >>> sha("Lorem…
ib.lundgren
  • 1,524
  • 14
  • 15
7
votes
4 answers

problems with easy_install pycrypto

I'm trying install pycrypto on osx with easy_install and I'm getting the following error: easy_install pycrypto Searching for pycrypto Reading http://pypi.python.org/simple/pycrypto/ Reading http://pycrypto.sourceforge.net Reading…
Julie
  • 668
  • 9
  • 9
7
votes
1 answer

RSA sign a string with private key in python

I am communicating with our clients server. For an api I need to sign a string with my private key. They have the following condition to follow User SHA 256 algorithm to calculate the hash of the string Use the private key and RSA (PKCS1_PADDING)…
varad
  • 7,309
  • 20
  • 60
  • 112
7
votes
1 answer

How to generate the PEM serialization for the public RSA/DSA key

Using PyCrypto I was able to generate the public and private PEM serialization for a RSA key, but in PyCrypto the DSA class has no exportKey() method. Trying PyOpenSSL I was able to generate the private PEM serialization for RSA and DSA keys, bu…
Adi Roiban
  • 1,293
  • 2
  • 13
  • 28