吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 512|回复: 8
收起左侧

[求助] 【已解决】Python AES解密求助

[复制链接]
Zhili.An 发表于 2024-3-26 10:58
本帖最后由 Zhili.An 于 2024-3-26 12:09 编辑

工作需要,有一本书的pdf被加密了。我逆向发现,是对pdf读取base64,再进行AES解密就好,但是在python那里解密一直出错;
不知道什么原因。如果对python中的解密函数直接赋值(指就直接输入较短的加密后的值),是可以解密的。代码如下:
python代码1
[Python] 纯文本查看 复制代码
import base64from Crypto.Cipher import AES

def unpad(s):
    return s[:-ord(s[len(s)-1])]

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)

会报错
[Python] 纯文本查看 复制代码
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:
[Python] 纯文本查看 复制代码
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
希望大佬们一起帮助一下的啦。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

7593454 发表于 2024-3-26 11:10
你这个报错是编码错误啊,如果是中文的书你用gb2312这个编码试试?
 楼主| Zhili.An 发表于 2024-3-26 11:22
7593454 发表于 2024-3-26 11:10
你这个报错是编码错误啊,如果是中文的书你用gb2312这个编码试试?

也提示错误
[Python] 纯文本查看 复制代码
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
auth98 发表于 2024-3-26 11:53
message = aes.decrypt(base64.b64decode(message)).decode(encoding='Latin-1'),使用这个编码看里面还是乱码的,应该是还有其他的步骤需要处理吧
 楼主| Zhili.An 发表于 2024-3-26 12:08
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:09
本帖最后由 helian147 于 2024-3-26 13:11 编辑

与base64无关吧

[Python] 纯文本查看 复制代码
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)
 楼主| Zhili.An 发表于 2024-3-26 14:24
helian147 发表于 2024-3-26 13:09
与base64无关吧

[mw_shl_code=python,true]from Crypto.Cipher import AES

哇塞!!!!!!!!!!!!!!!!!!!!!!!!!!!超级感谢
52PJ070 发表于 2024-3-26 16:15
感谢楼主与几位大神的分享指点,学习了
江男 发表于 2024-3-26 16:50
message=aes.decrypt(base64.b64decode(message))直接返回utf-8的信息不行吗,为什么还要decode
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 23:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表