-
Генерация случайного ключа:
- Python:
import random import string def generate_random_key(length): return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length)) # Usage random_key = generate_random_key(10) print(random_key)
- Python:
-
Ключи шифрования и дешифрования:
- Python (с использованием библиотеки шифрования):
from cryptography.fernet import Fernet def generate_key(): key = Fernet.generate_key() return key def encrypt_key(key, plaintext): cipher_suite = Fernet(key) cipher_text = cipher_suite.encrypt(plaintext.encode()) return cipher_text def decrypt_key(key, cipher_text): cipher_suite = Fernet(key) plain_text = cipher_suite.decrypt(cipher_text).decode() return plain_text # Usage key = generate_key() plaintext = "This is a secret key" encrypted = encrypt_key(key, plaintext) decrypted = decrypt_key(key, encrypted) print(decrypted)
- Python (с использованием библиотеки шифрования):
-
Хеширование ключей:
- Python (с использованием библиотеки hashlib):
import hashlib def hash_key(key): hashed_key = hashlib.sha256(key.encode()).hexdigest() return hashed_key # Usage key = "mysecretkey" hashed_key = hash_key(key) print(hashed_key)
- Python (с использованием библиотеки hashlib):
-
Сравнение ключей:
- Python:
def compare_keys(key1, key2): if key1 == key2: return True else: return False # Usage key1 = "key" key2 = "KEY" result = compare_keys(key1, key2) print(result)
- Python: