0% found this document useful (0 votes)
90 views

Python Basic Workbook OWTP

This document describes how to create a basic login panel in Python that stores username and password credentials in a CSV file. It includes functions to: 1. Take username and password inputs from the user. 2. Check the inputs against the credentials in the CSV file using the CSV module. 3. Print a success message if the credentials match or ask the user to re-enter if they do not match. The key steps are importing the CSV module, defining functions to get credentials and check them, opening the CSV file, reading it line by line, and verifying the inputs from the user match the credentials in the file.

Uploaded by

Harsh Mendapara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Python Basic Workbook OWTP

This document describes how to create a basic login panel in Python that stores username and password credentials in a CSV file. It includes functions to: 1. Take username and password inputs from the user. 2. Check the inputs against the credentials in the CSV file using the CSV module. 3. Print a success message if the credentials match or ask the user to re-enter if they do not match. The key steps are importing the CSV module, defining functions to get credentials and check them, opening the CSV file, reading it line by line, and verifying the inputs from the user match the credentials in the file.

Uploaded by

Harsh Mendapara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Project Workbook

Python (Basic)

www.azureskynet.com | +91-1244275997
Encryption and Decryption:
In this chapter, we’re going to take a closer look at how encryption and decryption takes place and why
they are important during the communication between two nodes.

So, this script will take only one message as an input and accordingly it will result you the encrypted and
decrypted message as shown below.

Input to be entered by
the user

Encrypted Message

Decrypted Message

Before you start with this make sure that you have already downloaded and installed python 3.x on
your system.

Encryption:-

Creating a new Script


Open python shell and create a new script for encryption and decryption. Go to:
File > New File
Setting up the dictionary
To understand the concept of encryption and decryption using python, we will start with
one of the earliest known and simplest ciphers. It is a substitution cipher in which each letter
in the plaintext is shifted to a certain number of places down the alphabet. Create a
dictionary of all the alphabets that needs to be used here for encryption and decryption

Setting the offset (or shift) value


Now we have to create a variable to store the key number(you can take any value for this) to
shift the indexing of the key number and then using that indexing in the dictionary that we
have created in the previous step to encrypt the message in such a way.

Key=9
Asking for plaintext from the user
Now you have to create a variable to take input from user and use it as a message that
needs to be securely transmitted from one node to the other followed by the encryption.

message=input(“Enter the message:”)

Converting Plaintext to cipher text


Now start by checking each alphabet in the message entered by the user and further
checking the indexing of that alphabet in the dictionary so as to further shift the indexing of
the encrypted message using the key number that we have taken in steps before.

for i in message:
if i in alphabet:
position=alphabet.find(i) #checking the
indexing of each alphabet
in the message in the dictionary
newposition=(position + key)%26
#changing the indexing according to the
dictionary and 26 is it’s length
newmessage+=alphabet[newposition]
#storing the message step by step after
encryption
else:
newmessage+=i #use this when
alphabet in the message didn’t matched with
any alphabet in dictionary

Displaying Final Cipher text


Now display the encrypted message on the output screen.

print(“Encrypted message:”,newmessage)
So you have successfully encrypted the input message and now it’s time to decrypt the
message to the original message as entered by the user

Decryption:-
For the conversion of your cipher text into the plain text as normal we need the same values
of the dictionary and the key number as we have previously taken for the process of
encryption.

Creating a new Variable


Now we will create a new variable to store the plain text (or the original message) after
conversion from cipher text.

Decrypt=””
Now it’s time to again start checking each alphabet in the message but this time your
message that needs to be checked will be the message that was encrypted in the previous
process of encryption and further we have to check for the indexing in the dictionary
containing all the alphabets so that we can decrypt the message back to the original text.

for i in newmessage:
if i in alphabet:
pos=alphabet.find(i)
newpos=(pos-key)%26
decrypt+=alphabet[newpos]
else:
decrypt+=i
Now display the decrypted message so as to verify that we are getting the original message
or not.

print(“Decrypted message:”,decrypt)
Login panel to store the username and password in csv format:
In this chapter, we’re going to take a closer look at how a login panel works and how you are able to
access your profile after a successful login and how it will ask you to re-enter your login credentials in
case of incorrect username or password.

So, this script will take two inputs from the user one will be the user name and the other will be the
password and accordingly you will be able to access your profile in case of successful login or it will ask
you to re-enter the login credentials in case of wrong username/password.

Username of the user

Password of the user

Output

Creating a new Script


Open python shell and create a new script to setup a login panel. Go to:
File > New File
Module Required
Firstly start by importing the modules required here and let’s
understand what module do we require and why do we need it here.
The module that we require is “csv” and the ansewer to the question
“why do we need it?” is that we have to create a file containing the
credentials of the authorized users separated by a comma “,” who could
access the profile on a website.

Storing the path


Now we will take a variable to store the path of the file containing the
uname and password (or say the credentials) of the authorized user.
Let’s say “as.txt” is the file containing the credentials in csv or comma
separated value format. So,
file='C:/Users/sd/Desktop/as.txt'

Creating the functions


Now we will create two new functions in our script one to take the
credential inputs from user as user name and password and the other
function to verify the credentials and further allows the authorized user
to access the account and disallow the unauthorized user and ask for the
user to re-enter the credentials.
def credentials(): #this function will take
credentials from user
global uname
global passwrd
uname=input('Enter user name:')
passwrd=input('Enter password:')
check()

In the above function we are creating two variables and both as a global
variable and further we are taking inputs from user and storing those
inputs in these variables, followed by the calling of the second function
,i.e., check() which will verify the credentials.

def check(): #this will verify the credentials


with open(file) as f: #accessing the file storing the
credentials
data=csv.reader(f) #reading the file using csv
module in csv format
for line in data: #checking each line and verifying
the credentials and displaying the
message accordingly
try:
unameC=line[0]
pwordC=line[1]
if uname == unameC and passwrd == pwordC:
print("Login Successful")
break #in case of correct credentials
exit the loop using break
statement
else: #in case of incorrect credentials
it will ask you to re-enter the
credentials to verify
print('try again')
details()
except IndexError:
pass
Calling the main function
Now when we have created the two functions containing all the
conditions to take the input from user and verifying the credentials to
result the successful login or to try again with new credentials. Finally it’s
time to call the main function which will carry on with all the processes.
if __name__== “__main__”:
credentials()

You might also like