# Project - 5 : Encrypting in python

project - 5

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719349132611/1a67a68f-e857-47ab-8ebd-5a70f21488ca.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719349164099/7675bb52-366f-4d5a-8ad9-34ec4e2194b2.png align="center")

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719349221397/19f65ef7-d5b4-4d7c-b1ee-19bb92d71ea9.png align="center")

Link to see the code

[https://youtu.be/MZvQisAqDQk](https://youtu.be/MZvQisAqDQk)

**<mark>Problems That I encountered</mark>**

1. Understanding how the cryptography module works.
    
2. For now I haven't understood this syntax:
    
    ```python
    fer.decrypt(passw.encode()).decode())
    ```
    

3. Using the write() function syntax:
    
    ```python
            f.write(name+"|"+ fer.encrypt(pwd.encode()).decode() + "\n")
    ```
    
4. How fernet() works
    
5. Key is a variable where the load\_key() function output is saved.fer =Fernet(key)
    
    ```python
    fer =Fernet(key)
    ```
    

**<mark>Functions That I have used</mark>**

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.
    

3. .rstrip()
    
4. .readlines()
    
5. split()
    
6. decrypt()
    
7. 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,
    

8. input() and print() function
    
9. quit() : used to end the code
    

10. write() : It is used to manipulate the data in file and
    
11. open() : used to open a file or create a file
    
12. close() : used to close a file
    

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

**<mark>What I Learned</mark>**

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.

4. 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.

5. Only once we define a key and everytime we load the same key each time the code is runned and executed.
    
6. 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](http://edited.no) overwritten will happen.

7. "rb" file format of code that is ruby file format.
    
8. How to crate a new file using open() and storing the data in a format using user and password
    
    ```python
                user,passw = data.split("|") #we are using it to separate the user and the passwrod 
    ```
    
9. rstrip()isused to remove the carriage returns.
    

10. .readlines(): The readlines() method returns a list containing each line in the file as a list item.
    
11. Syntax : user,passw = data.split("|")
    
    I learned how to separate the data into rows to be saved in the file we created.
    
12. Learned how to define user defined functions and call them when needed. ex:- def add(): #definig a function { code }
    
    add() #calling a function
    

13. In the file "key.key" we will enter a random text that is a key .
    
14. file.close() we have to manully close the file if do not use 'with' syntax ex:-
    
    ```python
        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
    ```
    
15. * ```python
        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 
        ```
        
16. Do not recommend this method to save password
