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 ...
随机推荐
- hdu1839(最小生成树)
题意:字面意思: 思路:就是多了一个前提,有些点之间可能有边,有两个处理方法,一个是有边的,这条边权值归零,另一个是,先一次循环用并查集过一遍: 代码:(用的是第一种方法) #include<i ...
- Power Spectral Density
对于一个特定的信号来说,有时域与频域两个表达形式,时域表现的是信号随时间的变化,频域表现的是信号在不同频率上的分量.在信号处理中,通常会对信号进行傅里叶变换得到该信号的频域表示,从而得到信号在频域上的 ...
- RESTful 架构详解
RESTful 架构详解 分类 编程技术 1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次 ...
- sws_getContext函数参数介绍
原型: SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, ...
- Qt QLabel的使用
QLabel类主要用来文本和图像的显示,没有提供用户交互功能.QLabel对象的视觉外观可以由用户自定义配置. 它还可以为另外一个可获得焦点的控件作为焦点助力器. QLabel可以显示下列的所有类型: ...
- eclipse添加tomcat服务器
在网上找资料好辛苦,还不对,自己试了好久,终于成功了 还是一如既往的分享 右键 弄好以后发现如此简单| _ |
- Java将Excel中科学计数法解析成数字
需要注意的是一般的科学表达式是1.8E12 1.8E-12 而在Excel中的科学表达式是1.8E+12 1.8E-12 我写的科学计数法的正则表达式是(-?\d+\.?\d*)[Ee]{1}[\+- ...
- python3 文件和流
流: 打开文件: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, ...
- Android assets的一个bug
摘要 Android assets目录下资源文件超过1MB的问题. 由于要显示一些奇奇怪怪的日文字符,我们在应用里放了一个字库文件,譬如叫做jp.ttf,放在assets目录下打包. 开发.调试一切正 ...
- 【HDU1846】Brave Game(博弈论)
题面 HDU 题解 \(Bash\ Game\)模板题 #include<iostream> using namespace std; int T,n,m; int main() { io ...