Programming
python security encryption save python-3.4
Updated Tue, 02 Aug 2022 08:09:05 GMT

writing a variable to a text file and encrypting it?


i know how to save a users input to a text file but how do i encrypt it? here is what i have for saving a users input to text file. i tried f.encrypt("Passwords_log.txt" but had no results

import time
password1 = input("Please type a password: ")
print("Your password has passed the verification!")
time.sleep(1)
print("Saving and encrypting password...")
time.sleep(2)
f=open("Passwords_log.txt",'a')
f.write(password)
f.write('\n')
f.close()
print("Done!")



Solution

There are some Python packages worth checking out that deal with cryptography.

Cryptography

PyCrypto

A simple example from cryptography would be the following:

from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)






External Links

External links referenced by this document: