來源:https://www.cnblogs.com/mypath/articles/8944301.htmlhtml
首先說下AES裏Cryto這個包json
在CBC下的使用:dom
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 requestsfrom lxml.html import etreeimport jsonimport base64import pyaesimport zlibfrom Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hexurl = '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的倍數了