轉:python安裝pycrypto

來源:互聯網
上載者:User

標籤:

from: http://ljhzzyx.blog.163.com/blog/static/3838031220136592824697/  

在windows下用一下開源工具就是悲催,如題python安裝pycrypto在windows也是很麻煩。一般在官方網站下載:

https://www.dlitz.net/software/pycrypto/

然後使用命令就可以安裝成功了:

python setup.py build

python setup.py install

但是在windows下會報錯:

Python error: Unable to find vcvarsall.bat

在這個url:http://www.biaodianfu.com/python-error-unable-to-find-vcvarsall-bat.html所講的已經很詳細了:

解決方案一:安裝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,是無法解密成功的。

轉:python安裝pycrypto

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.