來源:https://www.cnblogs.com/mypath/articles/8944301.html
首先說下AES里Cryto這個包
在CBC下的使用:
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)
if __name__ == '__main__':
pc = prpcrypt('keyskeyskeyskeys') # 初始化密鑰
e = pc.encrypt("my book is free")
d = pc.decrypt(e)
上面的例子是網上代碼改的,可以看到先用 AES加密再用兩個不一樣的包分別解密是沒有問題的。
特別注意一下子,這里面的key與要加密的內容都必須是按照要求來的,具體要求在注釋里了。
之后我們再看下CFB的這種的,從網上繼續偷:
# -*- 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那個包解密則
aes = pyaes.AESModeOfOperationCFB(key=b"61581af471b166682a37efe6", iv=b"c8f203fca312aaab", segment_size=16)
aes_text = aes.encrypt(content)
注意segment_size的值,雖然兩個包里的方法的參數都一樣,但是意義是不同的,一個是指128位,一個是指16個字符,這些東西網上的資料很少幾乎查不到。
下面為中醫智庫的文章破解,保留我測試時候使用的代碼,不需要可以刪除
#_*_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的倍數了
