Python关于AES的探索,目前涉及2个包(pyaes和Crypto)和两种模式(CBC和CFB)
首先说下AES里Cryto这个包
在CBC下的使用:
<pre>import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import pyaes
class prpcrypt():
def init(self, key):
self.key = key
self.mode = AES.MODE_CBC
# 加密函数,如果text不是16的倍数【加密文本text必须为16的倍数!】,那就补足为16的倍数
def encrypt(self, text):
cryptor = AES.new(self.key, self.mode, self.key)
text = text.encode("utf-8")
# 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
length = 16
count = len(text)
add = length - (count % length)
text = text + (b'\0' * add)
self.ciphertext = cryptor.encrypt(text)
# 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
# 所以这里统一把加密后的字符串转化为16进制字符串
# print(self.ciphertext)
aes = pyaes.AESModeOfOperationCBC(key=b"keyskeyskeyskeys", iv=b"keyskeyskeyskeys")
print(b2a_hex(self.ciphertext).decode("ASCII"))
aes_text = aes.decrypt(self.ciphertext)
print(222222222222222,aes_text)
cryptor = AES.new(self.key, self.mode, self.key)
plain_text = cryptor.decrypt(self.ciphertext)
print(111111111111111111,plain_text)</pre>
<pre>if name == 'main':
pc = prpcrypt('keyskeyskeyskeys') # 初始化密钥
e = pc.encrypt("my book is free")
d = pc.decrypt(e)
上面的例子是网上代码改的,可以看到先用 AES加密再用两个不一样的包分别解密是没有问题的。
特别注意一下子,这里面的key与要加密的内容都必须是按照要求来的,具体要求在注释里了。
之后我们再看下CFB的这种的,从网上继续偷:</pre>
<pre> # -- coding:utf-8 --
from Crypto import Random
from Crypto.Cipher import AES
key = b"61581af471b166682a37efe6"
raw = input('请输入要加密的明文:')
print(len(raw))
iv = b"c8f203fca312aaab" # block_size = 16
cipher = AES.new(key, AES.MODE_CFB, iv,segment_size=128)
data = cipher.encrypt(raw)
print(
"加密后的数据长度:"); # <span style="color:#ff0000;">为什么20个字节长度,不应该是16的整数倍吗?</span><span style="color:#ff0000;">#因为,mode=AES.MODE_CFB</span>
print(len(data))
print("加密后的数据为:");
print(data)
print(len(data))
cipher = AES.new(key, AES.MODE_CFB, iv,segment_size=128);
data1 = cipher.decrypt(data)
print("解密后的数据为:")
print(data1)
datastr = str(data1, 'UTF-8')
print("解密后的明文为:")
print(datastr)
上面的是可以用的,但是输入内容的长度必须为16的倍数。
这里面的128是指代2进制的128位,8位是一个字节,所以是128除以8结果为16
如果要使用pyaes那个包解密则</pre>
<pre>aes = pyaes.AESModeOfOperationCFB(key=b"61581af471b166682a37efe6", iv=b"c8f203fca312aaab", segment_size=16)
aes_text = aes.encrypt(content)
注意segment_size的值,虽然两个包里的方法的参数都一样,但是意义是不同的,一个是指128位,一个是指16个字符,这些东西网上的资料很少几乎查不到。
下面为中医智库的文章破解,保留我测试时候使用的代码,不需要可以删除</pre>
<pre>#coding:utf-8
import requests
from lxml.html import etree
import json
import base64
import pyaes
import zlib
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
url = 'https://www.zk120.com/ji/group/?nav=ahz'
response = requests.get(url)
html = etree.HTML(response.text)
name = html.xpath("//a[@class='ellipsis']/@href")
print(response.text)
print(name)
for i in name:
# print(i)
if 'group' in i:
src = 'https://www.zk120.com'+i
# print(src)
response = requests.get(src)
# print(response.text)
html = etree.HTML(response.text)
urls = html.xpath("//a[@class='mr5 native_read to_reader_url']/@href")
# print(urls)
url_1 = 'https://www.zk120.com'
for u in urls:
# print(u)
uu = u.replace('read','content')
# print(uu)
urll = url_1+uu
# print(urll)
response = requests.get(urll)
# print(response.text)
# 返回json数据
con = json.loads(response.text)
text = con['data']
# print(text)
# 解密
# print len(text)%4
# 判断这本书的内容是否是4X4规格的,如果不是的话,用=补齐16个字符
# missing_padding = 4 - len(text) % 4
# # print(missing_padding)
# if missing_padding:
# text += '=' * missing_padding
# 将分开的内容进行解码
# print(text)
content = base64.b64decode(text.encode('utf-8'))
# print(content)
# text = text.encode("utf-8")
# 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
# content= b',\x0bc\x17\xa3d\xb1+\xeb%_\x15:H\xab\x84'
# print(content)
# print(len(content))
decryptor = AES.new("61581af471b166682a37efe6",AES.MODE_CFB, "c8f203fca312aaab", segment_size=128)
decrypt_text = decryptor.decrypt(content)
# print(11111111111111111111111111111111111111111,decrypt_text,str(decrypt_text, 'utf8'))
# aes = pyaes.AESModeOfOperationCFB(key=b"61581af471b166682a37efe6", iv=b"c8f203fca312aaab", segment_size=16)
# aes_text = aes.encrypt(content)
# print(22222222222222222222222222222222222222222,aes_text)
# 解压缩
text_zip = json.loads(zlib.decompress(decrypt_text))
# 输出结果
text_code = text_zip.get("text").encode("utf-8", "ignore")
print(str(text_code, encoding='utf-8'))
with open('zhongyi.txt', 'a+', encoding='utf-8') as f:
f.write(str(text_code, encoding='utf-8'))
# 'https://www.zk120.com/ji/content/529?uid=None&_=1523528905719'
#
# 'https://www.zk120.com/ji/read/529?nav=ahz&uid=None'
# ur = 'https://www.zk120.com'+'/ji/read/529?nav=ahz&uid=None'
# print(ur)
注意,CFB的正文不必补充为8的倍数了</pre>
网友评论