源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python AES加密模块用法分析

  • 时间:2020-10-31 00:09 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python AES加密模块用法分析
本文实例讲述了Python AES加密模块用法。分享给大家供大家参考,具体如下: AES是新的一种加密模块。在上次介绍过在C语言中如何来OpenSSL中的DES。这次我们来看看Python自带的库如何来使用AES来加解密。其实二者的原理还是非常像,只是说在python中来做这个事情会比C语言要简单点,但是比起C#/Java还是有点点啰嗦。在C#/JAVA这种语言中,对于加密的源数据的处理,padding一般都会有完整的实现。我在上次C语言中也处理过这个问题。在python库中,也是需要自己来处理这个。
from Crypto.Cipher import AES
# padding算法
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(0)
unpad = lambda s : s[0:-ord(s[-1])]
# 将字符串转换成二进制的buff块
def parse_hex(hex_str):
 l=int(math.ceil(len(hex_str)/2))
 buf=''
 for i in range(0,l):
  s=hex_str[(i*2):((i+1)*2)]
  buf=buf+chr(int(s,16))
 return buf
# 解析加密的key
key=parse_hex("68b329da9893e34099c7d8ad5cb9c940")
iv=parse_hex("68b329da9893e34099c7d8ad5cb9c940")
# 新建一个AES的对象
aes_obj = AES.new(key, AES.MODE_CBC,iv)
# 做字节对齐
padding_zero=pad(raw_buf)
# 开始加密
encrypt_buf=aes_obj.encrypt(padding_zero)
# 解密
buff=aes_obj.decrypt(encrypt_buf)

这个代码中padding写的还是非常漂亮的。如果在C里面实现这个,还需要啰嗦写不少。 [b]PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:[/b] [b]文字在线加密解密工具(包含AES、DES、RC4等): [/b][url=http://tools.jb51.net/password/txt_encode]http://tools.jb51.net/password/txt_encode[/url] [b]MD5在线加密工具: [/b][url=http://tools.jb51.net/password/CreateMD5Password]http://tools.jb51.net/password/CreateMD5Password[/url] [b]在线散列/哈希算法加密工具: [/b][url=http://tools.jb51.net/password/hash_encrypt]http://tools.jb51.net/password/hash_encrypt[/url] [b]在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具: [/b][url=http://tools.jb51.net/password/hash_md5_sha]http://tools.jb51.net/password/hash_md5_sha[/url] [b]在线sha1/sha224/sha256/sha384/sha512加密工具: [/b][url=http://tools.jb51.net/password/sha_encode]http://tools.jb51.net/password/sha_encode[/url] 更多关于Python相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/788.htm]Python编码操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/663.htm]Python数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/648.htm]Python Socket编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/642.htm]Python函数使用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/636.htm]Python字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/520.htm]Python入门与进阶经典教程[/url]》及《[url=http://www.1sucai.cn/Special/516.htm]Python文件与目录操作技巧汇总[/url]》 希望本文所述对大家Python程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部