Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to generate dzi files for painting #2

Open
Stanford997 opened this issue Apr 11, 2024 · 1 comment
Open

failed to generate dzi files for painting #2

Stanford997 opened this issue Apr 11, 2024 · 1 comment

Comments

@Stanford997
Copy link

In generate_dzi.py
function generate_dzi_file_mhj

    encrypted = get_encrypted_text(paint_detail_url)
    decrypted = decrypt(encrypted, key, iv)

When I tried to download the image from 'mhj', I got the error Failed to generate dzi files for painting 5f2540b7eeb94a19b982a1c7a513b6c9: Incorrect AES key length (3 bytes)

Do you know how to solve it?

@10086mea
Copy link

我通过断点调试确定了这个 bug 的原因,网页返回的列表索引发生了变化,原本的索引序号不再适用。我修改了 info 和 decrypted 的索引后,新的 generate_dzi_file_mhj 已经可以正常工作。

I have determined the cause of the bug through breakpoint debugging: the indices of the list returned by the webpage have changed, and the original index numbers are no longer applicable. After modifying the indices of info and decrypted, the new generate_dzi_file_mhj is now working properly.

解释 Explanation
问题原因:

索引变化:由于网页内容更新,导致返回的数据列表 info 和 decrypted 的元素顺序发生了变化。
索引不匹配:原先代码中使用的固定索引已经不再对应正确的数据,导致程序无法正常解密或解析数据。
解决方法:

更新索引:通过断点调试,找出了新的索引位置,使得 info 和 decrypted 列表中的数据能够正确对应程序所需的参数。
修改 info 的索引:找到正确的密钥(key)和初始化向量(iv)在 info 列表中的新位置。
修改 decrypted 的索引:调整解密后数据在 decrypted 列表中的顺序,使其与程序预期的字段对应。
结果:

程序正常运行:更新索引后,generate_dzi_file_mhj 函数能够正确地生成所需的 DZI 文件,程序恢复正常功能。
Cause of the Issue:

Index Changes: The webpage content was updated, causing the order of elements in the returned data lists info and decrypted to change.
Index Mismatch: The fixed indices used in the original code no longer corresponded to the correct data, resulting in the program's inability to decrypt or parse the data properly.
Solution:

Update Indices: Through breakpoint debugging, the new index positions were identified, allowing the data in the info and decrypted lists to correctly match the parameters required by the program.
Modify info Indices: Located the new positions of the correct key (key) and initialization vector (iv) within the info list.
Modify decrypted Indices: Adjusted the order of decrypted data in the decrypted list to correspond with the expected fields in the program.
Outcome:

Program Running Normally: After updating the indices, the generate_dzi_file_mhj function was able to correctly generate the required DZI file, and the program returned to normal functionality.

def generate_dzi_file_mhj(paint_id, info=None):
paint_url = f'https://minghuaji.dpm.org.cn/paint/appreciate?id={paint_id}'
html_string = get_text_from_url(paint_url)
soup = BeautifulSoup(html_string, 'html.parser')
image_items = soup.select_one('#gundong_id').find_all('li')

# get info
if info is None: info = get_info_mhj()

# get key and vi and use them to decrypt the encrypted string
#for i in range(len(info)): #输出info进行测试
#    print(info2bytes(info[i]))
key = info2bytes(info[3])
iv = info2bytes(info[5])

for idx, item in enumerate(image_items):
    paint_detail_url = f'{paint_url}&type={item.get("value")}'

    encrypted = get_encrypted_text(paint_detail_url)
    decrypted = decrypt(encrypted, key, iv)

    # get xmlns and overlap
    xmlns = info2bytes(info[29]).decode('utf-8')
    overlap = info2bytes(info[30]).decode('utf-8')

    # create dzi file
    dzi_info = {
        'xmlns': xmlns,
        'url': decrypted[2],
        'overlap': decrypted[5],
        'tilesize': decrypted[1],
        'format': decrypted[0],
        'width': str(int(float(decrypted[4]))),
        'height': str(int(float(decrypted[3])))
    }
    if len(image_items) == 1:
        dzi_filename = f'{paint_id}.dzi'
    else:
        dzi_filename = f'{paint_id}_{idx}.dzi'
    write_dzi_file(dzi_filename, dzi_info)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants