Skip to main content

Command Palette

Search for a command to run...

Project - 5 : Encrypting in python

Updated
4 min readView as Markdown

project - 5

Above is "key.key" file where the key is saved.

Below is the "password.txt" file where all the password and username are saved.

Link to see the code

https://youtu.be/MZvQisAqDQk

Problems That I encountered

  1. Understanding how the cryptography module works.

  2. For now I haven't understood this syntax:

     fer.decrypt(passw.encode()).decode())
    
  1. Using the write() function syntax:

             f.write(name+"|"+ fer.encrypt(pwd.encode()).decode() + "\n")
    
  2. How fernet() works

  3. Key is a variable where the load_key() function output is saved.fer =Fernet(key)

     fer =Fernet(key)
    

Functions That I have used

  1. user defined functions :-

    1. add() : It is used to add the password and view the details

    2. view(): To view the information

    3. load_key(): It is used to load the key

  2. fernet : Fernet guarantees that a message encrypted using it cannot be manipulated or read without the key. Fernet is an implementation of symmetric (also known as “secret key”) authenticated cryptography.

  1. .rstrip()

  2. .readlines()

  3. split()

  4. decrypt()

  5. encode() , decode(): an encoder converts information signals into coded digital bitstreams, while a decoder converts those coded bits back into the original information signal. Encoding and decoding are different from encryption and decryption,

  1. input() and print() function

  2. quit() : used to end the code

  1. write() : It is used to manipulate the data in file and

  2. open() : used to open a file or create a file

  3. close() : used to close a file

  1. 'a': append :used to insert data into the file without overwrttitng existing file.

What I Learned

  1. I learned importing cryptography module it is used to encrypt a text. fernet: It will create a key using the text that has to be encrypted it will combine the text and key and create a random text.

  2. fernet: Fernet guarantees that a message encrypted using it cannot be manipulated or read without the key. Fernet is an implementation of symmetric (also known as “secret key”) authenticated cryptography.

  3. 3.1.We have to generate a key that is define a key and next

3.2 load the key once that is done we do not need to define a key again so we comment that part of the code after running the code once.

  1. How it works:

    4.1.To encrypt the original text that is converting it into a random text ; we do that by combining key and password . And while decrypting the random text to get the original text it will ask us for a key if that key inputed invalid then it will give us a random text that has no meaning .

4.2. Encrypting : 2 Random text ++ password is used to encrypt.

4.3. Key + password + original_text(text_to_encrypt) = Random text.

4.4.Decrypting : Require valid keys input if input is invalid key it give a randomtext an output that has no meaning.

  1. Only once we define a key and everytime we load the same key each time the code is runned and executed.

  2. open("file_name", "rb") function is used to open a text file if doesn't exists then it create one new file with that name with the file format as 'rb'.

    commands : read(r) , write(w) , execute(x)

6.1. read(): this function is to open and read a file only. 6.2]write(): It will overwrite any existing file .It is used to add data into a new file. 6.3]execute() : It is used to execute a file .. 6.4] append() :Existing file can be opened and edited.no overwritten will happen.

  1. "rb" file format of code that is ruby file format.

  2. How to crate a new file using open() and storing the data in a format using user and password

                 user,passw = data.split("|") #we are using it to separate the user and the passwrod
    
  3. rstrip()isused to remove the carriage returns.

  1. .readlines(): The readlines() method returns a list containing each line in the file as a list item.

  2. Syntax : user,passw = data.split("|")

    I learned how to separate the data into rows to be saved in the file we created.

  3. Learned how to define user defined functions and call them when needed. ex:- def add(): #definig a function { code }

    add() #calling a function

  1. In the file "key.key" we will enter a random text that is a key .

  2. file.close() we have to manully close the file if do not use 'with' syntax ex:-

        with open('password.txt' , 'r') as f:
    # open(file_name , mode) we can create a fime witha name ,
    # and we can a define a code with which we can open the file 
    f.write(name+"|"+ fer.encrypt(pwd.encode()).decode() + "\n") #written the details into the file txt
    
    •   with open('password.txt' , 'a') as f: file(a:append) 
        #here ' with ' closes file after we edit the file . 
        #No need  to use the close() function
      
  3. Do not recommend this method to save password

More from this blog

Let see

16 posts