python3.6安装pycrypto

安装命令:

pip install pycrypto      --index-url http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

1、问题描述:

File "d:\apply\python362\lib\site-packages\pip\compat_init_.py", line 73, in console_to_str return s.decode(sys.stdout.encoding) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd3 in position 94: invalid continuation byte

解决方法有两个,可以任选其一:

(1)修改python362\lib\site-packages\pip\compat_init_.py文件的第75行

    def console_to_str(s):
        try:
            return s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            return s.decode('utf-8')

    def console_to_str(s):
        try:
            result = s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            import locale
            s = s.decode(locale.getpreferredencoding(), 'ignore').encode('utf-8')
            result = s.decode('utf-8')
        return result

(2)修改python362\lib\site-packages\pip\compat_init_.py文件的第75行

    def console_to_str(s):
        try:
            return s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            return s.decode('utf-8')

    def console_to_str(s):
        try:
            return s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            try:
                return s.decode('utf-8')
            except UnicodeDecodeError:
                return s.decode('cp936')

2、问题描述:

winrand.c
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(26): error C2061: 语法错误: 标识符“intmax_t”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(27): error C2061: 语法错误: 标识符“rem”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(27): error C2059: 语法错误:“;”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(28): error C2059: 语法错误:“}”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(30): error C2061: 语法错误: 标识符“imaxdiv_t”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(30): error C2059: 语法错误:“;”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(40): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(41): error C2146: 语法错误: 缺少“)”(在标识符“_Number”的 前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(41): error C2061: 语法错误: 标识符“_Number”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(41): error C2059: 语法错误:“;”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(42): error C2059: 语法错误:“)”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(45): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(46): error C2146: 语法错误: 缺少“)”(在标识符“_Numerator”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(46): error C2061: 语法错误: 标识符“_Numerator”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(46): error C2059: 语法错误:“;”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(46): error C2059: 语法错误:“,”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(48): error C2059: 语法错误:“)”
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(50): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(56): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(63): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(69): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(76): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(82): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(89): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
C:\Program Files (x86)\Windows Kits\10\include\10.0.15063.0\ucrt\inttypes.h(95): error C2143: 语法错误: 缺少“{”(在“__cdecl”的前面)
error: command 'D:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.10.25017\\bin\\HostX64\\x64\\cl.exe' failed with exit status 2

解决方法有两个,可以任选其一:

(1)本机上安装了Microsoft Visual Studio2017 Community版

A、将D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\stdint.h文件拷贝到C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt\目录下

B、修改C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt\inttypes.h中的第13行,将

#include <stdint.h>

修改为

#include "stdint.h"

(2)使用环境变量(未验证)

设置环境变量VCINSTALLDIR为D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC set CL=/FI "%VCINSTALLDIR\Tools\MSVC\14.10.25017\include\stdint.h" %CL% 然后执行pip install pycrypto

(3)其他方案(未验证),转载于python安装pycrypto

================================================= 但是在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,是无法解密成功的。

转载于:https://my.oschina.net/mengyoufengyu/blog/1524422

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值