0

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. Unfortunately, I am having trouble in decoding the contents of the picture.

This is the original code in 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:

#!/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

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...

15
  • do you get any errors? if so, please include them. Commented Aug 15, 2018 at 4:39
  • If you can include any other information so we can reproduce the problem would be helpful, for example create a Minimal, Complete, and Verifiable example Commented Aug 15, 2018 at 4:41
  • the edit to your code capitalizing directory variable does not improve code formatting. python syntax encourages lower-case variables. Commented Aug 15, 2018 at 4:50
  • @davedwards Alright. Thanks. This is my first time using stack overflow so I am not too familiar on the syntax yet. Btw, I have reedited to provide the source of my problem. Commented Aug 15, 2018 at 4:58
  • 1
    no worries, thanks. but that link is protected by username and password, so we cannot see what you see, the picture, or the problem description or contents. Commented Aug 15, 2018 at 5:08

1 Answer 1

1

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").

Instead, try decoding with "utf-8".

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I have tried with that before but it still doesn't work. I am thinking maybe it has something to do with the byte object.
That's great so if you tried something already, include it in the question so we can focus our help instead of taking wild guesses :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.