安装命令:
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
修改为
#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那样直接解决问题,从网上找到如下解决方案,不知是否可行。
打开“\Lib\distutils\msvc9compiler.py”
找到 toolskey = “VS%0.f0COMNTOOLS” % version,直接修改为 toolskey = ”VS100COMNTOOLS”
2、在MinGW的安装目录下找到bin文件夹,找到mingw32-make.exe,复制一份更名为make.exe
3、把MinGW的路径添加到环境变量path中,比如我把MinGW安装到D:\MinGW\中,就把D:\MinGW\bin添加到path中;
4、在\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为了解决这个问题需要修改\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
这个命令尝试也不行。
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
-- 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,是无法解密成功的。