私は数日間このコードを作業していましたが、結果はありませんでした。 autodecrypt
は、ドメインA-Zおよびa-zの文字が数字offset
によって変更されたencrypt
によって暗号化された文字列を受け取ります。したがって、ASCIIコードでは 'A'は65となり、offset
= 7なら 'A'は現在 'H'(コード番号は72)になります。 0〜95のオフセット値が試されます。復号化は、単語の85%以上がdictionary.txt
と呼ばれるtxtファイルに現れていれば、基本的に多くの一般的な単語が含まれているかどうかチェックされます。これは私の問題がある場所です:私が生成した文字列がtxtファイルにあるかどうかは正しくチェックされていません。txtファイルpythonで検索
def autodecrypt(ciphertext):
text = list(ciphertext)
t = open ('dictionary.txt', 'r')
m = t.read()
diccond = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ' #initializing variables
ntext = []
ctext = ''
i = 0
offset = 0
check = 0
while offset <= 95: #cycling through 95 offset values
while i < len(text): #decrypting ciphertext with given offset value
r = ord(text[i])
if r - offset < 32:
s = chr(127 - (offset - (r - 32)))
else:
s = chr(r - offset)
ntext.append(s)
i+=1
stext = ''.join(ntext) #make decrypted list (ntext) into string, stext
for j in stext: #removing punctuation and store in new string, ctext
if j in diccond:
ctext += j
cltext = ctext.lower() #lowercasing ctext string
for k in (cltext.split(' ')): #checking if ctext, list version, is in txt file
if k in m:
check +=1
if check/len(ctext.split(' ')) >= 0.85: #checking if 85% or over of ctext string version in txt file
return stext
else: #else if there is not any fail and return cipher text (see below)
fail = 0
check = 0 #resetting all variables
ctext = ''
ntext=[]
i=0
offset +=1 #increasing offset
if fail == 0:
return ciphertext
また、85%以上一致しない場合、元の暗号化された文字列が返されます。
直ちに問題になるのは、(別のループ内の)ループで 'm = t.read()'を使用していますが、最初の繰り返しの後に 't.read()'は空文字列を返します。最初にファイルを読み、その結果を使用するだけです。それが最もきれいなアプローチです。また、おそらくあなたの辞書の中の単語を 'set'の中に入れておくべきです。それは痛いほど遅くはありません。 –
ええ、私は投稿後にそれを編集しましたが、それでも動作しません。具体的には、元のテキストが「Je pense、donc、j suis」であり、暗号化されたテキストが「Wr-」であるこのテストケースの場合、{r9!q | {p9-wr - !#v !; ' '。元のテキストの単語はtxtファイルにないので、 '' f "を返しますが、暗号化されたテキストを返すべきです。H <!、+ H <\ '" <02&0J " – AbdulM
あなたのコードは です。ciphertext = 'Wr-} r {!r9-q | {p9-wr - !#v !;'空のdictionary.txtは正しい結果を 'Wr-} r {!r9-q | {p9-wr - !#v !;'}として返します。 より具体的なテスト用データが役立ちます。 辞書ファイルで「je」を検索しようとすると、ヒットする「meje」のような単語が含まれています。 – DahliaSR