【已解决】Python AES解密求助
本帖最后由 Zhili.An 于 2024-3-26 12:09 编辑工作需要,有一本书的pdf被加密了。我逆向发现,是对pdf读取base64,再进行AES解密就好,但是在python那里解密一直出错;
不知道什么原因。如果对python中的解密函数直接赋值(指就直接输入较短的加密后的值),是可以解密的。代码如下:
python代码1import base64from Crypto.Cipher import AES
def unpad(s):
return s[:-ord(s)]
def decrypt(message, key):
aes = AES.new(key.encode(), AES.MODE_ECB)
message=aes.decrypt(base64.b64decode(message)).decode()
return unpad(message)
key='F79%AKdcDMSixiz4'
# 读取文件并编码为base64格式
with open("10.pdf", "rb") as file:
encoded = base64.b64encode(file.read())
decrypt(encoded, key)
会报错
Traceback (most recent call last):
File "D:\Users\lll\Desktop\jiaoxi\kezhi2.py", line 20, in <module>
decrypt(encoded, key)
File "D:\Users\lll\Desktop\jiaoxi\kezhi2.py", line 11, in decrypt
message=aes.decrypt(base64.b64decode(message)).decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte
后面我就直接把这个base64复制到网站【https://www.toolhelper.cn】中,他是可以解密的,但是得到pdf是空白的
python2:
import requests
import json
import base64
import PyPDF2
key='F79%AKdcDMSixiz4'
# 读取文件并编码为base64格式
with open("10.pdf", "rb") as file:
encoded = base64.b64encode(file.read())
url = "https://www.toolhelper.cn/SymmetricEncryption/AesDecrypt?gts=1711345342000&gv=638469004576249239&r_=0.2978800889153248"
data = {
"cipherMode": 2,
"paddingMode": 2,
"blockSize": 128,
"keyFormat": 1,
"key": "F79%AKdcDMSixiz4",
"ivFormat": 1,
"iv": "1234567891234567",
"associatedDataFormat": 1,
"associatedData": "",
"encoding": "UTF-8",
"outputFormat": 1,
"dest": encoded.decode()
}
response = requests.post(url, data=data)
response_text = response.text
data_dict = json.loads(response_text)
data = data_dict["Data"]
pdf_file = open("00.pdf", "wb")
# 写入原始数据
pdf_file.write(data.encode())
# 关闭文件
pdf_file.close()
文件地址【链接:https://pan.baidu.com/s/1SaTJhPKrzn6O6z1bj6PcsA?pwd=l3if
提取码:l3if】
希望大佬们一起帮助一下的啦。 你这个报错是编码错误啊,如果是中文的书你用gb2312这个编码试试? 7593454 发表于 2024-3-26 11:10
你这个报错是编码错误啊,如果是中文的书你用gb2312这个编码试试?
也提示错误Traceback (most recent call last):
File "D:\Users\lll\Desktop\jiaoxi\112.py", line 18, in <module>
encoded = base64.b64encode(file.read().decode(encoding='gb2312'))
UnicodeDecodeError: 'gb2312' codec can't decode byte 0x8c in position 0: illegal multibyte sequence message = aes.decrypt(base64.b64decode(message)).decode(encoding='Latin-1'),使用这个编码看里面还是乱码的,应该是还有其他的步骤需要处理吧 auth98 发表于 2024-3-26 11:53
message = aes.decrypt(base64.b64decode(message)).decode(encoding='Latin-1'),使用这个编码看里面还是 ...
解决了
pdf_file = open("00.pdf", "wb")
# 写入原始数据
pdf_file.write(c.encode(encoding='Latin-1'))
# 关闭文件
pdf_file.close() 本帖最后由 helian147 于 2024-3-26 13:11 编辑
与base64无关吧
from Crypto.Cipher import AES
def decrypt(message, key):
aes = AES.new(key.encode(), AES.MODE_ECB)
message=aes.decrypt(message)
with open("00.pdf", "wb") as pdf_file:
pdf_file.write(message)
key='F79%AKdcDMSixiz4'
# 读取文件
with open("10.pdf", "rb") as file:
encoded = file.read()
decrypt(encoded, key) helian147 发表于 2024-3-26 13:09
与base64无关吧
from Crypto.Cipher import AES
哇塞!!!!!!!!!!!!!!!!!!!!!!!!!!!超级感谢 感谢楼主与几位大神的分享指点,学习了 message=aes.decrypt(base64.b64decode(message))直接返回utf-8的信息不行吗,为什么还要decode
页:
[1]