Python基于pycrypto实现的AES加密和解密算法示例

yipeiwu_com5年前Python基础

本文实例讲述了Python基于pycrypto实现的AES加密和解密算法。分享给大家供大家参考,具体如下:

一 代码

# -*- coding: UTF-8 -*-
import string
import random
from Crypto.Cipher import AES
def keyGenerater(length):
  '''''生成指定长度的秘钥'''
  if length not in (16, 24, 32):
    return None
  x = string.ascii_letters+string.digits
  return ''.join([random.choice(x) for i in range(length)])
def encryptor_decryptor(key, mode):
  return AES.new(key, mode, b'0000000000000000')
#使用指定密钥和模式对给定信息进行加密
def AESencrypt(key, mode, text):
  encryptor = encryptor_decryptor(key, mode)
  return encryptor.encrypt(text)
#使用指定密钥和模式对给定信息进行解密
def AESdecrypt(key, mode, text):
  decryptor = encryptor_decryptor(key, mode)
  return decryptor.decrypt(text)
if __name__ == '__main__':
  text = 'Python3.5 is excellent.'
  key = keyGenerater(16)
  #随机选择AES的模式
  mode = random.choice((AES.MODE_CBC, AES.MODE_CFB, AES.MODE_ECB, AES.MODE_OFB))
  if not key:
    print('Something is wrong.')
  else:
    print('key:', key)
    print('mode:', mode)
    print('Before encryption:', text)
    #明文必须以字节串形式,且长度为16的倍数
    text_encoded = text.encode()
    text_length = len(text_encoded)
    padding_length = 16 - text_length%16
    text_encoded = text_encoded + b'0'*padding_length
    text_encrypted = AESencrypt(key, mode, text_encoded)
    print('After encryption:', text_encrypted)
    text_decrypted =AESdecrypt(key, mode, text_encrypted)
    print('After decryption:', text_decrypted.decode()[:-padding_length])

二 运行结果

E:\python\python可以这样学\第18章 密码学编程\code>python AES_test.py
('key:', 'D5pcO6iu0HIbj3I2')
('mode:', 1)
('Before encryption:', 'Python3.5 is excellent.')
('After encryption:', '\xf4\x15\x9f\xaf\xea\xd0\n\x03\xfdf\xf6}9\xaa\xa34\xb4\x1eL2\x0e \x16\xa5 \xff?\x8bA\x8e\xdd\xa8')
('After decryption:', u'Python3.5 is excellent.')

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python自动保存百度盘资源到百度盘中的实例代码

python自动保存百度盘资源到百度盘中的实例代码

本实例的实现逻辑是,应用selenium UI自动化登录百度盘,读取存储百度分享地址和提取码的txt文档,打开百度盘分享地址,填入提取码,然后保存到指定的目录中 全部代码如下: #...

python 将list转成字符串,中间用符号分隔的方法

如下所示: data = [1,2,3,4] print "|".join(str(i) for i in data) 如果data中有中文: import sys reloa...

Flask框架搭建虚拟环境的步骤分析

本文实例讲述了Flask框架搭建虚拟环境的步骤。分享给大家供大家参考,具体如下: 为什么要搭建虚拟环境? 在开发过程中, 当需要使用python的某些工具包/框架时需要联网安装...

Python读写txt文本文件的操作方法全解析

Python读写txt文本文件的操作方法全解析

一、文件的打开和创建 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python!\nhel...

TensorFlow实现卷积神经网络

TensorFlow实现卷积神经网络

本文实例为大家分享了TensorFlow实现卷积神经网络的具体代码,供大家参考,具体内容如下 代码(源代码都有详细的注释)和数据集可以在github下载: # -*- codin...