Python3.6 AES加密 pycrypto 更新为 pycryptodemo | TypeError: Object type <class 'str'> cannot be passed to C code
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @author: rui.xu
# @update: jt.huang
# 这里使用pycryptodemo库
# 安装方法 pip install pycryptodemo from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex class PrpCrypt(object):
def __init__(self, key):
self.key = key.encode('utf-8')
self.mode = AES.MODE_CBC # 加密函数,如果text不足16位就用空格补足为16位,
# 如果大于16当时不是16的倍数,那就补足为16的倍数。
def encrypt(self, text):
text = text.encode('utf-8')
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
# 这里密钥key 长度必须为16(AES-128),
# 24(AES-192),或者32 (AES-256)Bytes 长度
# 目前AES-128 足够目前使用
length = 16
count = len(text)
if count < length:
add = (length - count)
# \0 backspace
# text = text + ('\0' * add)
text = text + ('\0' * add).encode('utf-8')
elif count > length:
add = (length - (count % length))
# text = text + ('\0' * add)
text = text + ('\0' * add).encode('utf-8')
self.ciphertext = cryptor.encrypt(text)
# 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
# 所以这里统一把加密后的字符串转化为16进制字符串
return b2a_hex(self.ciphertext) # 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
cryptor = AES.new(self.key, self.mode, b'0000000000000000')
plain_text = cryptor.decrypt(a2b_hex(text))
# return plain_text.rstrip('\0')
return bytes.decode(plain_text).rstrip('\0') if __name__ == '__main__':
pc = PrpCrypt('keyskeyskeyskeys') # 初始化密钥
e = pc.encrypt("aaa") # 加密
d = pc.decrypt(e) # 解密
print("加密:", e)
print("解密:", d)
Python3.6 AES加密 pycrypto 更新为 pycryptodemo | TypeError: Object type <class 'str'> cannot be passed to C code的更多相关文章
- Python3使用AES加密的库函数PyCrypto、PyCryptodome
我们在网上查看Python爬虫教程的时候,细心的朋友会发现:很多网站为了降低服务器的请求压力都做了各式各样的反爬策略,浏览器通过http post请求服务器端数据时,传输的data字段很多都是经过加密 ...
- 使用Python3进行AES加密和解密 输入的数据
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES, ...
- python3 执行AES加密方法
cmd执行命令:pip install pycryptodome # -*- coding: utf-8 -*- # __author__ = 'Carry' import base64 from C ...
- php的AES加密、解密类
<?php /** * php.ios.Android 通用的AES加密.解密方法 */ namespace Common\Business; class AESCrypt { /** * 初始 ...
- python3.7 AES.MODE_ECB(128位) pkcs5padding 加密算法
用惯用的写法总报TypeEerror错误,经过调试,总算成功啦,直接上代码 TypeError("Object type %s cannot be passed to C code" ...
- python3.6 安装第三方库 pyCryptodome 实现AES加密
起因 前端日子写完的Python入库脚本,通过直接读取配置文件的内容(包含了数据库的ip,数据库的用户名,数据库的密码),因为配置文件中的数据库密码是明文显示的,所以不太安全,由此对其进行加密. 编码 ...
- python3.6 实现AES加密的示例(pyCryptodome)
当然我也是通过官方推荐,使用下面命令去下载安装的,pip就是好用... pip install pycryptodome 撸码开始 废话不多说,直接上demo # from Crypto.Has ...
- Python中crypto模块进行AES加密和解密
#coding: utf8 import sys from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex class p ...
- Python: AES加密与解密
起源: 视频下载,解析到一个网站时,发现其视频id是用AES加密过的,用的是https://code.google.com/archive/p/crypto-js/这个库.解密很简单的一句js代码: ...
随机推荐
- c++ 双向链表 的查找和删除
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> ...
- python基础学习1-随机验证码
import random i=random.randrange(65,90) #根据设置的范围生成随机数字 print(i) c=chr(i)#根据随机产生的数字 然后用chr生成对应ASCII 数 ...
- Storm 第三章 Storm编程案例及Stream Grouping详解
1 功能说明 设计一个topology,来实现对文档里面的单词出现的频率进行统计.整个topology分为三个部分: SentenceSpout:数据源,在已知的英文句子中,随机发送一条句子出去. S ...
- intellIJ IDEA配置maven相关问题记录
IntellIJ IDEA 配置 Maven 以及 修改 默认 Repository 参考:https://www.cnblogs.com/phpdragon/p/7216626.html non-m ...
- SSIS 数据流优化
一,数据流设计优化 数据流有两个特性:流和在内存缓冲区中处理数据,根据数据流的这两个特性,对数据流进行优化. 1,流,同时对数据进行提取,转换和加载操作 流,就是在source提取数据时,转换组件处理 ...
- mongodb原生node驱动
写在前面 最近读<node.js学习指南>,对于mongodb没有介绍太多的工作原理,但是对于一个前端开发者,即使你还没有用过这种数据库也可以让你很好的理解和使用 一本非常好的 ...
- JavaScript 的HTML转义方法 html_encode 和 html_decode
此方法用来将用户输入内容中的尖括号.引号等进行转义
- css选择器分类与作用
本文旨在总结css中各种选择器及其相应用途(持续更新) 通配符(全局)选择器 样式:*{} 示例: 总结:选定文档中所有类型的对象,如图所示写在css样式文件开头用来定义全局通用的一些属性.font- ...
- .Net Core Linux centos7行—jenkins linux 构建.net core web app
1.安装jdk.jenkins 是一个java web程序.所以必然需要jdk. yum install java 或者 yum install java-1.8.0-openjdk 2.下载jenk ...
- python 利用urllib 获取办公区公网Ip
import json,reimport urllib.requestdef GetLocalIP(): IPInfo = urllib.request.urlopen("http://ip ...