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

AES 128 in Python

I don't know why when I encrypt in AES a text with the PyCrypto (Crypto.Cipher- AES), the result isn't the same as the ciphertext generate by a code in C. For example, the following code gives me…
TomasG
  • 53
  • 1
  • 1
  • 4
5
votes
3 answers

Non-detached PKCS#7 SHA1+RSA signature without M2Crypto

I'm trying to create a non-detached signature on python3. I currently have code that does this on python2 with m2crypto, but m2crypto isn't available for python3. I've been trying rsa, pycrypto and openssl, but haven't seen to find how. Here's the…
WhyNotHugo
  • 9,423
  • 6
  • 62
  • 70
5
votes
1 answer

C# AES CBC PKCS7 to Python

I'm currently trying to translate a simple AES code from C# to Python. I know both of these languages pretty well, but I have no idea about the encryption field (especially AES). I wrote this AES code before in C#, but now I have no idea how to make…
Tim
  • 1,029
  • 2
  • 14
  • 23
5
votes
2 answers

RSA Encrypt and Decrypt between JS and Python(pycrypto)

I encrypt plain text from JS RSA Library(http://www-cs-students.stanford.edu/~tjw/jsbn/) and decrypt from python, Crypto.PublicKey. But, when I decrypt cipher text from JS with python, It has dummy text. I don't know why they are. So, I want clear…
yumere
  • 199
  • 1
  • 9
5
votes
2 answers

PyCrypto Possible To Check If File Already AES Encrypted?

from Crypto.Cipher import AES def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024): """ Encrypts a file using AES (CBC mode) with the given key. key: The encryption key - a…
user861555
5
votes
3 answers

Python AES implementations difference

I'm comparing AES implementations in Python from the pycrypto and the cryptography.io library. from cryptography.hazmat.primitives.ciphers import Cipher, modes, algorithms from cryptography.hazmat.backends import default_backend #…
marczellm
  • 1,224
  • 2
  • 18
  • 42
5
votes
4 answers

Pyinstaller & Pycrypto

We've recently added pycrypto to a project we've been working on and now I am unable to run the software after it is built with pyinstaller. I have had issues with new packages in pyinstaller, but I am unable to fix this one in particular. The…
horriblyUnpythonic
  • 853
  • 2
  • 14
  • 34
5
votes
1 answer

What is the difference between Pycrypto's Random.get_random_bytes and a simple random byte generator?

I came across this method in Pycrypto, which is used to generate random bytes: from Crypto import Random Random.get_random_bytes(5) I was wondering how this method is different from a simple generator like the following: import random def…
verybadalloc
  • 5,768
  • 2
  • 33
  • 49
5
votes
2 answers

How can I read a standard openssl rsa private key with PyCrypto and decrypt with it

I generated a private key with: openssl req -x509 -out anytime-pub.der -outform der -new -newkey rsa:2048 -keyout anytime.pem -days 3650 In my old code, I use M2Crypto load the key file to decrypt something, and it works. from M2Crypto import RSA…
Kizer
  • 135
  • 1
  • 9
5
votes
1 answer

decrypt a message with RSA public key with PyCrypto

I want to decrypt a message with RSA public key with PyCrypto I am useing code below but getting no private key error what should changed in code below? from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import…
alizx
  • 1,148
  • 2
  • 15
  • 27
5
votes
1 answer

hex string to SHA256 digest in python

I have a string that contain a SHA256 digest in hexadecimal like blow: "257612236efae809c23330ab67cf61f73aec938503f3ce126c34c6a32059f5f0" and I want to convert it to hash.digest() that can be like …
alizx
  • 1,148
  • 2
  • 15
  • 27
5
votes
3 answers

Why must all inputs to AES be multiples of 16?

I'm using the PyCrypto implementation of AES and I'm trying to encrypt some text (24 bytes) using a 24 byte key. aes_ecb = AES.new('\x00'*24, AES.MODE_ECB) aes_ecb.encrypt("123456"*4) I get this surprising error ValueError: Input strings must be…
Mark
  • 5,286
  • 5
  • 42
  • 73
5
votes
1 answer

First 8 byes of my encrypted data corrupting using 3DES and CBC

I'm using PyCrypto in an application to encrypt data, but for some reason the first 8 bytes (corresponding to the first block) are coming through corrupt no matter what I do. >>> from Crypto.Cipher import DES3 >>> from Crypto import Random >>> iv =…
rspeed
  • 1,612
  • 17
  • 21
5
votes
2 answers

Implement OpenSSL AES Encryption in Python

I'm trying to implement the following in Python: openssl enc -e -aes-256-cbc -base64 -k "Secret Passphrase" -in plaintext.txt -out ciphertext.txt openssl enc -d -aes-256-cbc -base64 -k "Secret Passphrase" -in ciphertext.txt -out…
PizzaPanther
  • 1,321
  • 2
  • 12
  • 18
5
votes
2 answers

Why is AES.decrypt not returning my original text?

I'm trying to use AES to safely store some passwords in a home-made password safe, but for some reason I'm not getting the original data back from AES.decrypt. Here is the code I'm testing with: from Crypto.Cipher import AES from Crypto.Hash import…
Hubro
  • 56,214
  • 69
  • 228
  • 381