Advertisement
steve-shambles-2109

Encrypt and decrypt a string

Oct 18th, 2019
1,533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. """
  2. Python code snippets vol 35:
  3. 172-Encrypt and decrypt a string
  4. stevepython.wordpress.com
  5.  
  6. pip3 install cryptography
  7.  
  8. source:
  9. https://stackoverflow.com/questions/27335726/how-do-i-encrypt-and-decrypt-a-string-in-python
  10.  
  11. """
  12. from cryptography.fernet import Fernet
  13.  
  14. key = Fernet.generate_key() #this is your "password"
  15. cipher_suite = Fernet(key)
  16.  
  17. # Enter text to encode here.
  18. encoded_text = cipher_suite.encrypt(b"Your darkest secret")
  19. print("encoded text:\n", encoded_text)
  20.  
  21. #save to text file if you want, or comment out.
  22. with open("enc-text.txt", "wb") as text_file:
  23.     text_file.write(encoded_text)
  24.  
  25. # Print decoded text into shell if required.
  26. decoded_text = cipher_suite.decrypt(encoded_text)
  27. print("\n\ndecoded text:", decoded_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement