Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/python-cipher/main.py
5925 views
1
from cryptography.fernet import Fernet
2
3
key = Fernet.generate_key()
4
print(key)
5
6
cipher = Fernet(key)
7
text = b'Hi everyone!!! This is me again!!!'
8
9
secured_text = cipher.encrypt(text)
10
print(secured_text)
11
12
decrypted_text = cipher.decrypt(secured_text)
13
print(decrypted_text)
14
15
# ----------------------------------------------------
16
17
with open('my_key.txt', 'wb') as f:
18
f.write(key)
19
20
text_to_secure = open('stih.txt', 'rb').read()
21
print(text_to_secure)
22
23
with open('my_secured_text.txt', 'wb') as f:
24
f.write(cipher.encrypt(text_to_secure))
25
26
text_to_unsecure = open('my_secured_text.txt', 'rb').read()
27
print(text_to_unsecure)
28
29
with open('my_unsecured_text.txt', 'wb') as f:
30
f.write(cipher.decrypt(text_to_unsecure))
31
32
33