确定ROT编码
Determine ROT encoding
我想确定使用了哪种类型的 ROT 编码,并基于此进行正确的解码。
此外,我发现以下代码确实可以将 rot13 "sbbone" 正确解码为 "foobart":
import codecs
codecs.decode('sbbone', 'rot_13')
问题是我想 运行 这个 python 文件与现有的具有 rot13 编码的文件。 (例如 rot13.py encoded.txt)。
谢谢!
嗯,你可以逐行读取文件并解码。
输出应转到输出文件:
import codecs
import sys
def main(filename):
output_file = open('output_file.txt', 'w')
with open(filename) as f:
for line in f:
output_file.write(codecs.decode(line, 'rot_13'))
output_file.close()
if __name__ == "__main__":
_filename = sys.argv[1]
main(_filename)
要回答第一个问题的第二部分,解码 ROT-x
中的内容,您可以使用以下代码:
def encode(s, ROT_number=13):
"""Encodes a string (s) using ROT (ROT_number) encoding."""
ROT_number %= 26 # To avoid IndexErrors
alpha = "abcdefghijklmnopqrstuvwxyz" * 2
alpha += alpha.upper()
def get_i():
for i in range(26):
yield i # indexes of the lowercase letters
for i in range(53, 78):
yield i # indexes of the uppercase letters
ROT = {alpha[i]: alpha[i + ROT_number] for i in get_i()}
return "".join(ROT.get(i, i) for i in s)
def decode(s, ROT_number=13):
"""Decodes a string (s) using ROT (ROT_number) encoding."""
return encrypt(s, abs(ROT_number % 26 - 26))
要回答你第一个问题的第一部分,找到任意编码字符串的rot编码,你可能想要暴力破解。使用所有的rot-encodings,并检查哪一个最有意义。一种快速(-ish)的方法是获取包含英语中最常见单词的 space 分隔(例如 cat\ndog\nmouse\nsheep\nsay\nsaid\nquick\n...
,其中 \n
是换行符)文件,然后检查哪个编码中的单词最多。
with open("words.txt") as f:
words = frozenset(f.read().lower().split("\n"))
# frozenset for speed
def get_most_likely_encoding(s, delimiter=" "):
alpha = "abcdefghijklmnopqrstuvwxyz" + delimiter
for punctuation in "\n\t,:; .()":
s.replace(punctuation, delimiter)
s = "".join(c for c in s if c.lower() in alpha)
word_count = [sum(w.lower() in words for w in encode(
s, enc).split(delimiter)) for enc in range(26)]
return word_count.index(max(word_count))
您可以使用的 Unix 机器上的文件是 /usr/dict/words
, which can also be found here
我想确定使用了哪种类型的 ROT 编码,并基于此进行正确的解码。
此外,我发现以下代码确实可以将 rot13 "sbbone" 正确解码为 "foobart":
import codecs
codecs.decode('sbbone', 'rot_13')
问题是我想 运行 这个 python 文件与现有的具有 rot13 编码的文件。 (例如 rot13.py encoded.txt)。
谢谢!
嗯,你可以逐行读取文件并解码。 输出应转到输出文件:
import codecs
import sys
def main(filename):
output_file = open('output_file.txt', 'w')
with open(filename) as f:
for line in f:
output_file.write(codecs.decode(line, 'rot_13'))
output_file.close()
if __name__ == "__main__":
_filename = sys.argv[1]
main(_filename)
要回答第一个问题的第二部分,解码 ROT-x
中的内容,您可以使用以下代码:
def encode(s, ROT_number=13):
"""Encodes a string (s) using ROT (ROT_number) encoding."""
ROT_number %= 26 # To avoid IndexErrors
alpha = "abcdefghijklmnopqrstuvwxyz" * 2
alpha += alpha.upper()
def get_i():
for i in range(26):
yield i # indexes of the lowercase letters
for i in range(53, 78):
yield i # indexes of the uppercase letters
ROT = {alpha[i]: alpha[i + ROT_number] for i in get_i()}
return "".join(ROT.get(i, i) for i in s)
def decode(s, ROT_number=13):
"""Decodes a string (s) using ROT (ROT_number) encoding."""
return encrypt(s, abs(ROT_number % 26 - 26))
要回答你第一个问题的第一部分,找到任意编码字符串的rot编码,你可能想要暴力破解。使用所有的rot-encodings,并检查哪一个最有意义。一种快速(-ish)的方法是获取包含英语中最常见单词的 space 分隔(例如 cat\ndog\nmouse\nsheep\nsay\nsaid\nquick\n...
,其中 \n
是换行符)文件,然后检查哪个编码中的单词最多。
with open("words.txt") as f:
words = frozenset(f.read().lower().split("\n"))
# frozenset for speed
def get_most_likely_encoding(s, delimiter=" "):
alpha = "abcdefghijklmnopqrstuvwxyz" + delimiter
for punctuation in "\n\t,:; .()":
s.replace(punctuation, delimiter)
s = "".join(c for c in s if c.lower() in alpha)
word_count = [sum(w.lower() in words for w in encode(
s, enc).split(delimiter)) for enc in range(26)]
return word_count.index(max(word_count))
您可以使用的 Unix 机器上的文件是 /usr/dict/words
, which can also be found here