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

Using RSA in Python

I am using RSA to encrypt/decrypt my session keys in Python. I am using Pycrypto library. After generating the keypair, I want to extract the private key and public key from that generated key and store them in different files. How can I do this? I…
Tara Singh
  • 1,821
  • 6
  • 28
  • 36
17
votes
3 answers

pycrypto on Ubuntu giving compiler error

I am trying to install pycrypto2.6 on Ubuntu 10.04 (Lucid Lynx) with Python 2.7.3. I am encountering the following error: running build running build_py running build_ext running build_configure checking for gcc... no checking for cc... no checking…
Pratibha
  • 1,730
  • 7
  • 27
  • 46
16
votes
1 answer

Can't install python module "pycrypto" on Debian lenny

I tried to install pycrypto module by downloading the source code and executing the following command python setup.py install, then an error came running install running build running build_py running build_ext warning: GMP library not found; Not…
alex CSD
  • 405
  • 2
  • 4
  • 11
16
votes
4 answers

SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

I cannot find anything on this. I'm getting error: Traceback (most recent call last): File "/path/to/pwdb.py", line 265, in password_db() File "/path/to/pwdb.py", line 73, in __init__ self.cipher = AES.new(key,AES.MODE_ECB) …
WaXxX333
  • 388
  • 1
  • 2
  • 11
16
votes
9 answers

Cryptography tools for python 3

I'm writing a program in python 3 which needs encryption functions (at least aes and rsa). I've found PyCrypto which seems to work only on 2.x versions. Is there any good tool available for python 3 or should I rather start translating my program to…
Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58
16
votes
1 answer

How to decrypt password from JavaScript CryptoJS.AES.encrypt(password, passphrase) in Python

I have a password which is encrypt from JavaScript via var password = 'sample' var passphrase ='sample_passphrase' CryptoJS.AES.encrypt(password, passphrase) Then I tried to decrypt the password comes from JavaScript in Python: from…
Bing
  • 378
  • 1
  • 3
  • 15
16
votes
2 answers

Diffie-Hellman (to RC4) with Wincrypt From Python

I am currently working on a project written in C++ that leverages the CryptoAPI to perform a Diffie-Hellman key exchange. I'm having a bit of trouble getting this to work as the eventual RC4 session key I get cannot be used to encrypt the same text…
Jeremy
  • 163
  • 5
16
votes
8 answers

PyCrypto install error on Windows

I am trying to install PyCrypto 2.6 Library on my computer. But I keep getting the following error D:\Software\Python\package\pycrypto-2.6>python setup.py build running build running build_py running build_ext warning: GMP or MPIR library not found;…
Khurram Majeed
  • 2,291
  • 8
  • 37
  • 59
14
votes
7 answers

from Crypto import Random -> ImportError: cannot import name Random

I have installed pycrypto (version 2.3) to /usr/local/lib/python2.6/dist-packages/Crypto/ and I am able to see the Random package there. But when I try to import the Crypto.Random, it pomps me that from Crypto.Random import * ImportError: No module…
Kevin
  • 2,191
  • 9
  • 35
  • 49
13
votes
3 answers

Is PyCrypto safe and reliable to use?

I am planning on using PyCrypto for a project and I want to know whether PyCrypto is safe and reliable enough to use. How can I ensure that PyCrypto is encrypting data correctly according to the various encryption algorithms such as RSA and AES?
Imran Azad
  • 1,384
  • 1
  • 21
  • 36
13
votes
2 answers

app engine: ImportError: No module named Crypto.Hash

I have a script that uses Crypto.Hash but import fails with error: ImportError: No module named Crypto.Hash in my sys.path if I print the sys.path list, there is this entry (among others):…
JackNova
  • 3,911
  • 5
  • 31
  • 49
13
votes
2 answers

pycrypto - Ciphertext with incorrect length

I've generated a public and private key with pycrypto, and I save them to a file using export key: from Crypto.PublicKey import RSA bits=2048 new_key = RSA.generate(bits, e=65537) prv =…
user2106070
  • 151
  • 1
  • 1
  • 7
13
votes
2 answers

Python PyCrypto encrypt/decrypt text files with AES

I already have a working program, but the only thing that doesn't work is the decrypt_file() function I have. I can still copy the encrypted text from the file and put it in my decrypt() function and have it work, but when I try to use my…
Zach King
  • 798
  • 1
  • 8
  • 21
13
votes
2 answers

Decrypt using an RSA public key with PyCrypto

As far as I understand, I should be able to use RSA to ensure authenticity or privacy, as I wish. In my case, I want to ensure authenticity so I encrypt the data with the private key and allow anyone to decrypt it with the public key. The data is…
sergiopereira
  • 2,026
  • 2
  • 23
  • 31
13
votes
2 answers

PyCrypto - How does the Initialization Vector work?

I'm trying to understand how PyCrypto works to use in a project but I'm not fully understanding the significance of the Initialization Vector (IV). I've found that I can use the wrong IV when decoding a string and I still seem to get the message…
Tim Tisdall
  • 9,914
  • 3
  • 52
  • 82
1 2
3
59 60