0% found this document useful (0 votes)
40 views3 pages

Python

This document demonstrates how to use the Fernet module in Python to encrypt and decrypt a file containing password information. It shows generating an encryption key, using it to encrypt a file, writing the encrypted data to a new file, then decrypting and comparing the original and decrypted files. The key steps are: generating an encryption key, using it to encrypt a file containing passwords, writing the encrypted data to a new file, decrypting the file, and comparing the original and decrypted files to verify they match.

Uploaded by

Anna Ka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Python

This document demonstrates how to use the Fernet module in Python to encrypt and decrypt a file containing password information. It shows generating an encryption key, using it to encrypt a file, writing the encrypted data to a new file, then decrypting and comparing the original and decrypted files. The key steps are: generating an encryption key, using it to encrypt a file containing passwords, writing the encrypted data to a new file, decrypting the file, and comparing the original and decrypted files to verify they match.

Uploaded by

Anna Ka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

L’aide sommaire de python

python --help

la version de python
┌──(kali㉿kali)-[~]

└─$ python -V

Python 3.10.8

┌──(kali㉿kali)-[~]

└─$ python

Python 3.10.8 (main, Nov 4 2022, 09:21:25) [GCC 12.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

Fernet est un outil de chiffrement symetric et il guarantit qu’un message crypte


ne peut etre lu ou manipule sans son cle de chiffrement.
>>> import cryptography.fernet

L’aide sommaire de la commande de cryptage


>>> help(cryptography.fernet.Fernet.encrypt)

L’aide detaille de la commande de cryptage


>>> help(cryptography.fernet.Fernet.encrypt.__doc__)

Creer un fichier mypass.txt et y mettre root et mon utilisateur


>>> import os

>>> with open("mypass.txt", "w") as file:... file.write("root:x:0:0:root:/root:/usr/bin/zsh")...


file.write("kali:x:1000:1000:,,,:/home/kali:/usr/bin/zsh")...

34

44

Crypter le fichier mypass.txt


>>>

>>> from cryptography.fernet import Fernet

>>> key = Fernet.generate_key()

>>> with open("key.key", "wb") as key_file:

... key_file.write(key)

...

44

>>> with open("mypass.txt", "rb") as input_file:


... input_data=input_file.read()

...

>>>

>>> fernet=Fernet(key)

>>> fernet.encrypt(input_data)

b'gAAAAABkSb-
zbLbZZxHcSmlh5Hs5MZxxyUfyZUpftdiAUMX0aeKEgdSxTCgjbXRDzovHTWritQgxCnfkPgY7NiKhx7nk_X
pWmSxXQIfKknNgZ6oHyHw9HGlrSic7YiaIxtd7WBk0qnyzlXvuRPdSWVhyLBWxWwsz9MTrbjk0NCdvN
mlHhzTp8Os='

>>> encrypted_data = fernet.encrypt(input_data)

>>> with open("mypass.crypt", "wb") as output_file:

... output_file.write(encrypted_data)

...

184

Afficher le contenu de mypass.txt et mypass.crypt


>>> with open("mypass.crypt", "rb") as file:

... print(file.read())

...

b'gAAAAABkScAqzLXCCG6QoavLV86Xthyj92uGJVDLTAGY3rZhpeD236ONqGYMJMEveYEG4IwYZl5oqjp
VWOBT8axcoBphW-whOphKcrHXZlOtnN45Se6kTETC2-hgV-
DSns_TPmeaQmCHENdqviWDR3cyXdoyWQ6CzBsKuo_d5baZNDHsPNQkh18='

>>> with open("mypass.txt", "rb") as file:

... print(file.read())

...

b'root:x:0:0:root:/root:/usr/bin/zshkali:x:1000:1000:,,,:/home/kali:/usr/bin/zsh'

Dechiffrer mypass.crypt en mypass.decrypted


>>> fernet= Fernet(key)

>>> with open("mypass.crypt", "rb") as file:

... encrypted_data=file.read()

...

>>> decrypted_data=fernet.decrypt(encrypted_data)

>>> with open("mypass.decrypted","wb") as file:

... file.write(decrypted_data)
...

78

Comparer les fichiers mypass.txt et mypass.decrypted


>>> with open("mypass.txt") as file_1:file_1_text=file_1.readlines()

...

>>> with open("mypass.decrypted") as file_2:file_2_text=file_2.readlines()

...

>>> import difflib

>>> for line in difflib.unified_diff(

... file_1_text,file_2_text,fromfile="mypass.txt",tofile="mypass.decrypted",lineterm=""):print(line)

...

>>>

You might also like