换位密码高级应用查询
Transposition Cipher Advanced Application Inquiry
我在尝试对换位密码做一些进一步的应用时遇到了一些问题。我想创建一个程序,允许我使用未知密钥来加密“密码”。虽然我能够设计出一种方法来打印出所有可能的代码,但搜索正确的纯文本太长且乏味了。
因此,我想阅读名为字典的文本文件(其中包括字典中的所有单词)并比较打印的代码以找出一个且唯一正确的答案代码。换句话说,我希望 Python 程序检查打印代码中的所有单词,如果包含在字典中,则将其打印出来。
这是我目前的项目:
from pathlib import Path
my_dictionary = open(Path.home() / 'dictionary.txt', 'r')
my_words = my_dictionary.read()
my_dictionary.close()
cipher_text = 'A ohtsS ommyvt t i nt snat,tcfps eonroaa eseere ro u,nr uiflrudtq arsimdt etnmntles ruocsynmaeae ubgey ediert,mgotrndsmesn trann e bsna d tbr ,wa-tgcmrno p tresf hkga eosulafirwoir aeeie.i r msronoe nstisnnnAsoe(b)er g cg orp geA f le. mcrmoso lew rAa cer a oaan rsyceiaA'
plain_text = ''
key = len(cipher_text)
for u in range(1, key):
divider = int(len(cipher_text)/u)
for k in range(divider):
for i in range(u):
plain_text += cipher_text[k + i*divider]
print(plain_text)
我猜你想要的是你有这么多可能的密文并且你希望其中一个是明文所以你想要一个自动检查程序通过将它的单词与字典,如果这是你想要的,我想这个修改过的程序会对你有所帮助。 阅读评论
from pathlib import Path
import re
import random
my_dictionary = open(Path.home() / 'dictionary.txt', 'r')
# convert all to lower so that casing doesnt matter.
# better to save the result to file so that you dont have to do
# this every time to improve effiency.
my_words = my_dictionary.read().lower()
my_dictionary.close()
# convert all to lower case.
cipher_text = 'A ohtsS ommyvt t i nt snat,tcfps eonroaa eseere ro u,nr uiflrudtq arsimdt etnmntles ruocsynmaeae ubgey ediert,mgotrndsmesn trann e bsna d tbr ,wa-tgcmrno p tresf hkga eosulafirwoir aeeie.i r msronoe nstisnnnAsoe(b)er g cg orp geA f le. mcrmoso lew rAa cer a oaan rsyceiaA'.lower()
# split the ciphertext at words boundries so a,b are two different words a and b.
cipher_text = re.findall(r"[\w']+", cipher_text)
plain_text = ''
length = len(cipher_text) -1
found = 0
# Try 3 times.
for word in range(3):
x = random.randint(0, length)
if cipher_text[x] in my_dictionary:
found += 1
# if found words are 2 or 3 then the ciphertext is propably plaintext.
if found >= 2:
print("[+] Found One result of cipher Text that looks like plaintext")
print("=> ciphertext: {}".format(" ".join(cipher_text)))
另请注意,此程序只会将一个密文与明文进行比较,复杂度为 O(n)
(n 是字典中的单词数),但如果您要比较 v
个密文,它将 O(vn)
这可能很耗时,所以如果您可以使用其他有效的搜索算法或数据结构(例如 dict 或 set)会更好。
我在尝试对换位密码做一些进一步的应用时遇到了一些问题。我想创建一个程序,允许我使用未知密钥来加密“密码”。虽然我能够设计出一种方法来打印出所有可能的代码,但搜索正确的纯文本太长且乏味了。
因此,我想阅读名为字典的文本文件(其中包括字典中的所有单词)并比较打印的代码以找出一个且唯一正确的答案代码。换句话说,我希望 Python 程序检查打印代码中的所有单词,如果包含在字典中,则将其打印出来。
这是我目前的项目:
from pathlib import Path
my_dictionary = open(Path.home() / 'dictionary.txt', 'r')
my_words = my_dictionary.read()
my_dictionary.close()
cipher_text = 'A ohtsS ommyvt t i nt snat,tcfps eonroaa eseere ro u,nr uiflrudtq arsimdt etnmntles ruocsynmaeae ubgey ediert,mgotrndsmesn trann e bsna d tbr ,wa-tgcmrno p tresf hkga eosulafirwoir aeeie.i r msronoe nstisnnnAsoe(b)er g cg orp geA f le. mcrmoso lew rAa cer a oaan rsyceiaA'
plain_text = ''
key = len(cipher_text)
for u in range(1, key):
divider = int(len(cipher_text)/u)
for k in range(divider):
for i in range(u):
plain_text += cipher_text[k + i*divider]
print(plain_text)
我猜你想要的是你有这么多可能的密文并且你希望其中一个是明文所以你想要一个自动检查程序通过将它的单词与字典,如果这是你想要的,我想这个修改过的程序会对你有所帮助。 阅读评论
from pathlib import Path
import re
import random
my_dictionary = open(Path.home() / 'dictionary.txt', 'r')
# convert all to lower so that casing doesnt matter.
# better to save the result to file so that you dont have to do
# this every time to improve effiency.
my_words = my_dictionary.read().lower()
my_dictionary.close()
# convert all to lower case.
cipher_text = 'A ohtsS ommyvt t i nt snat,tcfps eonroaa eseere ro u,nr uiflrudtq arsimdt etnmntles ruocsynmaeae ubgey ediert,mgotrndsmesn trann e bsna d tbr ,wa-tgcmrno p tresf hkga eosulafirwoir aeeie.i r msronoe nstisnnnAsoe(b)er g cg orp geA f le. mcrmoso lew rAa cer a oaan rsyceiaA'.lower()
# split the ciphertext at words boundries so a,b are two different words a and b.
cipher_text = re.findall(r"[\w']+", cipher_text)
plain_text = ''
length = len(cipher_text) -1
found = 0
# Try 3 times.
for word in range(3):
x = random.randint(0, length)
if cipher_text[x] in my_dictionary:
found += 1
# if found words are 2 or 3 then the ciphertext is propably plaintext.
if found >= 2:
print("[+] Found One result of cipher Text that looks like plaintext")
print("=> ciphertext: {}".format(" ".join(cipher_text)))
另请注意,此程序只会将一个密文与明文进行比较,复杂度为 O(n)
(n 是字典中的单词数),但如果您要比较 v
个密文,它将 O(vn)
这可能很耗时,所以如果您可以使用其他有效的搜索算法或数据结构(例如 dict 或 set)会更好。