EzDevInfo.com

pycrypto

The Python Cryptography Toolkit PyCrypto - The Python Cryptography Toolkit

How do I install PyCrypto on Windows?

I've read every other google source and SO thread, with nothing working.

Python 2.7.3 32bit installed on Windows 7 64bit. Download, extracting, and then trying to install PyCrypto results in "Unable to find vcvarsall.bat".

So I install MinGW and tack that on the install line as the compiler of choice. But then I get the error "RuntimeError: chmod error".

How in the world do I get around this? I've tried using pip, which gives the same result. I found a prebuilt PyCrypto 2.3 binary and installed that, but it's nowhere to be found on the system (not working).

Any ideas?


Source: (StackOverflow)

Broken Pipe error when using pip to install pycrypto on Mac OS X

I am attempting to install pycrypto (version 2.3) on OS X via pip. I am getting a "Broken pipe" error when the compiler attempts to compile MD2.c. I get a very similar error when using easy_install.

Here is the error that I am getting:

bash-3.2$ 
bash-3.2$ sudo pip install pycrypto
Password:
Downloading/unpacking pycrypto
  Running setup.py egg_info for package pycrypto
Installing collected packages: pycrypto
  Running setup.py install for pycrypto
    warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
    building 'Crypto.Hash.MD2' extension
    gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -fwrapv -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.6-universal-2.6/src/MD2.o
    /usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed
    Installed assemblers are:
    /usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64
    /usr/bin/../libexec/gcc/darwin/i386/as for architecture i386
    src/MD2.c:134: fatal error: error writing to -: Broken pipe
    compilation terminated.
    lipo: can't open input file: /var/tmp//cc47qHNI.out (No such file or directory)
    error: command 'gcc-4.2' failed with exit status 1
    Complete output from command /usr/bin/python -c "import setuptools;__file__='/Users/damusin/tmp/pycrypto/pycrypto-2.3/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-W6xQ61-record/install-record.txt:
    running install

running build

running build_py

running build_ext

warning: GMP library not found; Not building Crypto.PublicKey._fastmath.

building 'Crypto.Hash.MD2' extension

gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -fwrapv -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.6-universal-2.6/src/MD2.o

/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed

Installed assemblers are:

/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64

/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386

src/MD2.c:134: fatal error: error writing to -: Broken pipe

compilation terminated.

lipo: can't open input file: /var/tmp//cc47qHNI.out (No such file or directory)

error: command 'gcc-4.2' failed with exit status 1

----------------------------------------
Command /usr/bin/python -c "import setuptools;__file__='/Users/damusin/tmp/pycrypto/pycrypto-2.3/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /tmp/pip-W6xQ61-record/install-record.txt failed with error code 1
Storing complete log in /Users/admin/.pip/pip.log
bash-3.2$ 
bash-3.2$ 

I attempted to install this package using mac ports with the command "sudo port install py26-crypto", and looks to install smoothly but then it tells me that the pycrypto package is missing when running the command "fab" (Fabric).

I attempted using the "ARCHFLAGS" command recommended in answer by YH Wong here but no luck: http://superuser.com/questions/259278/python-2-6-1-pycrypto-2-3-pypi-package-broken-pipe-during-build

I am using Mac OS X 10.6.7 with python 2.6.6 and XCode is installed.


Source: (StackOverflow)

Advertisements

PyCrypto not fully installed on Windows XP

I ran python setup.py install in a Windows XP console, and it reported as follows:

running install
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Random.OSRNG.winrandom' extension
error: None

When I try to run a script with import Crypto.Cipher.AES, it doesn't work, saying:

ImportError: cannot import name AES

How can I fix this?


Source: (StackOverflow)

Using RSA in Python

I am using RSA to encrypt/decrypt my session keys in Python. I am using Pycrypto library. After generating the keypair, I want to extract the private key and public key from that generated key and store them in different files. How can I do this? I can see the has Private method which can tell that the generated keypair has private component but not able to find how to extract both the keys from this generated keypair. Any Suggestion would be of great help.


Source: (StackOverflow)

Signing and verifying data using pycrypto (RSA)

I am trying to familiarize myself with the pycrypto module, but the lack of clear documentation makes things difficult.

To start with, I would like to understand signing and verifying data. Could someone please provide an example for how this would be written?


Source: (StackOverflow)

Encrypt & Decrypt using PyCrypto AES 256

