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

Securing a value in python properties file

I am writing a python script which does some database operations. I have kept the database credentials in a config file and reading via RawConfigParser in python. So for securing database password, is there any way I can encrypt just the database…
ab_
  • 377
  • 2
  • 5
  • 16
0
votes
1 answer

From M2Crypto to pyCrypto

I'm trying to port an M2Crypto function to pyCrypto or any other solution, 'cause the last version of M2Crypto doesn't work with pypy. So, i'm trying to port an existing code from…
luisurrutia
  • 586
  • 6
  • 21
0
votes
0 answers

Python transfer encrypted image using CRYPTO AES

I want to know how I can encrypt and decrypt file transfer by using crypto AES encryption. In this example I can transfer a print screen but I need to crypte sending and decrypt recieving. Client: import socket import sys from PIL import…
nater303
  • 85
  • 2
  • 14
0
votes
1 answer

Why does pycrypto return the same encryption result when using two different public keys?

I've generated two key pairs with openssl: openssl genpkey -algorithm RSA -out ndkey.pem -pkeyopt rsa_keygen_bits:1023 -pkeyopt rsa_keygen_pubexp:3 I using pycrypto to encrypt cipher with primitive RSA for two public key: key1 =…
0
votes
2 answers

Why I am not able to decrypt what I encrypted with pycrypto?

Here's my code: Encrypt: from Crypto.Cipher import AES import base64 def encryption (privateInfo): BLOCK_SIZE = 16 PADDING = '{' pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING EncodeAES = lambda c, s:…
user5835566
0
votes
1 answer

Encrypting user input in Python with pyCrypto

I am trying to do basic encryption in Python, in below program I encrypt whatever user types in, and then I am displaying it back to the user after decryption. I am using the pyCrypto library which I downloaded from here:…
jubins
  • 1
  • 1
  • 2
0
votes
1 answer

TypeError: 'str' does not support the buffer interface when using PyCrypto.AES

I am trying to do some experimenting in encrypting and decrypting using PyCrypto.AES when I try to decrypt it gives me TypeError: 'str' does not support the buffer interface I found some solutions where I have to encode or use string, but I couldn't…
0
votes
0 answers

How to use pyCrypto in order to encrypt and decrypt files

I have the following code written in python and a few lines in the bottom to debug and execute the code: class encoder: encryptor = False IV = 16 * '\x00' def __init__(self, password): self.encryptor =…
Isdj
  • 1,835
  • 1
  • 18
  • 36
0
votes
0 answers

C# RSAPKCS1SignatureFormatter in Python

I've tried many times over to get C# and Python to sign the same the same way but have failed in all my attempts, is there something that I'm not doing correctly? C#: RSAPKCS1SignatureFormatter formatter = new…
0
votes
1 answer

Python - How to exchange AES key over socket

I'm trying to make a client which gets an AES key from a sever over socket. both client and server have this code: import base64 from Crypto.Cipher import AES from Crypto import Random BS = 16 pad = lambda s: s + (BS - len(s) % BS) * chr(BS -…
iYonatan
  • 916
  • 3
  • 10
  • 26
0
votes
0 answers

AES encryption of file using Pycrypto

I am trying to find the correct way to encrypt files using Pycrypto. Every method I have found online doesn't seem to decrypt in Android or online at sites like http://aes.online-domain-tools.com/ Here is the Python code I have tried: from Crypto…
James
  • 693
  • 1
  • 13
  • 27
0
votes
3 answers

Howto generate TrueLicence in Python

We have a licencing server which generates keys using the Java TrueLicense library. I would like to move that code to a Python using the same algorithm so that the new keys will be equivalent with keys generated with the Java code. Perhaps it is…
pjesi
  • 3,931
  • 3
  • 20
  • 16
0
votes
1 answer

TypeError happens with Nginx but not with Flask Server

I am running an api behind flask in python and connections are handled through nginx and uwsgi. Some of the api routes use pycrypto but am getting errors when using nginx on port 80 regarding this line in the pycrypto source. The full traceback…
internetwhy
  • 635
  • 1
  • 6
  • 19
0
votes
1 answer

How to import RSA keys by command line argument?

I have been using Pycrypto module to generate RSA public/private key pair and trying to import keys by sending the public key as command line argument: from Crypto.PublicKey import RSA from Crypto import Random import sys input_key =…
Manuj Mittal
  • 97
  • 1
  • 2
  • 12
0
votes
1 answer

Python Crypto blinding big messages

I tried to blind some big message using pythons RSA from Crypto.PublicKey. The problem is, even if i generate big key, like 6400 bits, key.blind() method still crushes with "message too large" error. I know, that my message can't be bigger than N in…
user3387666
  • 237
  • 1
  • 3
  • 9