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

PyCrypto Dependency issue with py3compat

I have pycrypto 2.6.1 installed but I still get this error when trying to importRSA. >>> from Crypto.Cipher import AES >>> from Crypto.PublicKey import RSA Traceback (most recent call last): File "", line 1, in File…
summerNight
  • 1,446
  • 3
  • 25
  • 52
0
votes
1 answer

CBC decryption using ECB function

Trying to solve Cryptopals Challenge 10 where have to CBC decrypt a text file against "YELLOW SUBMARINE" with an IV of all ASCII 0 (\x00\x00\x00 &c). Link to the text file is following: http://cryptopals.com/static/challenge-data/10.txt I have…
Manahil
  • 265
  • 2
  • 16
0
votes
0 answers

Unable to cipher non english text

I use this code to encrypt string. When I encrypt string which contains only english letters - it works fine, but when I try to encrypt cyrillic text it throws an exception. import base64 from Crypto.Cipher import AES def encrypt(text): …
changer
  • 329
  • 2
  • 4
  • 19
0
votes
2 answers

Getting ImportError: No module named 'Crypto' after installation

I am getting ImportError: No module named 'Crypto' error when trying to run. I have installed pycrypto using pip install pycrypto and updated it also. Everything I have tried to far has been unsuccessful. Tried: reinstalling pycrypto, updating…
Choncy
  • 3
  • 1
  • 8
0
votes
1 answer

Encrypting message containing public key with another public key

I have two asymmetric key pairs client_key, A and another_key, B. I want to send a encrypt a dict with B's public key. However, the dict must contain A's public key, like the following example: message_dict = { 'hostname': socket.getfqdn(), …
Matt
  • 73
  • 7
0
votes
1 answer

ValueError when reading RSA key from .PEM file using ImportKey

This documentation of PyCrypto for RSA https://www.dlitz.net/software/pycrypto/api/current/Crypto.PublicKey.RSA-module.html mentions this how we are supposed to write and read keys to and from .PEM : from Crypto.PublicKey import RSA key =…
daipayan
  • 319
  • 3
  • 5
  • 17
0
votes
1 answer

python Pycrypto how can I encrypt files and not corrupt them

I've been dealing with this problem for quite some time now. When I try to encrypt a file with PyCrypto. I can encrypt and decrypt it (only works with images so far). The problem is that the image becomes corrupted when I encrypt it and try to…
0
votes
1 answer

Cannot decrypt blob send by Java server in python client

I have the following workflow. Use openssl to generate rsa keypair Post public key to java server Server constructs in memory public key Server encrypts text with public key and sends blob back Python decrypts blob with private key Generate…
amrka
  • 49
  • 1
  • 6
0
votes
1 answer

Decryption issue in python using AES algorithm

I am building website in python flask & using AES algorithm of pycrypto library. In sign up web page, I am saving encrypted pwd & encrypted key in text file. In login page, I am comparing entered pwd with decrypted pwd,using below code def…
Ashish Bainade
  • 426
  • 5
  • 11
0
votes
1 answer

Python3 PyCrypto produce a Valueerror when decrypt long message

Hy, i must decrypt a DES encrypted message with DES and XOR. When i decrypt a 24 bytes long message, it runs correctly. But with 40 bytes and more, produce an error like this: result4 =…
Brawn1
  • 5
  • 2
0
votes
0 answers

Encryption and decryption of a string in python

I am trying to hide an actual string. So, I am choosing encoding mechanism to encode it. Is there any best Encryption/Decryption Algorithms to deal with it ? How can I do it in Python? The encoded string should be of length 10 (encoded string should…
Jackie
  • 129
  • 17
0
votes
1 answer

Pycrypto 2.6.1 for android smartphone

I am doing a project where I am required to do an encryption on Windows system and then I need to decrypt the message on an Android smartphone. I am using Python with pycrypto 2.6.1 module on Windows and qpython app on the smartphone. Qpython is not…
0
votes
0 answers

AES cipher process creation

I have a program that ciphers and deciphers a file, a file named a1, get transformed into a file named a1.enc, and it works properly, my problem is the -p option, which is supposed to define the number processes used to make the ciphers, if i used…
0
votes
2 answers

Encrypt in PHP and decrypt in Python or openssl command line

A lot of data in a database I took over contains encrypted fields. The method used to encrypt the data is the following PHP code:
rthill
  • 143
  • 3
  • 11
0
votes
1 answer

how to display encrypted image as an image without decrypting it

#encrypting an image using AES import binascii from Crypto.Cipher import AES def pad(s): return s + b"\0" * (AES.block_size - len(s) % AES.block_size) filename = 'path to input_image.jpg' with open(filename, 'rb') as f: content =…
baconSoda
  • 503
  • 2
  • 5
  • 15