I'm trying to build two functions using PyCrypto that accept two parameters: the message and the key, and then encrypt/decrypt the message.

I found several links on the web to help me out, but each one of them has flaws:

This one at codekoala uses os.urandom, which is discouraged by PyCrypto.

Moreover, the key I give to the function is not guaranteed to have the exact length expected. What can I do to make that happen ?

Also, there are several modes, which one is recommended? I don't know what to use :/

Finally, what exactly is the IV? Can I provide a different IV for encrypting and decrypting, or will this return in a different result?

Here's what I've done so far:

from Crypto import Random
from Crypto.Cipher import AES
import base64

BLOCK_SIZE=32

def encrypt(message, passphrase):
    # passphrase MUST be 16, 24 or 32 bytes long, how can I do that ?
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return base64.b64encode(aes.encrypt(message))

def decrypt(encrypted, passphrase):
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return aes.decrypt(base64.b64decode(encrypted))

Thanks for helping me out!


Source: (StackOverflow)

Installing PyCrypto on Ubuntu - fatal error on build

Having looked at other similar threads, I still can't get pycrypto running.

I'm trying to get it working on my Ubuntu laptop - but I couldn't manage it on my Windows PC either.

I downloaded pycrypto-2.6, extracted it, and ran

    python setup.py build

but then this happened

warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash._MD2' extension
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 - fomit-frame-pointer -Isrc/ -I/usr/include/python2.7 -c src/MD2.c -o build/temp.linux-i686-?2.7/src/MD2.o
src/MD2.c:31:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

Would appreciate any help.


Source: (StackOverflow)

ImportError: No module named Crypto.Cipher

When I try to run app.py (Python 3.3, PyCrypto 2.6) my virtualenv keeps returning the error listed above. My import statement is just from Crypto.Cipher import AES. I looked for duplicates and you might say that there are some, but I tried the solutions (although most are not even solutions) and nothing worked.

You can see what the files are like for PyCrypto below:


Source: (StackOverflow)

How to AES encrypt/decrypt files using Python/PyCrypto in an OpenSSL-compatible way?

There are many ways to use AES, resulting in frequent incompatibilities between different implementations. OpenSSL provides a popular command line interface for AES encryption/decryption:

openssl aes-256-cbc -salt -in filename -out filename.enc
openssl aes-256-cbc -d -in filename.enc -out filename

This could make for a standard.

Python has support for AES in the shape of the PyCrypto package, but it only provides the tools. How to use Python/PyCrypto to encrypt files in a way that OpenSSL can be used to decrypt them, and to decrypt files that have been encrypted using OpenSSL?


Source: (StackOverflow)

Cryptography tools for python 3

I'm writing a program in python 3 which needs encryption functions (at least aes and rsa). I've found PyCrypto which seems to work only on 2.x versions.

Is there any good tool available for python 3 or should I rather start translating my program to be compatible with python 2 (or any other solution) ?

Thank you


Update as mentioned below, PyCrypto is now available on py3k


Source: (StackOverflow)

How do I use a X509 certificate with PyCrypto?

I want to encrypt some data in python with PyCrypto.

However I get an error when using key = RSA.importKey(pubkey):

RSA key format is not supported

The key was generated with:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key -out mycert.pem

The code is:

def encrypt(data):
    pubkey = open('mycert.pem').read()
    key = RSA.importKey(pubkey)
    cipher = PKCS1_OAEP.new(key)
    return cipher.encrypt(data)

Source: (StackOverflow)

Can't install python module "pycrypto" on Debian lenny

I tried to install pycrypto module by downloading the source code and executing the following command python setup.py install, then an error came

