File size: 2,053 Bytes
1066eaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bdb9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import streamlit as st
import os


URI_OS = os.getenv("MONGODB_LOGIN")

def mongodb_connection(db_name: str, collection_name: str) -> tuple:
    client = MongoClient(URI_OS, server_api=ServerApi('1'))
    db = client[db_name] 
    collection = db[collection_name] 

    try:
        client.admin.command('ping')
        print("MongoDB connection successful")
        return True, collection
    except Exception as e:
        print(f"Error: {e}")
        return False, None
 
def get_keys_from_collection(collection) -> list:
    document = collection.find_one()
    return document.keys()

def get_from_collection(collection, name: str) -> list:
    categories = collection.distinct(name) 
    return categories

def delete_from_collection(collection, id_to_delete: str) -> None:
    collection.delete_one({'_id': id_to_delete})
    return None

ALPHABET = 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
ALPHABET_SIZE = len(ALPHABET)

def normalize_text(text):
    # Приводим к верхнему регистру и убираем символы вне алфавита
    return ''.join([c for c in text.upper() if c in ALPHABET])

def get_shift(char):
    return ALPHABET.index(char)

def vigenere_encrypt(plaintext, key):
    plaintext = normalize_text(plaintext)
    key = normalize_text(key)
    ciphertext = ''
    for i, char in enumerate(plaintext):
        shift = get_shift(key[i % len(key)])
        char_index = get_shift(char)
        new_index = (char_index + shift) % ALPHABET_SIZE
        ciphertext += ALPHABET[new_index]
    return ciphertext

def vigenere_decrypt(ciphertext, key):
    ciphertext = normalize_text(ciphertext)
    key = normalize_text(key)
    plaintext = ''
    for i, char in enumerate(ciphertext):
        shift = get_shift(key[i % len(key)])
        char_index = get_shift(char)
        new_index = (char_index - shift) % ALPHABET_SIZE
        plaintext += ALPHABET[new_index]
    return plaintext