如何为mac python安装pycrypto

如题所述

解决方案一:安装Vs2008(实测)
完全的无脑流,安装完问题直接解决。
解决方案二:安装Vs2010(未测试)
上次在电脑上装个Vs2010并不能像 vs2008那样直接解决问题,从网上找到如下解决方案,不知是否可行。
打开“<python安装目录>\Lib\distutils\msvc9compiler.py”
找到 toolskey = “VS%0.f0COMNTOOLS” % version,直接修改为 toolskey = ”VS100COMNTOOLS”
解决方案三:安装MinGW(实测)
1、下载安装MinGW,下载地址为:http://sourceforge.net/projects/mingw/files/latest/download?source=files
2、在MinGW的安装目录下找到bin文件夹,找到mingw32-make.exe,复制一份更名为make.exe
3、把MinGW的路径添加到环境变量path中,比如我把MinGW安装到D:\MinGW\中,就把D:\MinGW\bin添加到path中;
4、在<python安装目录>\distutils增加文件distutils.cfg,在文件里输入
[build]
compiler=mingw32
保存;
5、执行原先的模块安装,发现还是报错,报错内容为:error: command ’gcc’ failed: No such file or directory 解决方案是将D:\MinGW\lib再添加到PATH中。
6、如果安装过程中出现 error: Could not find ‘openssl.exe’ 则直接到http://pypi.python.org/pypi/pyOpenSSL/0.13 下载安装即可。
再次执行时安装模块时,发现如下错误:
D:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall “-ID:\Program Files\Python27\inc
lude” “-ID:\Program Files\Python27\include” “-ID:\Program Files\Python27\PC” -c
../libdasm.c -o build\temp.win32-2.7\Release\..\libdasm.o
cc1.exe: error:unrecognized command line option ‘-mno-cygwin’
error: command ‘gcc’ failed with exit status 1
原因是gcc 4.6.x 以后不再接受-mno-cygwin为了解决这个问题需要修改<python安装目录>\distutils\cygwinccompiler.py文件。找到:
self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
compiler_so='gcc -mno-cygwin -mdll -O -Wall',
compiler_cxx='g++ -mno-cygwin -O -Wall',
linker_exe='gcc',
linker_so='%s -mno-cygwin %s %s'
% (self.linker_dll, shared_option,
entry_point))
修改为:
self.set_executables(compiler='gcc -O -Wall',
compiler_so='gcc -mdll -O -Wall',
compiler_cxx='g++ -mno-cygwin -O -Wall',
linker_exe='gcc',
linker_so='%s -mno-cygwin %s %s'
% (self.linker_dll, shared_option,
entry_point))
讲了三个解决方案,安装visualstudio太庞大的,没有试,于是就尝试第三种方法。其中openssl.exe的错误没有碰到,应该是已经有了,而distutils.cfg文件的目录在python2.7下面有点不一样,在Python27\Lib\distutils下面。一直到最后个修改项,最终错误是:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘PyInt_AsUnsignedLongLongMask’
没有解决。
中间有个警告,在cygwin中使用dos style的path,设置path CYGWIN=nodosfilewarning 来规避
cygwin warning:
MS-DOS style path detected: C:\cygwin\home\ADMINI~1\hadoop\/build/native
Preferred POSIX equivalent is: /home/ADMINI~1/hadoop/build/native
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
12/02/13 10:34:53 INFO namenode.NameNode: STARTUP_MSG:
python setup.py install build --compiler=mingw32
这个命令尝试也不行。
在这个url:http://stackoverflow.com/questions/1687283/why-cant-i-just-install-the-pycrypto,国际友人介绍用PyPM来安装,由于要另外安装工具,没有尝试:
You may use PyPM to install (pre-built binary package of) pycrypto:
C:> pypm install pycrypto
Ready to perform these actions:
The following packages will be installed:
pycrypto-2.0.1
Get: [pypm.activestate.com] pycrypto 2.0.1-1
Installing pycrypto-2.0.1
PyPM can be installed by installing ActivePython.http://www.activestate.com/activepython/
后来在这里http://lili-xiang.iteye.com/blog/1796640,看到有预编译好的版本用来安装,在地址http://www.voidspace.org.uk/downloads/pycrypto26/pycrypto-2.6.win-amd64-py3.2.exe下载PyCrypto 2.6 for Python 3.2 64bit,随后安装成功,可以在Komodo IDE 7中使用了。测试代码是这里的:http://ddkangfu.blog.51cto.com/311989/484801
但是例子的代码是跑不起来的,因为aes加密中,cbc模式下是还有个iv参数的,修改成这样
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
PADDING = '\0'
pad_it = lambda s: s+(16 - len(s)%16)*PADDING
if __name__ == "__main__":
key = '1234567890123456'
data = 'qwertyuiopasdfgh'
obj = AES.new(key, AES.MODE_CBC, data)
#obj = AES.new(key, AES.MODE_ECB)
crypt = obj.encrypt(data)
print crypt
#obj2 = AES.new(key, AES.MODE_ECB)
obj2 = AES.new(key, AES.MODE_CBC, data)
recovery = obj2.decrypt(crypt)
print recovery
才可以正常运行,如果使用ECB模式,就不用最后一个iv参数的。这里使用加密源data作为iv参数是没有意思的,正式使用的时候肯定会另外定义的字符串,记得iv长度要是16位的倍数。代码里还要注意obj2,不能重复使用第一个obj,在加密过程中obj已经改变了,如果不充生成obj2,是无法解密成功的。
温馨提示:答案为网友推荐,仅供参考