简体   繁体   English

解码功能中python2和python3之间的区别

[英]Difference between python2 and python3 in the decode function

Recently, I watched a video about writing a code to decode the title and contents of the file from decimal numbers into strings. 最近,我观看了一段有关编写代码以将文件标题和内容从十进制数字解码为字符串的视频。 However, it is written in python2 so I decided to rewrite the code in python3. 但是,它是用python2编写的,所以我决定用python3重写代码。 Unfortunately, I am having trouble in decoding the contents of the picture. 不幸的是,我在解码图片内容时遇到了麻烦。

This is the original code in python2: 这是python2中的原始代码:

#!/usr/bin/env python

import os

directory = '1262404985085867488371'

def decrypt(number): 
    return hex(int(number))[2:].replace("L","").decode("hex")


os.chdir(directory)
for i in os.listdir('.'):
    try:
        print(decrypt(i))
        c = open(i).read()
        open(decrypt(i),'w').write(decrypt(c))
        #o.write(decrypt(c))

    except:
        print("FAILED WITH",i)

And this is the code written in python3: 这是用python3编写的代码:

#!/usr/bin/env python3

import os

directory = '1262404985085867488371'

def decrypt(number): 
    hex_num = hex(int(number))[2:].replace("L","")
    return  bytes.fromhex(hex_num).decode("ascii")


os.chdir(directory)
for i in os.listdir('.'):
    try:
        print(decrypt(i))
        c = open(i).read()
        open(decrypt(i),'w').write(decrypt(c))
        #o.write(decrypt(c))

    except:
        print("FAILED WITH",i)

Can anyone help me to have a look how can I solve this problem? 谁能帮我看看如何解决这个问题? This is the problem about: 这是关于以下问题:

My computer got infected with ransomware and now none of my documents are accessible anymore! 我的计算机感染了勒索软件,现在我的文档都无法访问了! If you help me out, I'll reward you a flag! 如果您帮助我,我会奖励您一个标志! https://static.tjctf.org/7459b0c272ba30c9fea94391c7d7051d78e1732c871c3a6f27070fcb34f9e734_encrypted.tar.gz https://static.tjctf.org/7459b0c272ba30c9fea94391c7d7051d78e1732c871c3a6f27070fcb34f9e734_encrypted.tar.gz

Basically, I have tried by changing the ascii into utf-8 and open the file with the mode in "wb" or "rb" but neither of them works... 基本上,我尝试过将ascii更改为utf-8,并使用“ wb”或“ rb”模式打开文件,但它们都不起作用...

I'm going to jump the gun here and since you did not include any examples and\\or errors I will guess you got this kind of error: 我将在这里开枪,由于您没有提供任何示例和/或错误,我想您会遇到这种错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 0: ordinal not in range(128)

This is caused by a file\\directory with a unicode name, which in turn triggers this exception when you try to decode with ascii as in decode("ascii") . 这是由具有Unicode名称的file \\ directory引起的,当您尝试使用ascii进行解码时,反过来会触发此异常,如decode("ascii")

Instead, try decoding with "utf-8". 而是尝试使用“ utf-8”进行解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM