|
@@ -0,0 +1,110 @@
|
|
|
+import sqlite3
|
|
|
+
|
|
|
+def create_tables():
|
|
|
+ conn = sqlite3.connect('alphabets.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute('''CREATE TABLE IF NOT EXISTS alphabets
|
|
|
+ (id INTEGER PRIMARY KEY, name TEXT, alphabet TEXT)''')
|
|
|
+ cursor.execute('''CREATE TABLE IF NOT EXISTS encrypted_texts
|
|
|
+ (id INTEGER PRIMARY KEY, encrypted_text TEXT)''')
|
|
|
+ cursor.execute('''INSERT INTO alphabets (name, alphabet) VALUES
|
|
|
+ ('Полный русский алфавит', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'),
|
|
|
+ ('Без буквы ё', 'абвгдежзийклмнопрстуфхцчшщъыьэюя'),
|
|
|
+ ('С подчеркиванием', 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя_')''')
|
|
|
+ conn.commit()
|
|
|
+ conn.close()
|
|
|
+
|
|
|
+def get_custom_alphabet():
|
|
|
+ length = int(input("Введите длину алфавита: "))
|
|
|
+ alphabet = input(f"Введите свой алфавит ({length} символов): ")
|
|
|
+ if len(alphabet) != length:
|
|
|
+ print("Длина алфавита не соответствует заданной!")
|
|
|
+ return None
|
|
|
+ return alphabet
|
|
|
+
|
|
|
+def get_preset_alphabet(choice):
|
|
|
+ conn = sqlite3.connect('alphabets.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute('SELECT alphabet FROM alphabets WHERE id=?', (choice,))
|
|
|
+ alphabet = cursor.fetchone()[0]
|
|
|
+ conn.close()
|
|
|
+ return alphabet
|
|
|
+
|
|
|
+def save_encrypted_text(text):
|
|
|
+ conn = sqlite3.connect('alphabets.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute('INSERT INTO encrypted_texts (encrypted_text) VALUES (?)', (text,))
|
|
|
+ conn.commit()
|
|
|
+ conn.close()
|
|
|
+
|
|
|
+def get_last_encrypted_text():
|
|
|
+ conn = sqlite3.connect('alphabets.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute('SELECT encrypted_text FROM encrypted_texts ORDER BY id DESC LIMIT 1')
|
|
|
+ encrypted_text = cursor.fetchone()[0]
|
|
|
+ conn.close()
|
|
|
+ return encrypted_text
|
|
|
+
|
|
|
+def vigenere_encrypt(plaintext, key, alphabet):
|
|
|
+ encrypted = ''
|
|
|
+ alphabet_len = len(alphabet)
|
|
|
+ key_indices = [alphabet.index(char) for char in key]
|
|
|
+ for i, char in enumerate(plaintext):
|
|
|
+ if char in alphabet:
|
|
|
+ plain_idx = alphabet.index(char)
|
|
|
+ key_idx = key_indices[i % len(key)]
|
|
|
+ encrypted += alphabet[(plain_idx + key_idx) % alphabet_len]
|
|
|
+ else:
|
|
|
+ encrypted += char
|
|
|
+ return encrypted
|
|
|
+
|
|
|
+def vigenere_decrypt(ciphertext, key, alphabet):
|
|
|
+ decrypted = ''
|
|
|
+ alphabet_len = len(alphabet)
|
|
|
+ key_indices = [alphabet.index(char) for char in key]
|
|
|
+ for i, char in enumerate(ciphertext):
|
|
|
+ if char in alphabet:
|
|
|
+ cipher_idx = alphabet.index(char)
|
|
|
+ key_idx = key_indices[i % len(key)]
|
|
|
+ decrypted += alphabet[(cipher_idx - key_idx) % alphabet_len]
|
|
|
+ else:
|
|
|
+ decrypted += char
|
|
|
+ return decrypted
|
|
|
+
|
|
|
+def main():
|
|
|
+ create_tables()
|
|
|
+
|
|
|
+ choice = int(input("Введите 1 для своего алфавита, 2 для заготовленного: "))
|
|
|
+ if choice == 1:
|
|
|
+ alphabet = get_custom_alphabet()
|
|
|
+ else:
|
|
|
+ preset_choice = int(input("Выберите заготовленный алфавит (1 полный Рсский алфавит без ё, 2 с ё, или 3 с _ в конце ): "))
|
|
|
+ alphabet = get_preset_alphabet(preset_choice)
|
|
|
+
|
|
|
+ if not alphabet:
|
|
|
+ print("Ошибка с алфавитом!")
|
|
|
+ return
|
|
|
+
|
|
|
+ print(f"Ваш алфавит: {alphabet}")
|
|
|
+
|
|
|
+ while True:
|
|
|
+ action = input("Введите 'шифр' для шифрования, 'дешифр' для расшифрования, 'exit' для выхода: ")
|
|
|
+ if action == 'шифр':
|
|
|
+ key = input("Введите ключ: ")
|
|
|
+ plaintext = input("Введите текст для шифрования: ")
|
|
|
+ encrypted_text = vigenere_encrypt(plaintext, key, alphabet)
|
|
|
+ print(f"Зашифрованный текст: {encrypted_text}")
|
|
|
+ save_encrypted_text(encrypted_text)
|
|
|
+ elif action == 'дешифр':
|
|
|
+ key = input("Введите ключ: ")
|
|
|
+ ciphertext = get_last_encrypted_text()
|
|
|
+ decrypted_text = vigenere_decrypt(ciphertext, key, alphabet)
|
|
|
+ print(f"Расшифрованный текст: {decrypted_text}")
|
|
|
+ elif action == 'exit':
|
|
|
+ print("Программа завершена.")
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ print("Неверная команда, попробуйте снова.")
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|