running install
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.5 -c src/MD2.c -o build/temp.linux-x86_64-2.5/src/MD2.o
src/MD2.c:31:20: error: Python.h: No such file or directory
src/MD2.c:118: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
In file included from src/MD2.c:134:
src/hash_template.c:42: error: expected specifier-qualifier-list before 'PyObject_HEAD'
src/hash_template.c:46: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'PyTypeObject'
src/hash_template.c: In function 'newALGobject':
src/hash_template.c:55: warning: implicit declaration of function 'PyObject_New'
src/hash_template.c:55: error: expected expression before 'ALGobject'
src/hash_template.c:55: warning: assignment makes pointer from integer without a cast
src/hash_template.c: At top level:
src/hash_template.c:62: error: expected ')' before '*' token
src/hash_template.c:77: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
src/hash_template.c:96: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
src/hash_template.c:108: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
src/hash_template.c:143: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
src/hash_template.c:160: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'ALG_methods'
src/hash_template.c:169: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
src/hash_template.c:178: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'ALGtype'
src/hash_template.c:203: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
src/hash_template.c:237: error: array type has incomplete element type
src/hash_template.c:238: error: 'PyCFunction' undeclared here (not in a function)
src/hash_template.c:238: error: expected '}' before 'ALG_new'
src/hash_template.c: In function 'initMD2':
src/hash_template.c:254: error: 'PyObject' undeclared (first use in this function)
src/hash_template.c:254: error: (Each undeclared identifier is reported only once
src/hash_template.c:254: error: for each function it appears in.)
src/hash_template.c:254: error: 'm' undeclared (first use in this function)
src/hash_template.c:256: error: 'ALGtype' undeclared (first use in this function)
src/hash_template.c:256: error: 'PyType_Type' undeclared (first use in this function)
src/hash_template.c:257: warning: implicit declaration of function 'Py_InitModule'
src/hash_template.c:260: error: 'o' undeclared (first use in this function)
src/hash_template.c:260: warning: implicit declaration of function 'PyInt_FromLong'
src/hash_template.c:260: warning: implicit declaration of function 'PyDict_SetItemString'
src/hash_template.c:260: warning: implicit declaration of function 'PyModule_GetDict'
src/hash_template.c:260: warning: implicit declaration of function 'Py_DECREF'
src/hash_template.c:263: warning: implicit declaration of function 'PyErr_Occurred'
src/hash_template.c:264: warning: implicit declaration of function 'Py_FatalError'
error: command 'gcc' failed with exit status 1

Which means that he didn't find the Python.h. I searched online for this error and it seems that by installing the python header file the problem will be solved, but my Debian lenny came with anther error.

apt-get install python2.6-dev

and error :

Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Couldn't find package python2.6-dev

Source: (StackOverflow)

How to read a RSA public key in PEM + PKCS#1 format

I have a RSA public key in PEM format + PKCS#1(I guess):

-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAJNrHWRFgWLqgzSmLBq2G89exgi/Jk1NWhbFB9gHc9MLORmP3BOCJS9k
onzT/+Dk1hdZf00JGgZeuJGoXK9PX3CIKQKRQRHpi5e1vmOCrmHN5VMOxGO4d+zn
JDEbNHODZR4HzsSdpQ9SGMSx7raJJedEIbr0IP6DgnWgiA7R1mUdAgMBAAE=
-----END RSA PUBLIC KEY-----

I want to get the SHA1 digest of its ASN1 encoded version in Python. The first step should be to read this key, but I failed to do it in PyCrypto:

>> from Crypto.PublicKey import RSA
>> RSA.importKey(my_key)
ValueError: RSA key format is not supported

The documentation of PyCrypto says PEM + PKCS#1 is supported, so I'm confused. I've also tried M2Crypto, but it turns out that M2Crypto does not support PKCS#1 but only X.509.


Source: (StackOverflow)

Trying to install pycrypto on Mac OSX mavericks

I am currently trying to install pycrypto and when I execute python setup.py build I receive this following error:

cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.9-intel-2.7/src/_fastmath.o -lgmp -o build/lib.macosx-10.9-intel-2.7/Crypto/PublicKey/_fastmath.so
ld: illegal text-relocation to '___gmp_binvert_limb_table' in /usr/local/lib/libgmp.a(mp_minv_tab.o) from '___gmpn_divexact_1' in /usr/local/lib/libgmp.a(dive_1.o) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'cc' failed with exit status 1

I've already tried reinstalling the command line tools and removing old instances of Xcode.

Any help would be great thanks


Source: (StackOverflow)

python pycrypto installation error [duplicate]

Possible Duplicate:
PyCrypto and GMP library not found error [Mac OS 10.6.3]

I'm trying to install pycrypto on ubuntu, but it throws error

hom@PC71:~/Desktop/pycrypto-2.3$ sudo python setup.py build
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc -pthread -fno-strict-aliasing -fwrapv -Wall -Wstrict-prototypes -fPIC -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/usr/include/python2.6 -c src/MD2.c -o build/temp.linux-i686-2.6/src/MD2.o
src/MD2.c:31: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

I have installed python-dev tools already.


Source: (StackOverflow)