Python Code : Encrypt / Decrypt : 8X8

  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functionality of this Python Code.

1. Ensure that the user who is encrypting the file is a valid IDM user authorized on the Local linux machine.
2. Generate the cryptographic key that uses the fernet module to encrypt the file.
3. Encrypts the Given files 


Code ....

from __future__ import unicode_literals

import io

import string

from io import StringIO

import sys

import os

import pam


##### Functions to create the filekey.key


def generate_fernet_key():

   import os

   import os.path


   location = os.getcwd()

   path = 'location/filekey.key'

   isFile = os.path.isfile(path)

   if isFile == "True":

     os.remove("filekey.key")

   else:

     from cryptography.fernet import Fernet

     # key generation

     key = Fernet.generate_key()


     # string the key in a file

     with open('filekey.key', 'wb') as filekey:

       filekey.write(key)


##### End fo  filekey.key


##### function to encrypt file


def encrypt_file():

   # import required module

   from cryptography.fernet import Fernet


   # opening the key

   with open('filekey.key', 'rb') as filekey:

     key = filekey.read()


   # using the generated key

     fernet = Fernet(key)


   # opening the original file to encrypt

     with open('volt.txt', 'rb') as file:

       original = file.read()


   # encrypting the file

     encrypted = fernet.encrypt(original)


   # opening the file in write mode and

   # writing the encrypted data

     with open('volt.txt', 'wb') as encrypted_file:

       encrypted_file.write(encrypted)


#### End of Encrypt


import getpass

user = getpass.getuser()

print("==================================================================================")

print("Hello !!! " "'" + user + "'" "," "  Enter your Password to confirm it is you !!!"  )

print("==================================================================================")

print('')

from getpass import getuser, getpass

if pam.authenticate(getuser(), getpass()):

   result = "Success"

   print(result)


   generate_fernet_key()

   encrypt_file()


else:

   print("===========================")

   print('Password Incorrect')

   print("===========================")

   print('')

   print("                 *******    *******                  ")

   print("You are NOT AUTHORIZED to Encrypt / Decrypt any files")

   print("-----------------------------------------------------")










Comments

Popular posts from this blog

Datatypes

PYTHON : In 4 hours

Complete Python Developer in 2021 : Zero to Mastery :::