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

python encrypt big file

This script is xor encrypt function, if encrypt small file, is good ,but I have tried to open encrypt a big file (about 5GB) error information: "OverflowError: size does not fit in an int" ,and open too slow. Anyone can help me optimization my…
vinson
  • 15
  • 4
0
votes
1 answer

Crypto is not support python 3.x?

when I use python3.x , run code: with open('rsa_public_key.pem') as f: key = f.read() rsakey = RSA.importKey(key) cipher = Cipher_pkcs1_v1_5.new(rsakey) cipher_text = base64.b64encode(cipher.encrypt(aes_key)) str1 =…
gassy
  • 35
  • 1
  • 5
0
votes
1 answer

How to set the right data type block size for encryption

I'm trying to encrypt some plain text with the use of a private and public key. I'm using python and this is what I'm working with to start. from hashlib import md5 from base64 import b64decode from base64 import b64encode from Crypto import…
MadeInDreams
  • 1,991
  • 5
  • 33
  • 64
0
votes
3 answers

A java server use SHA256WithRSA to sign message, but python can not verify

Here is the code, the second (message, signature and public key) works fine on Java, can verify the message. But when I am using python, it will failed. If am signed the message and the code will verify the message correctly. Would some one help me…
boykimi
  • 1
  • 1
0
votes
0 answers

ModuleNotFoundError: No module named 'Crypto' ubuntu 18 python 3.6.7

I have a python version 3.6.7 installed in ubuntu 18 and I'm trying to run code that has 'import crypto' in it. whenever I run the code the following error occurs: ModuleNotFoundError: No module named 'Crypto' I've tried to install Crypto and…
Rasool Ahmed
  • 175
  • 1
  • 4
  • 13
0
votes
0 answers

PyCrypto Signature Verfication failing with other languages

I have been trying to do signature generation to send the encrypted message to server. But server is not able to verify the signature. Similarly, I am not able to verify the signature sent by server. My code is in python2.7 and I am using PyCrypto…
0
votes
1 answer

Have an error with AES key after his RSA decryption

I'm trying to realize hybrid cryptosystem with pycryptodome. from Crypto.PublicKey import RSA from Crypto.Cipher import AES from Crypto.Random import get_random_bytes from Crypto.Random.random import getrandbits from Crypto.Cipher import…
maribor
  • 31
  • 5
0
votes
0 answers

Mix net implementation with RSA in python

I'm trying to implement Mixnet in Python. Consider the example in the linked wiki page — A encrypts a message with A's public key and then encrypts the resulting ciphertext along with B's address with M's public key. When I run my code which…
captain
  • 815
  • 14
  • 26
0
votes
1 answer

Checking hash sums with same data => Always 2 different sums

I am working on a Proof-of-Work system in Python. For generating hash value I am using this function from Cryptodome.Hash import SHA256 def blockhash(sender, msg, signature, nonce, timestamp): h = SHA256.new() hash_str = str(sender) +…
Emil Engler
  • 489
  • 5
  • 14
0
votes
1 answer

ImportError: cannot import name '_AES' after upgraded to Python 3.6

I recently upgraded Linux Mint to 19.1 X64 with Python 3.6 from Linux Mint 18.3 X64 (with Python 3.5.2), and tried from Crypto.Cipher import AES I first got, ImportError: No module named apt_pkg fixed it by sudo ln -s…
daiyue
  • 7,196
  • 25
  • 82
  • 149
0
votes
0 answers

PyCrypto AES-CTR mode get different output

I use PyCrypto to encrypt my data, but why theirs output is different? from Crypto.Cipher import AES from Crypto.Util import Counter data = b'\x02\x01\xf2\xca\x04\x03\x02P\x02\x02\x01\x80\xd0\x0f\x80\xd0\x0f' key = b'random 16 string' nonce =…
sunrise
  • 1
  • 2
0
votes
1 answer

Trailing garbage after Python decryption of JavaScript AES-GCM

I have JavaScript tied to an HTMl button, which does an AES-GCM encoding and logs the useful info to the console. function strToArrayBuffer(str) { let buf = new ArrayBuffer(str.length * 2); let bufView =…
simpleuser
  • 1,617
  • 25
  • 36
0
votes
0 answers

Issue when installing pycrypto

I am attempting to install pycrypto I have followed multiple other threads, currently I have upgraded pip, setuptools and installed Microsoft Visual Studio 2015. However I am still getting errors when running the setup. I am installing pycrypto via…
Ogden
  • 477
  • 1
  • 8
  • 21
0
votes
0 answers

Pycrypto AES-256 - How to Encrypt Texts with Large Bytes?

I've been playing around with pycrypto and AES-256, and I've run into a bit of an issue with larger file sizes. For small strings, like "hello world!", the encryption/decryption works fine. But I would like to extend the encryption to text files of…
LeetCoder
  • 563
  • 1
  • 5
  • 17
0
votes
1 answer

I am trying to use pycrypto to learn about AES and I do not know what I am doing wrong

This is the code I am using to teach myself how AES works. It is supposed to produce the encrypted message an then produce the decrypted message right after it. If anyone can help me figure out what I did wrong, it would be greatly…
Max
  • 1
  • 4