python的Crypto模块实现AES加密实例代码

作者:werewolf_st 时间:2022-10-02 17:43:10 

本文主要探索的是python的Crypto模块实现AES加密,分享了具体实现代码,下面看看具体内容。

学了使用Crypto模块的AES来加密文件,现在记录下来便于后边儿查看。

在刚开始知道这个模块的时候,连基本的Crypto模块的安装都花了很多很多时间来搞,也不知道什么情况反正是折腾很久了才安装起的,记得是包安装起来了,但使用的时候始终提示找不到Crypto.Cipher模块。然后怎么解决的呢?

一、把我的python换成了64位的,本来电脑就是64位的也不知道之前是啥情况安装成32位的了。(O(∩_∩)O哈哈~)
二、安装了VCForPython27.msi
三、在cmd中执行:


pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/

经过上边儿的几个步骤,我是能够成功执行


from Crypto.Cipher import AES

现在上一个实例代码:


# !/usr/bin/env python
# coding: utf-8
'''

'''

from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

class MyCrypt():
 def __init__(self, key):
   self.key = key
   self.mode = AES.MODE_CBC

def myencrypt(self, text):
   length = 16
   count = len(text)
   print count
   if count < length:
     add = length - count
     text= text + ('\0' * add)

elif count > length:
     add = (length -(count % length))
     text= text + ('\0' * add)

# print len(text)
   cryptor = AES.new(self.key, self.mode, b'0000000000000000')
   self.ciphertext = cryptor.encrypt(text)
   return b2a_hex(self.ciphertext)

def mydecrypt(self, text):
   cryptor = AES.new(self.key, self.mode, b'0000000000000000')
   plain_text = cryptor.decrypt(a2b_hex(text))
   return plain_text.rstrip('\0')

if __name__ == '__main__':
 mycrypt = MyCrypt('abcdefghjklmnopq')
 e = mycrypt.myencrypt('hello,world!')
 d = mycrypt.mydecrypt(e)
 print e
 print d

在cmd中执行结果:

python的Crypto模块实现AES加密实例代码

来源:http://blog.csdn.net/werewolf_st/article/details/45935913

标签:python,crypto,aes加密
0
投稿

猜你喜欢

  • python3+pyqt5+itchat微信定时发送消息的方法

    2022-02-28 04:31:49
  • python框架Django实战商城项目之工程搭建过程图文详解

    2022-12-16 16:25:57
  • 讲解SQL Server危险扩展存储删除和恢复

    2008-12-09 14:30:00
  • asp如何实现无组件上传二进制文件?

    2010-06-03 10:09:00
  • 能说明一下GETROWS的用法吗?

    2009-11-02 20:12:00
  • Mootools 1.2教程(20)——选项卡效果(Tabs)

    2008-12-26 18:19:00
  • python使用参数对嵌套字典进行取值的方法

    2022-04-12 10:13:27
  • 黄相如:如何做好用户体验

    2008-06-04 17:34:00
  • ext3下删除mysql数据库的数据恢复案例

    2009-05-13 14:39:00
  • 浅析SQL Server与Oracle数据库的区别

    2007-10-31 11:39:00
  • Python +Selenium解决图片验证码登录或注册问题(推荐)

    2022-12-30 05:41:51
  • 用AspJpeg调整文字水印透明,生成图片水印的效果

    2008-12-29 19:43:00
  • HTML中事件触发列表与解说

    2007-10-22 12:50:00
  • Python中使用Frozenset对象的案例详解

    2023-09-27 09:36:32
  • asp 横排显示数据

    2011-03-10 10:50:00
  • python多进程控制学习小结

    2021-08-31 00:48:57
  • python利用线程生成不同尺寸的缩略图实例详解

    2023-07-07 08:47:55
  • 白鸦:贪守米缸者,饿死灶台

    2009-02-23 13:03:00
  • 全新感受Oracle 9i

    2010-07-16 13:32:00
  • 常用ASCII 码对照表

    2007-08-21 14:35:00
  • asp之家 网络编程 m.aspxhome.com