Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,107 @@
|
|
| 1 |
-
# 导入所需的库
|
| 2 |
-
from pycipher import SimpleSubstitution as SimpleSub
|
| 3 |
-
import random
|
| 4 |
-
import re
|
| 5 |
-
from ngram_score import ngram_score
|
| 6 |
-
import proability
|
| 7 |
-
import gradio as gr
|
| 8 |
-
|
| 9 |
-
def decrypt_text_internal(ciphertext):
|
| 10 |
-
fitness = ngram_score('quadgrams.txt')
|
| 11 |
-
ctext = re.sub('[^A-Z]', '', ciphertext.upper())
|
| 12 |
-
maxkey = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
| 13 |
-
maxscore = -99e9
|
| 14 |
-
parentscore, parentkey = maxscore, maxkey[:]
|
| 15 |
-
|
| 16 |
-
i = 0
|
| 17 |
-
# Limit iterations to prevent infinite loops in a web server environment
|
| 18 |
-
while i < 1000:
|
| 19 |
-
i = i + 1
|
| 20 |
-
random.shuffle(parentkey)
|
| 21 |
-
deciphered = SimpleSub(parentkey).decipher(ctext)
|
| 22 |
-
parentscore = fitness.score(deciphered)
|
| 23 |
-
count = 0
|
| 24 |
-
while count < 1000:
|
| 25 |
-
a = random.randint(0, 25)
|
| 26 |
-
b = random.randint(0, 25)
|
| 27 |
-
child = parentkey[:]
|
| 28 |
-
child[a], child[b] = child[b], child[a]
|
| 29 |
-
deciphered = SimpleSub(child).decipher(ctext)
|
| 30 |
-
score = fitness.score(deciphered)
|
| 31 |
-
if score > parentscore:
|
| 32 |
-
parentscore = score
|
| 33 |
-
parentkey = child[:]
|
| 34 |
-
count = 0
|
| 35 |
-
count = count + 1
|
| 36 |
-
if parentscore > maxscore:
|
| 37 |
-
maxscore, maxkey = parentscore, parentkey[:]
|
| 38 |
-
ss = SimpleSub(maxkey)
|
| 39 |
-
plaintext = ss.decipher(ctext)
|
| 40 |
-
plaintext1 = add_punctuation_and_spaces(ciphertext, plaintext)
|
| 41 |
-
# In a web context, we return the first good result.
|
| 42 |
-
# The original loop was infinite, which is not suitable for a server.
|
| 43 |
-
return plaintext1
|
| 44 |
-
# Fallback if no good solution is found within the iteration limit
|
| 45 |
-
ss = SimpleSub(maxkey)
|
| 46 |
-
plaintext = ss.decipher(ctext)
|
| 47 |
-
return add_punctuation_and_spaces(ciphertext, plaintext)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def output(string1, dic, string2):
|
| 51 |
-
modified_string1 = list(string1)
|
| 52 |
-
modified_string2 = list(string2)
|
| 53 |
-
for i in range(len(string1)):
|
| 54 |
-
if modified_string1[i] in dic and modified_string2[i] != ' ':
|
| 55 |
-
modified_string2[i] = dic[modified_string1[i]]
|
| 56 |
-
modified_string2 = ''.join(modified_string2)
|
| 57 |
-
return modified_string2
|
| 58 |
-
|
| 59 |
-
def add_punctuation_and_spaces(ciphertext, plaintext):
|
| 60 |
-
result = ""
|
| 61 |
-
j = 0
|
| 62 |
-
for i in range(len(ciphertext)):
|
| 63 |
-
if not ciphertext[i].isalpha():
|
| 64 |
-
result += ciphertext[i]
|
| 65 |
-
else:
|
| 66 |
-
if ciphertext[i].islower():
|
| 67 |
-
result += plaintext[j].lower()
|
| 68 |
-
else:
|
| 69 |
-
# The original code had a bug here, always making it lowercase.
|
| 70 |
-
# This is a guess at the intended behavior.
|
| 71 |
-
result += plaintext[j]
|
| 72 |
-
j += 1
|
| 73 |
-
return result
|
| 74 |
-
|
| 75 |
-
def decrypt_interface(ciphertext, key):
|
| 76 |
-
"""
|
| 77 |
-
This is the main function that will be exposed through the Gradio interface.
|
| 78 |
-
"""
|
| 79 |
-
if not ciphertext:
|
| 80 |
-
return "Please enter some ciphertext."
|
| 81 |
-
|
| 82 |
-
plaintext = decrypt_text_internal(ciphertext)
|
| 83 |
-
|
| 84 |
-
if key:
|
| 85 |
-
try:
|
| 86 |
-
key_dic = proability.read_key(key)
|
| 87 |
-
plaintext = output(ciphertext, key_dic, plaintext)
|
| 88 |
-
except Exception as e:
|
| 89 |
-
return f"Error processing key: {e}. Please check the key format (e.g., a=B c=D)."
|
| 90 |
-
|
| 91 |
-
return plaintext
|
| 92 |
-
|
| 93 |
-
# Create the Gradio interface
|
| 94 |
-
iface = gr.Interface(
|
| 95 |
-
fn=decrypt_interface,
|
| 96 |
-
inputs=[
|
| 97 |
-
gr.Textbox(lines=10, label="Ciphertext", placeholder="Enter the text to decrypt..."),
|
| 98 |
-
gr.Textbox(lines=2, label="Known Key Mappings (Optional)", placeholder="e.g., a=B c=D")
|
| 99 |
-
],
|
| 100 |
-
outputs=gr.Textbox(lines=10, label="Plaintext"),
|
| 101 |
-
title="Simple Substitution Cipher Decryptor",
|
| 102 |
-
description="An automatic decryption tool for simple substitution ciphers. You can optionally provide known letter mappings to improve accuracy."
|
| 103 |
-
)
|
| 104 |
-
|
| 105 |
-
# Launch the app
|
| 106 |
-
if __name__ == "__main__":
|
| 107 |
-
iface.launch(server_name="127.0.0.1", server_port=
|
|
|
|
| 1 |
+
# 导入所需的库
|
| 2 |
+
from pycipher import SimpleSubstitution as SimpleSub
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
from ngram_score import ngram_score
|
| 6 |
+
import proability
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
def decrypt_text_internal(ciphertext):
|
| 10 |
+
fitness = ngram_score('quadgrams.txt')
|
| 11 |
+
ctext = re.sub('[^A-Z]', '', ciphertext.upper())
|
| 12 |
+
maxkey = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
|
| 13 |
+
maxscore = -99e9
|
| 14 |
+
parentscore, parentkey = maxscore, maxkey[:]
|
| 15 |
+
|
| 16 |
+
i = 0
|
| 17 |
+
# Limit iterations to prevent infinite loops in a web server environment
|
| 18 |
+
while i < 1000:
|
| 19 |
+
i = i + 1
|
| 20 |
+
random.shuffle(parentkey)
|
| 21 |
+
deciphered = SimpleSub(parentkey).decipher(ctext)
|
| 22 |
+
parentscore = fitness.score(deciphered)
|
| 23 |
+
count = 0
|
| 24 |
+
while count < 1000:
|
| 25 |
+
a = random.randint(0, 25)
|
| 26 |
+
b = random.randint(0, 25)
|
| 27 |
+
child = parentkey[:]
|
| 28 |
+
child[a], child[b] = child[b], child[a]
|
| 29 |
+
deciphered = SimpleSub(child).decipher(ctext)
|
| 30 |
+
score = fitness.score(deciphered)
|
| 31 |
+
if score > parentscore:
|
| 32 |
+
parentscore = score
|
| 33 |
+
parentkey = child[:]
|
| 34 |
+
count = 0
|
| 35 |
+
count = count + 1
|
| 36 |
+
if parentscore > maxscore:
|
| 37 |
+
maxscore, maxkey = parentscore, parentkey[:]
|
| 38 |
+
ss = SimpleSub(maxkey)
|
| 39 |
+
plaintext = ss.decipher(ctext)
|
| 40 |
+
plaintext1 = add_punctuation_and_spaces(ciphertext, plaintext)
|
| 41 |
+
# In a web context, we return the first good result.
|
| 42 |
+
# The original loop was infinite, which is not suitable for a server.
|
| 43 |
+
return plaintext1
|
| 44 |
+
# Fallback if no good solution is found within the iteration limit
|
| 45 |
+
ss = SimpleSub(maxkey)
|
| 46 |
+
plaintext = ss.decipher(ctext)
|
| 47 |
+
return add_punctuation_and_spaces(ciphertext, plaintext)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def output(string1, dic, string2):
|
| 51 |
+
modified_string1 = list(string1)
|
| 52 |
+
modified_string2 = list(string2)
|
| 53 |
+
for i in range(len(string1)):
|
| 54 |
+
if modified_string1[i] in dic and modified_string2[i] != ' ':
|
| 55 |
+
modified_string2[i] = dic[modified_string1[i]]
|
| 56 |
+
modified_string2 = ''.join(modified_string2)
|
| 57 |
+
return modified_string2
|
| 58 |
+
|
| 59 |
+
def add_punctuation_and_spaces(ciphertext, plaintext):
|
| 60 |
+
result = ""
|
| 61 |
+
j = 0
|
| 62 |
+
for i in range(len(ciphertext)):
|
| 63 |
+
if not ciphertext[i].isalpha():
|
| 64 |
+
result += ciphertext[i]
|
| 65 |
+
else:
|
| 66 |
+
if ciphertext[i].islower():
|
| 67 |
+
result += plaintext[j].lower()
|
| 68 |
+
else:
|
| 69 |
+
# The original code had a bug here, always making it lowercase.
|
| 70 |
+
# This is a guess at the intended behavior.
|
| 71 |
+
result += plaintext[j]
|
| 72 |
+
j += 1
|
| 73 |
+
return result
|
| 74 |
+
|
| 75 |
+
def decrypt_interface(ciphertext, key):
|
| 76 |
+
"""
|
| 77 |
+
This is the main function that will be exposed through the Gradio interface.
|
| 78 |
+
"""
|
| 79 |
+
if not ciphertext:
|
| 80 |
+
return "Please enter some ciphertext."
|
| 81 |
+
|
| 82 |
+
plaintext = decrypt_text_internal(ciphertext)
|
| 83 |
+
|
| 84 |
+
if key:
|
| 85 |
+
try:
|
| 86 |
+
key_dic = proability.read_key(key)
|
| 87 |
+
plaintext = output(ciphertext, key_dic, plaintext)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"Error processing key: {e}. Please check the key format (e.g., a=B c=D)."
|
| 90 |
+
|
| 91 |
+
return plaintext
|
| 92 |
+
|
| 93 |
+
# Create the Gradio interface
|
| 94 |
+
iface = gr.Interface(
|
| 95 |
+
fn=decrypt_interface,
|
| 96 |
+
inputs=[
|
| 97 |
+
gr.Textbox(lines=10, label="Ciphertext", placeholder="Enter the text to decrypt..."),
|
| 98 |
+
gr.Textbox(lines=2, label="Known Key Mappings (Optional)", placeholder="e.g., a=B c=D")
|
| 99 |
+
],
|
| 100 |
+
outputs=gr.Textbox(lines=10, label="Plaintext"),
|
| 101 |
+
title="Simple Substitution Cipher Decryptor",
|
| 102 |
+
description="An automatic decryption tool for simple substitution ciphers. You can optionally provide known letter mappings to improve accuracy."
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Launch the app
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
iface.launch(server_name="127.0.0.1", server_port=7860)
|