Код IT
← Каталог

Шифрование — Пример шифрования

Фрагмент из «Шифрование»: Пример шифрования.

python infra-securityencyclopedia8-07-informatsionnaya-bezopasnost-115 embed URL статья в энциклопедии
Python main.py
from cryptography.fernet import Fernet

# Генерация ключа
key = Fernet.generate_key()
cipher = Fernet(key)

# Исходный текст
plaintext = b"Secret message"
print(f"Открытый текст: {plaintext.decode()}")

# Шифрование
ciphertext = cipher.encrypt(plaintext)
print(f"Шифротекст: {ciphertext}")

# Расшифрование
decrypted = cipher.decrypt(ciphertext)
print(f"Расшифрованный текст: {decrypted.decode()}")
from cryptography.fernet import Fernet

# Генерация ключа
key = Fernet.generate_key()
cipher = Fernet(key)

# Исходный текст
plaintext = b"Secret message"
print(f"Открытый текст: {plaintext.decode()}")

# Шифрование
ciphertext = cipher.encrypt(plaintext)
print(f"Шифротекст: {ciphertext}")

# Расшифрование
decrypted = cipher.decrypt(ciphertext)
print(f"Расшифрованный текст: {decrypted.decode()}")