python - 如何从文件中解码编码为base64的行?
问题描述
我需要解码文件中的行。
到目前为止,这是我的代码:
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
解码线:
[')-.7)-\x06\x06AOO', '-57)-0\x06\x06JASJAOOASJ', ')07)2\x06\x06AJSAJAJOAJJAAO',...]
由于某种原因,我的输出具有单独的所有字母,下面的输出的一小部分等于解码后的 decoded_lines 的第一个列表元素我的输出:
['-', '1', '2', ';', '-', '1', '\n', '\n', 'E', 'S', 'S', ...]
预期输出:
['-12;-1\n\nESS', ...]
有没有办法让行用逗号分隔而不是每个字母用逗号分隔?非硬编码密钥等于 1016。
解决方案
一行中只有一个单词吗?它是一个词吗?它由多个单词组成,然后您的第二个循环应该在单词上,然后第三个循环应该在字母上。代码应该是这样的
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)
推荐阅读
- sql - 如何投射计算字段乘以货币字段?
- tensorflow - 从源代码构建张量流时如何指定“工具链标识符”
- sparql - 如何在 sparql 中使用 kleene 星号运算符 (*) 或其变体 (+) 和变量?
- cuda - 在Anaconda中使用Pytorch-GPU时,是不是不需要安装CUDA?
- types - 如何检查球拍中的类型?
- google-sheets - 用于将列复制到新工作表上的行的脚本,带有每日日志的时间戳
- spring - Spring Boot 2.1.0 和 Java 11 上的 Spring Data Redis 无法正常工作
- xamarin - Xamarin Forms Prism:INavigationService 是否必须在构造函数中传递?除了构造函数注入之外的任何其他方式
- c++ - 在 C++ 中找不到窗口
- html - 使 Image 溢出 hero 元素并对齐到右下角