Windows下python安装PyCrypto加密模块以及使用

本机环境:
  • Windows8(Windows系列都可以)
  • python3.4
步骤一:首先下载PyCryto

PyCrypto点击下载—下载完解压

假设我解压到H盘,打开cmd,cd到pycrypto-2.6.1文件夹下
执行如下命令:
这里写图片描述

接着执行:H:\pycrypto-2.6.1>python setup.py install

安装完成!

下面官方对模块的介绍以及使用的一些小例子

This is a collection of both secure hash functions (such as SHA256 and
RIPEMD160), and various encryption algorithms (AES, DES, RSA, ElGamal,
etc.). The package is structured to make adding new modules easy.
This section is essentially complete, and the software interface will
almost certainly not change in an incompatible way in the future; all
that remains to be done is to fix any bugs that show up. If you
encounter a bug, please report it in the Launchpad bug tracker at

   https://launchpad.net/products/pycrypto/+bugs
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
#SHA256例子
hash = SHA256.
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Python 中,`crypto` 模块不是 Python 标准库的一部分,需要额外安装。常用的安装方式是使用 `pip` 包管理器,在终端中执行以下命令即可完成安装: ``` pip install pycrypto ``` 安装完成后,你可以在 Python 代码中使用 `crypto` 模块提供的加密和解密算法。以下是一个使用 `AES` 算法进行加密和解密的示例代码: ```python from Crypto.Cipher import AES import base64 # 加密函数 def encrypt(text, key): # 填充文本 text = text + (16 - len(text) % 16) * chr(16 - len(text) % 16) # 初始化加密器 aes = AES.new(key, AES.MODE_ECB) # 加密 encrypted_text = aes.encrypt(text) # base64 编码 encrypted_text = base64.b64encode(encrypted_text) return encrypted_text # 解密函数 def decrypt(encrypted_text, key): # base64 解码 encrypted_text = base64.b64decode(encrypted_text) # 初始化解密器 aes = AES.new(key, AES.MODE_ECB) # 解密 decrypted_text = aes.decrypt(encrypted_text) # 去除填充 decrypted_text = decrypted_text[:-ord(decrypted_text[-1])] return decrypted_text # 测试 text = 'Hello, world!' key = 'secret key 123' encrypted_text = encrypt(text, key) decrypted_text = decrypt(encrypted_text, key) print('加密前的文本:', text) print('加密后的文本:', encrypted_text) print('解密后的文本:', decrypted_text) ``` 需要注意的是,在使用 `AES` 加密算法时,需要使用一个长度为 16、24 或 32 的密钥,对于不同的密钥长度,使用加密模式也不同。在上述示例代码中,我们使用的是 `ECB` 模式。如果需要使用其他加密算法或加密模式,你可以查看官方文档或其他相关文档进行学习。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值