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
2 answers

AES encryption/decryption between Ruby-OpenSSL, PyCrypto

I have to encrypt a piece of text using Ruby. I've used the Ruby-Openssl gem for that purpose. This encrypted text is them passed to a python program using which I have to decrypt it. I've used Pycrypto for the purpose. The problem is, in Pycrypto…
vishy1618
  • 493
  • 3
  • 7
5
votes
0 answers

OpenSSL's command for Partial Chain in Python

I'm trying to use OpenSSL module in Python for certificate validation. I have window's openssl command as: openssl verify -partial_chain -CAfile Intermediate.pem UserCert.pem Will you please suggest me equivalent of this in Python? Requirement: This…
Bogota
  • 401
  • 4
  • 15
5
votes
1 answer

AES 256 Encryption -> Changing inititialization vector slightly changes decrypted message without knowing key

I made the following observation with an AES encrypt/decrypt example which is very counter intuitive for me. I tried to encrypt and the decrypt a simple payload with AES in CBC mode. My understanding is/was that the initialization vector does not…
DanEEStar
  • 6,140
  • 6
  • 37
  • 52
5
votes
1 answer

Python Cryptography export key to DER

In the past when using PyCrypto I was able to do the following to generate a fingerprint of a RSA public key: rsa_cipher = PKCS1_v1_5.new(RSA.importKey(pub_rsa_key)) hashlib.sha1(rsa_cipher._key.exportKey("DER")).hexdigest() How can I achieve the…
McAbra
  • 2,382
  • 2
  • 21
  • 29
5
votes
2 answers

How to encrypt messages with ECC in pycryptodom

I'm using Hybrid encryption(RSA+AES) but the length was large and now i want to use ECC instead of RSA, but in pycryptodom there is no implementation for that.. this is my RSA code def generate_keys(): key = RSA.generate(1024) private_key =…
Ahmed Salama
  • 111
  • 2
  • 6
5
votes
1 answer

ModuleNotFoundError: No module named 'Crypto'

I installed Crypto module and SHA256 but showing ModuleNotFoundError :- Traceback (most recent call last): File "Digitalsig.py", line 1, in from Crypto.Hash import SHA256 ModuleNotFoundError: No module named 'Crypto' Here is the…
Rahul Thakur
  • 77
  • 1
  • 1
  • 2
5
votes
0 answers

private encrypt (generate signature) using cryptography/ pycrypto as generated by M2Crypto

I am trying to generate a signature for a payload. My tech stack is in Python3.6, which provides library like cryptography or pycrypto. Problem is, I am unable to recreate the function private_encrypt of M2Crypto in libraries like cryptography. The…
Abhishek
  • 2,543
  • 4
  • 34
  • 46
5
votes
0 answers

Python pyCrypto PKCS1 OAEP to Java Cipher

I have been given some Python code from the backend that decrypts some data. On my side, the Android app, I need to also decrypt it. Here are some snippets of the Python code which I believe are the most relevant. cipher =…
Ersen Osman
  • 7,067
  • 8
  • 47
  • 80
5
votes
1 answer

PyCryptodome Error: MAC Check Failed

I am working on an encryption program with Pycryptodome in Python 3. I am trying to encrypt a (byte) string and then decrypt it and verify the MAC tag. When I get to verify it, an error is thrown. This is the code: from Crypto.Cipher import AES from…
purple_dot
  • 106
  • 2
  • 9
5
votes
1 answer

Generated encrypted string does not have same size in PyCrypto and Crypto++

I recently used a Python script to encrypt a string. But could not decrypt it in C++ using Crypto++. I just compared generated encrypted strings and found they are not the same. Anybody can help? Here is my Python code: key =…
user562549
  • 181
  • 3
  • 12
5
votes
3 answers

PyCrypto installation error (vc\cl.exe fails no matter what)

I am trying to install pyCrypto (by pip3 install pycrypto) , and I keep getting this error : Failed building wheel for pycrypto Running setup.py clean for pycrypto Failed to build pycrypto Installing collected packages: pycrypto Running…
Alex Walker
  • 91
  • 2
  • 9
5
votes
1 answer

How can I use Keystore in Android Native Code?

I need to encrypt the some file which are created by the android native service written in C++. I have to use Keystore but I dont know how to use. is there any libraries or document?
5
votes
1 answer

problems with installation pycrypto in python 3.6

I've tried to install pysnmp (which uses pycrypto) on python 3.6.0 using pip, but I've got an error: Command ""c:\program files (x86)\python36-32\python.exe" -u -c "import setuptools, tokenize;file='C: …
5
votes
1 answer

Encrypt in python and decrypt in Java with AES-CFB

I am aware of a question very similar to this (How do I encrypt in Python and decrypt in Java?) but I have a different problem. My problem is, I am not able to decrypt in Java correctly. Despite using the correct key and IV, I still get garbage…
Srikanth Kandalam
  • 965
  • 3
  • 15
  • 26
5
votes
1 answer

Extract Publickey from Privatekey input using Python

I need to generate publickey from a private key without temporary location locally like we do in sshgen.So i use this.Here iam passing my private key as input like this(while executing): python codekey.py "-----BEGIN RSA PRIVATE…
Doefi
  • 79
  • 1
  • 4
  • 10