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 equivalent for RSA.SignData in .NET

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.FromXmlString(PrivateKey); string data = "SAMPLE TEXT."; byte[] signedData = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider()); string signedString =…
mahyard
  • 1,230
  • 1
  • 13
  • 34
0
votes
0 answers

How can I encrypt the same message on different machines?

I'm working with some sensitive data and need to encrypt identification numbers (9 digits). In the future, I may have to get more data from where it is currently stored and add it to a database. I want the encryption to be so such that if I add an…
Demetri Pananos
  • 6,770
  • 9
  • 42
  • 73
0
votes
1 answer

Encrypting and Decrypting with python and nodejs

I am trying to pass data between Python and Node.js application. For that i am using AES encryption. The problem is that Node.js produces encrypted data which is twice longer than the one produced using Python. Below are code snippets. Python…
Termos
  • 664
  • 1
  • 7
  • 31
0
votes
1 answer

Python RSA file encrypt-decrypt result varies with large files

I know that RSA can't encrypt more than 128 bytes at a time (modulus), so I am encrypting and decrypting the file in chunks. However, if my file is larger than a few kb, the result changes everytime I run the program. Sometimes the whole file is…
Chuque
  • 587
  • 1
  • 5
  • 22
0
votes
2 answers

Python - Installing Pycrypto package

I am using Pycharm and i need to install a package called pycrypto. But when i tried it is giving an error like Collecting pycrypto Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(,…
0
votes
1 answer

SHA1withRSA verify in python?

I want to rewrite the code of Java to Python.The code is used to verify data through a .cer file. public static boolean verifyByRSA(String certPath, byte[] aPlainData, byte[] aSignature) { boolean tResult = false; try { …
mutoulbj
  • 3
  • 3
0
votes
0 answers

Python DES encryption strange behavior

I have a text file containing the following text: message.txt this is my secret message I wrote a small python code to read that file and encode it using Crypto.Cipher from Crypto.Cipher import DES f = open('message.txt','rb') text =…
Ahmad
  • 12,336
  • 6
  • 48
  • 88
0
votes
0 answers

Pip install error on Mac for PyCryptodome

I've tried installing pycryptodome, pycrypto, and pycryptodomex and none of them seem to be downloading. I keep getting this pip install error: error: command '/usr/bin/clang' failed with exit status 1 Command …
Michael A.
  • 11
  • 1
  • 7
0
votes
0 answers

Using PyCrypto to calculate SHA256 hash of RSA public key

I used OpenSSL to generate a 2048-bit RSA key: openssl genrsa -out mykey.pem 2048 openssl rsa -pubout -in mykey.pem -out mypubkey.pub I would like to use OpenSSL to calculate the SHA256 hash value of the public key (not the file, just the key) I…
bfigure8
  • 1
  • 2
0
votes
1 answer

Python code not stopping, getting stuck at a certain point

I have written a small piece of code to encrypt a file and decrypt it. However It is getting stuck somewhere, I think it is after it initially decrypts it as it creates the encrypted file but does not print 'Encrypted!'. What am I doing wrong? Is it…
SDurrani
  • 33
  • 5
0
votes
0 answers

PyCrypto in Python, cannot choose what file to open

I am fairly new to python and pycrypto. I read many guides and manuals on pycrypto and I have come up with a small bit of code. I don't understand how to choose which files I want to encrypt. Is it supposed to be defined somewhere? My code: import…
user6288190
0
votes
1 answer

Error when trying to install paramiko and cryptography in Kivy Buildozer

I am using a Kivy as a Virtual Machine (in MAC OS). I am trying to install the python packages pycrypto and paramiko. Starting with pycrypto, I tried pip install pycrypto Requirement already satisfied: pycrypto in…
0
votes
1 answer

RSA : decrypt openSSL certificate

I am trying to decrypt an encrypted openSSL certificate using the Crypto library. For that, I use the following function : def decryptMessage(privateKeyString, encryptedMessage): print 'Enter private key pass phrase' passPhrase =…
rafatic
  • 223
  • 1
  • 10
0
votes
1 answer

Python encrypt/decrypt txt with a given password

I have a custom account manager. At the moment I store the whole information in a single txt document. I'm trying to figure out how to encrypt all the whole txt with a password introduced by user. I've seen PyCrypto but I have so many problems…
Lechucico
  • 1,914
  • 7
  • 27
  • 60
0
votes
0 answers

ValueError: Message too large

Client.py import socket from Crypto.Cipher import AES import base64 import os import Crypto import os, shutil import sys from stat import ST_SIZE sys.path.insert(0, '/home/ubuntu/finalpro/server') import keygen # Create a…