Right now you have something like this:
password = 'supersecret'
send_mail(password = password)
If you encrypt the password it would look something like this:
encrypted_password = 'kasdjhfkauedsjflh'
encryption_key = 'allyourbase'
send_mail(password = decrypt(encrypted_password, encryption_key)
E.g. to send an email:
import yagmail
yag = yagmail.SMTP()
yag.send(contents = ["See picture below", "/local/path/to/img.png"])
To get this done, first install (below) and then register your email into the keyring once by using:
yagmail.register('myemail', 'mypass')
Install:
pip install yagmail # python 2 pip3 install yagmail # python 3
Taking this further we take the encrypted password, use the cryptography library to decrypt it and finally convert it back to a string. ,This tip will walk us through how to import the cryptography module and then use it to encrypt one password and store it in a file. We will then retrieve the encrypted password, use the cryptography module to decrypt the password and then pass it off to SQL Server to be used. ,With the data now written to a binary file we�re going to retrieve the encrypted password and use the previously generated key to decrypt it. Essentially, working in reverse from where we started. Let�s start by retrieving the encrypted password. ,Due to requirements in my environment I need to use SQL Server authentication with my Python scripts instead of Windows trusted authentication. We don�t want a clear text password stored in the Python scripts and we�re not sure how we can secure the passwords.
C: \pip install cryptography
from cryptography.fernet
import Fernet
key = Fernet.generate_key()
print(key)
from cryptography.fernet
import Fernet
key = b 'pRmgMa8T0INjEAfksaq2aafzoZXEuwKI7wDe4c1F8AY='
cipher_suite = Fernet(key)
ciphered_text = cipher_suite.encrypt(b "SuperSecretPassword") #required to be bytes
print(ciphered_text)
from cryptography.fernet
import Fernet
key = b 'pRmgMa8T0INjEAfksaq2aafzoZXEuwKI7wDe4c1F8AY='
cipher_suite = Fernet(key)
ciphered_text = b 'gAAAAABaHvk3g8IG4cln7g5HCulppy1bAPVuhtskVcgPXRyytx6RkIqjcI0mAMA7Oy_56T6J0dk-yjxI_WlZtjxnUBbR-EvoQa_oqCKoQJFbv_uc2WdXMSI='
unciphered_text = (cipher_suite.decrypt(ciphered_text))
print(unciphered_text)
from cryptography.fernet
import Fernet
key = b 'pRmgMa8T0INjEAfksaq2aafzoZXEuwKI7wDe4c1F8AY='
cipher_suite = Fernet(key)
ciphered_text = cipher_suite.encrypt(b 'SuperSecretpassword')
with open('c:\savedfiles\mssqltip_bytes.bin', 'wb') as file_object: file_object.write(ciphered_text)
with open('c:\savedfiles\mssqltip_bytes.bin', 'rb') as file_object:
for line in file_object:
encryptedpwd = line
print(encryptedpwd)
Last Updated : 16 Feb, 2022
Use pip to install maskpass in the command prompt.
pip install maskpass
askpass uses the standard library to get non-blocking input and returns the password.
import maskpass
pwd = maskpass.askpass()
Output:
F: \files > python password.py Enter Password: greeksforgreeks
Decrypt Password: Django doesn’t provide any built-in library or function to decrypt the encrypted password. As decrypting a password is never a good idea.,We will first understand what encryption and decryption are before learning how to encrypt and decrypt passwords in Django.,In Django, there are two methods for encrypting and decrypting passwords.,Encrypt and Decrypt password in Django without using built-in library
Create Project: First, we need to build a Django Project. To do so, open a terminal and type the following command.
django - admin startproject PythonGuides
Create App: Then, we need to build a Django App. To do so, open a terminal and type the following command.
python manage.py startapp CryptoApp
Add Templates: We also need to add the Templates directory in the settings.py file.
'DIRS' = ['Templates']
Create Models: Add the following code inside the models.py file.
from django.db
import models
class Login(models.Model):
email = models.EmailField()
password = models.CharField(max_length = 10)
class Meta:
db_table = "Login"
Register Model: Add the following code in the admin.py file.
from django.contrib
import admin
from.models
import Login
admin.site.register(Login)