win10 python3.7 安装 pycrypto2.6.1遇到的问题及生成大素数

win10 python3.7 安装 pycrypto2.6.1生成大素数.

1.安装pycrypto

pip install pycrypto

如果安装没有出现错误,关闭页面直接进入代码阶段.出现下面内容即为成功

running install_egg_info

安装pycrypto遇到的问题:

  • 首先是遇到常见error,缺少C++组件:

报错内容:Microsoft Visual C++ 14.0 is required.

报错原因:组件缺失

解决办法:使用下面下载链接下载Microsoft Visual C++ 14.0安装就ok

链接: https://pan.baidu.com/s/1_yNMN-bioEn67GKV7Me9cw 提取码: qu4p 

  • 组件inttypes.h报错
running install
running build
running build_py
running build_ext
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
building ‘Crypto.Random.OSRNG.winrandom‘ extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isrc/ -Isrc/inc-msvc/ -IC:\Python36\include -IC:\Python36\include
winrand.c
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(26): error C2061: syntax error: identifier ‘intmax_t‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(27): error C2061: syntax error: identifier ‘rem‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(27): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(28): error C2059: syntax error: ‘}‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(30): error C2061: syntax error: identifier ‘imaxdiv_t‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(30): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(40): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(41): error C2146: syntax error: missing ‘)‘ before identifier ‘_Number‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(41): error C2061: syntax error: identifier ‘_Number‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(41): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(42): error C2059: syntax error: ‘)‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(45): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2146: syntax error: missing ‘)‘ before identifier ‘_Numerator‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2061: syntax error: identifier ‘_Numerator‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2059: syntax error: ‘,‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(48): error C2059: syntax error: ‘)‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(50): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(56): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(63): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(69): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(76): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(82): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(89): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(95): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
error: command ‘C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe‘ failed with exit status 2

 解决办法: 1.找到你Microsoft Visual Studio 14.0的安装路径,我的是

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include

                 复制stdint.h到下一步的路径中

win10 python3.7 安装 pycrypto2.6.1遇到的问题及生成大素数_第1张图片

 2.把上一步复制的文件粘贴在下图路径中,我的路径为

   

C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt

    该处的路径即为第二种问题报错路径

win10 python3.7 安装 pycrypto2.6.1遇到的问题及生成大素数_第2张图片

 3.修改报错地址的inttypes.h文件,#include 改为 #include “stdint.h”

win10 python3.7 安装 pycrypto2.6.1遇到的问题及生成大素数_第3张图片

重新安装,显示running install_egg_info即安装成功.

  • 引用库时,注意大小写Crypto还是crypto

安装成功之后使用库

from crypto.PublicKey import RSA
if __name__ == '__main__':

    RsaKey = RSA.generate(2048)
    print(RsaKey.p)
    print(hex(RsaKey.p))
    print(len(hex(RsaKey.p)))
    print("#################")
    print(RsaKey.q)
    print(hex(RsaKey.q))
    print(len(hex(RsaKey.q)))

运行代码报错,修改引用

from Crypto.PublicKey import RSA

并在python目录下,我的目录

D:\ocean\application\python3.7\Lib\site-packages

 修改crypto为Crypto,运行无误

win10 python3.7 安装 pycrypto2.6.1遇到的问题及生成大素数_第4张图片

随机生成2048位秘钥时素数p.q

142870561478849601570504679105273643758117627585454743995907919541553148689632419113837185803560793759638573441813977114317278225693080976034587580982499350490827518014983024672401517643279801065991925718870879528219030758164362516664442706194260853431851326701977068212301202405048490389675097124448876298153
0xcb7455b7e4b8f14c9f75b065e2f5633a04d9fab2234d42fc66f4d8b741f832214f5cef674f40c4a6d4460a379f1ea9de3124d2031d85baeae4d0de175b7ae950e33c928a9e803a00ac872da07e7a659ba47d5e4721514c78c0596d5b04e16da73da32915c140ff23135687800ceafeae9e5623724603d44704d233168212ffa9
258
#################
162875009632011411706814877177686326465404862017258811144535894919567455139192650436940298878327275359959208525172709218072877823432029841824843636301963748347031066488635537909588000032275656796333401001731434906092447548558541996170259155414830039893865459046249631078424653943011599507700265946932309114463
0xe7f11415d5ae47292dc7a177b74809a78bda2cf7c5710f8f87acbb0a815f3c4983fa8c9d0a2e58780f69392f44015cb65cf02a8fe4c6588cec5bb5e0f5bd235e6a8c07c705bacc2d104a63a270b1e88dac24d4dfbf1f6d6dd7be96c9823291a5f4d98250aceba9b69f451190807a3c206e28804162d41154531895f678b3be5f
258

 使用RSA库生成2048位秘钥时素数p.q时,p为136字节,q为120字节.查看源码注释,是为了两个素数不相近.

我们的需求是只能生成128字节的素数.上图中258为p.q转化为hex时,包含0x的长度.

至此,可以生成128字节素数.

你可能感兴趣的:(Python)