encryption and decryption python code example
Example 1: encrypt and decrypt python
from cryptography.fernet import Fernet
message = "my deep dark secret".encode()
f = Fernet(key)
encrypted = f.encrypt(message)
from cryptography.fernet import Fernet
encrypted = b"...encrypted bytes..."
f = Fernet(key)
decrypted = f.decrypt(encrypted)
Example 2: encryption using python
import random
text = input ( "Enter text: " )
result = ""
private_key = ""
for i in text:
rand = random.randint ( 1, 125 )
en = rand + ord ( i )
en = chr ( en )
en = str ( en )
private_key = private_key + str ( rand ) + " "
result = result + en
print ( "\nPublic key:", result )
print ( "Private key:", private_key )
Example 3: how to encrypt text in python
text = input()
def encrypt(t):
chars = list(text)
allowed_characters = list(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?!")
for char in chars:
for i in allowed_characters:
if char == i:
chars[chars.index(char)] = allowed_characters.index(i)
return chars
print(encrypt(text))