简体   繁体   English

如何从文件中解码编码为 base64 的行?

[英]How to decode lines encoded as base64 from file?

I need to decode lines from a file.我需要解码文件中的行。

Here is my code so far:到目前为止,这是我的代码:

    def decode(self) -> list:
        """
        Decode file with key.

        For correct answer you have to convert file lines from base64 to utf-8.

        To decode one line you have to take a UNICODE value of a letter, subtract cipher step and take mod of 255.
        After that you have to convert that number back to a character.
        Example: key = 'test', decoded_data = "+%'"
        '+' -> (43 - 448) % 255 -> 'i' -> ... -> 'ice'

        :return: List of decoded lines
        """
        decoded_lines1 = []
        decoded_lines = []
        lines = self.read_code_from_file()
        for line in lines:
            decoded_lines.append(base64.b64decode(line).decode())
        for decoded_line in decoded_lines:
            for letter in decoded_line:
                decoded_lines1.append(chr((ord(letter) - sum([ord(i) for i in self.key])) % 255))
        return decoded_lines1

decoded_lines:解码行:

[')-.7)-\x06\x06AOO', '-57)-0\x06\x06JASJAOOASJ', ')07)2\x06\x06AJSAJAJOAJJAAO',...]

My output for some reason has all the letters individually, the small portion of output below is equal to the first list element of decoded_lines after decoding My output:我的 output 由于某种原因所有的字母都是单独的,下面 output 的一小部分在解码我的 output 后等于 decoded_lines 的第一个列表元素:

['-', '1', '2', ';', '-', '1', '\n', '\n', 'E', 'S', 'S', ...]

Expected output:预计 output:

['-12;-1\n\nESS', ...]

Is there any way to have the lines seperated by commas rather than each letter seperated by commas?有什么办法可以用逗号分隔行而不是用逗号分隔每个字母? The non-hardcoded key is equal to 1016.非硬编码密钥等于 1016。

The problem is that you append each character, one-by-one to decoded_lines1 when you do问题是你 append 每个字符,一个一个地解码到decoded_lines1当你做

.append(chr((ord(letter) - sum([ord(i) for i in self.key])) % 255))

Instead, you want to do this to each character in decoded_line , make one string out of all the decoded characters, and then append that string to decoded_lines1 .相反,您想对decoded_line中的每个字符执行此操作,从所有解码字符中创建一个字符串然后将该字符串append 到decoded_lines1

for decoded_line in decoded_lines:
    new_line = []
    for letter in decoded_line:
       new_line.append(chr((ord(letter) - sum([ord(i) for i in self.key])) % 255))
    decoded_lines1.append("".join(new_line))

But you can simplify your code further by combining the for line in lines and for decoded_line in decoded_lines loops:但是您可以通过组合for line in linesfor decoded_line in decoded_lines循环来进一步简化代码:

def decode(self):
    decoded_lines = []
    lines = self.read_code_from_file()
    for line in lines:
        decoded_line = base64.b64decode(line).decode()
        new_line = []
        for letter in decoded_line:
            new_line.append(chr((ord(letter) - sum([ord(i) for i in self.key])) % 255))
        decoded_lines.append("".join(new_line))
    return decoded_lines

The problem lies in below code:问题出在下面的代码中:

for decoded_line in decoded_lines:
    for letter in decoded_line:
        decoded_lines1.append(chr((ord(letter) - sum([ord(i) for i in self.key])) % 255))

Here, you are appending letter by letter to the list while you want to append decoded line into the list.在这里,您在将 append 解码行添加到列表中时逐个字母地附加到列表中。

So, I have stored decoded letters into one string and then append it to the list.所以,我将解码后的字母存储到一个字符串中,然后将 append 存储到列表中。

for decoded_line in decoded_lines:
            temp=""
            for letter in decoded_line:
                temp=temp+chr((ord(letter) - sum([ord(i) for i in self.key])) % 255)
            decoded_lines1.append(temp)

Final code will be like below:最终代码将如下所示:

def decode(self) -> list:
        """
        Decode file with key.

        For correct answer you have to convert file lines from base64 to utf-8.

        To decode one line you have to take a UNICODE value of a letter, subtract cipher step and take mod of 255.
        After that you have to convert that number back to a character.
        Example: key = 'test', decoded_data = "+%'"
        '+' -> (43 - 448) % 255 -> 'i' -> ... -> 'ice'

        :return: List of decoded lines
        """
        decoded_lines1 = []
        decoded_lines = []            
        lines = self.read_code_from_file()
        for line in lines:
            decoded_lines.append(base64.b64decode(line).decode())
        for decoded_line in decoded_lines:
            temp=""
            for letter in decoded_line:
                temp=temp+chr((ord(letter) - sum([ord(i) for i in self.key])) % 255)
            decoded_lines1.append(temp)                
        return decoded_lines1

Do you have only a single word in a line?你一行中只有一个词吗? it is a sentence of words?它是一个句子吗? It consists of multiple words then your second loop should be on words then 3rd lop should be on letters.它由多个单词组成,那么你的第二个循环应该在单词上,然后第三个循环应该在字母上。 The code should be something like this代码应该是这样的

def decode(self) -> list:
        """
        Decode file with key.

        For correct answer you have to convert file lines from base64 to utf-8.

        To decode one line you have to take a UNICODE value of a letter, subtract cipher step and take mod of 255.
        After that you have to convert that number back to a character.
        Example: key = 'test', decoded_data = "+%'"
        '+' -> (43 - 448) % 255 -> 'i' -> ... -> 'ice'

        :return: List of decoded lines
        """
        decoded_lines1 = []
        decoded_lines = []
        lines = self.read_code_from_file()
        for line in lines:
            decoded_lines.append(base64.b64decode(line).decode())
        for decoded_line in decoded_lines:
            for word in decoded_line.split():
                docoded_word=''
                for letter in word:
                    docoded_word+=(chr((ord(letter) - sum([ord(i) for i in self.key])) % 255))
                decoded_lines1.append(docoded_word)
        return (decoded_lines1)

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

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