python3 aes加解密
# encoding: utf-8
import xlrd
import os
import yaml
import logging.config
from Crypto.Cipher import AES
import base64 def setup_logging(default_path = "log_config.yaml",default_level = logging.INFO,env_key = "LOG_CFG"):
path = default_path
value = os.getenv(env_key,None)
if value:
path = value
if os.path.exists(path):
with open(path,"r") as f:
config = yaml.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level = default_level) def encrypt(text, key, iv):
if type(text) is str:
text = text.encode(encoding='utf-8', errors='strict')
if type(key) is str:
key = key.encode(encoding='utf-8', errors='strict')
if type(iv) is str:
iv = iv.encode(encoding='utf-8', errors='strict')
if len(text) % 16:
text += (chr(16 - len(text) % 16) * (16 - len(text) % 16)).encode(encoding='utf-8', errors='strict')
text = base64.b64encode(s=AES.new(key, mode=AES.MODE_CBC, IV=iv).encrypt(text),
altchars=None).decode(encoding='utf-8', errors='strict')
return text def decrypt(cipher_text, key, iv):
if type(key) is str:
key = key.encode(encoding='utf-8', errors='strict')
if type(iv) is str:
iv = iv.encode(encoding='utf-8', errors='strict')
if type(cipher_text) is str:
cipher_text_bytes = base64.b64decode(cipher_text)
#todo aes解密
plaintext_bytes = AES.new(key, mode=AES.MODE_CBC, IV=iv).decrypt(cipher_text_bytes)
#todo 去填充字节
for i in range(1,17):
plaintext_bytes = plaintext_bytes.rstrip(chr(i).encode(encoding='utf-8', errors='strict'))
plaintext = plaintext_bytes.decode(encoding='utf-8', errors='strict')
return plaintext if __name__ == '__main__':
setup_logging()
"""
book = xlrd.open_workbook('testdata_1.xlsx')
sheet = book.sheet_by_index(0) # 根据顺序获取sheet
print(sheet.ncols) # 获取excel里面有多少列
print(sheet.nrows) # 获取excel里面有多少行
for i in range(1, sheet.nrows):
user_info_dict = {}
if len(sheet.cell(i, 0).value) > 0:
user_info_dict['name'] = sheet.cell(i, 0).value.strip()
user_info_dict['idcard'] = sheet.cell(i, 1).value.strip()
print(sheet.cell(i, 2).value)
print(sheet.cell(i, 2).ctype)
user_info_dict['phone'] = str(int(sheet.cell(i, 2).value)).strip()
print(user_info_dict)
print(chr(1))
""" for i in range(1,20):
s = 'a'* i
logging.info(('s', s))
mw = encrypt(s,'', '')
logging.info(('e',mw))
logging.info(('d',decrypt(mw,'', '')))
logging.info('====================')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'a')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'iEqJ43muMK0K98y26ZG55A==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'a')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'tXevZVzEJABPQtw9DECgYQ==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', '/I7IfuZDcDaKXZtifDcs5w==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'Alk1FhjAr4JUbb+m+u+F2Q==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'udmP3Tf0/u4RYBMKJPJ0/A==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'PdwwbsFTMHDNUsJMZAeM+A==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'X5sjPFnk30mCHVDVacXjyA==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'n67DCEhkX6Dr0soQVz63zg==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 72 - ('e', 'jmv9NFzbmiWcpd+gOMXFLg==')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaa')
2018-10-31 08:59:50,153 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,153 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'N1eeE8sahDhoNxGKhHINcg==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'X5ldc5VOXhEY9mpySsAf8A==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'S2z8gQ/EzVhbhcthz1ILbQ==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'FMNUDHyJxF13BvqT3Iru6g==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'hB6U1AchwgQuYOlwMcmegA==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'N7wIcnTQH5jlRyG6YqAcqA==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'LWlQdN/UTy+opSdVdh/hyw==')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'LWlQdN/UTy+opSdVdh/hyw4zq/Ogqcr8FyuqK/GUqAY=')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'LWlQdN/UTy+opSdVdh/hy1q9voPP4CUk7ON/d/piMPU=')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
2018-10-31 08:59:50,169 - root - INFO - MainThread - 70 - ('s', 'aaaaaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 72 - ('e', 'LWlQdN/UTy+opSdVdh/hy736EUVnpijwTpyfWWP+i0k=')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 73 - ('d', 'aaaaaaaaaaaaaaaaaaa')
2018-10-31 08:59:50,169 - root - INFO - MainThread - 74 - ====================
Process finished with exit code 0
python3 aes加解密的更多相关文章
- python3 AES 加解密
#coding:utf-8 import base64 from Crypto.Cipher import AES #注:python3 安装 Crypto 是 pip3 install -i htt ...
- Python3 AES加解密(AES/ECB/PKCS5Padding)
class AesEncry(object): key = "wwwwwwwwwwwwwwww" # aes秘钥 def encrypt(self, data): data = j ...
- DES,AeS加解密,MD5,SHA加密
1.DES一共就有4个参数参与运作:明文.密文.密钥.向量.其中这4者的关系可以理解为: 密文=明文+密钥+向量: 明文=密文-密钥-向量: 为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复, ...
- c# Aes加解密和对象序列化
aes加解密 public class AesCryptto { private string key = "hjyf57468jhmuist"; private string i ...
- Java、C#双语版配套AES加解密示例
这年头找个正经能用的东西那是真难,网上一搜索一大堆,正经能用的没几个,得,最后还是得靠自己,正巧遇上需要AES加解密的地方了,而且还是Java和C#间的相互加解密操作,这里做个备忘 这里采用的加解 ...
- AES加解密算法Qt实现
[声明] (1) 本文源码 在一位未署名网友源码基础上,利用Qt编程,实现了AES加解密算法,并添加了文件加解密功能.在此表示感谢!该源码仅供学习交流,请勿用于商业目的. (2) 图片及描述 除图1外 ...
- aes加解密 Illegal key size
做aes加密时,发生一个奇怪的错误,在本地环境是好的,发布到测试环境就出问题, java.security.InvalidKeyException: Illegal key size 想到本地环境之前 ...
- C# RSA加解密与验签,AES加解密,以及与JAVA平台的密文加解密
前言: RSA算法是利用公钥与密钥对数据进行加密验证的一种算法.一般是拿私钥对数据进行签名,公钥发给友商,将数据及签名一同发给友商,友商利用公钥对签名进行验证.也可以使用公钥对数据加密,然后用私钥对数 ...
- Aes加解密,php
Aes类库 <?php namespace Aes; class Aes { /** * var string $method 加解密方法,可通过openssl_get_cipher_metho ...
随机推荐
- Atcoder Beginner Contest 118 D-Match Matching(完全背包)
题目链接 题意就是给N根火柴,M个数(M只能是1到9,对应的数字也只能是1到9),只能用这M个出现过的数(但每个数可以随便用多少个,只要火柴够)来拼出一个数字(拼出1,2,3,4,5,6,7,8,9分 ...
- servlet篇 之 访问形式
get方式访问和post方式访问: get/post区别? 1) 参数传递 查询字符串(的形式)! get url?key1=value&key2=value 2) http协议 请求报文包 ...
- python之旅5【第五篇】
装饰器详解 函数刚开始不解析内部,只是放进内存 装饰器是函数,只不过该函数可以具有特殊的含义,装饰器用来装饰函数或类,使用装饰器可以在函数执行前和执行后添加相应操作. 1 下面以一个函数开始,理解下面 ...
- Cash Machine POJ - 1276 多重背包二进制优化
题意:多重背包模型 n种物品 每个m个 问背包容量下最多拿多少 这里要用二进制优化不然会超时 #include<iostream> #include<cstdio> #in ...
- Matplotlib学习---用matplotlib画直方图/密度图(histogram, density plot)
直方图用于展示数据的分布情况,x轴是一个连续变量,y轴是该变量的频次. 下面利用Nathan Yau所著的<鲜活的数据:数据可视化指南>一书中的数据,学习画图. 数据地址:http://d ...
- (转)最短路径算法-Dijkstra算法分析及实践
原地址:http://www.wutianqi.com/?p=1890 这篇博客写的非常简洁易懂,其中各个函数的定义也很清晰,配合图表很容易理解这里只选取了 其中一部分(插不来图片). Dijkstr ...
- 「POJ 1135」Domino Effect(dfs)
BUPT 2017 Summer Training (for 16) #3G 题意 摆好的多米诺牌中有n个关键牌,两个关键牌之间有边代表它们之间有一排多米诺牌.从1号关键牌开始推倒,问最后倒下的牌在哪 ...
- 【HDU - 5845】Best Division(xor-trie、01字典树、dp)
BUPT2017 wintertraining(15) #7E 题意 把数组A划分为k个区间,每个区间不超过L长度,每一个区间异或和之和为S.现在求:S不超过X,区间个数的最大值. 且A是这样给你的: ...
- __init__、__new__、__call__ 方法
__init__方法 __init__方法负责对象的初始化,系统执行该方法前,其实该对象已经存在了,要不然初始化什么东西呢?先看例子: # class A(object): python2 必须显示地 ...
- VSIX 插件右键菜单(2)
编译项目然后发布 // 获取当前右击的活动项目 EnvDTE.Project activeProj = ProjectHelpers.GetActiveProject(); // 获取 编译选项 Re ...