Ungabhala njani kwaye ubethelele idatha kwiPython usebenzisa iThala leeNgcaciso

Kule tutorial uya kufunda indlela yokubhala kunye nokuchithwa kwedatha, umz. umtya wokubhaliweyo usebenzisa ilayibrari ye-cryptography ePython.

Encryption yinkqubo yokufaka iikhowudi kulwazi ngendlela enokuthi ifumaneke kuphela kumaqela agunyazisiweyo. Isivumela ukuba sikhusele ngokukhuselekileyo idatha esingafuni ukuba nabani na ayibone okanye ayifikelele.

Idibeneyo:


Kulo mzekelo, siya kuba sisebenzisa ukubethela okuhambelanayo, oku kuthetha ukuba isitshixo esifanayo esasisebenzisa ukubethela idatha, iyasetyenziselwa ukuchithwa.

Ilayibrari ye-cryptography esiyisebenzisayo apha yakhelwe ngaphezulu kwe-AES algorithm.




Bethela idatha kwiPython

Okokuqala, kufuneka sifake ithala leencwadi lokufihla:



pip3 install cryptography

Ukusuka kwilayibrari ye-cryptography, kufuneka singenise Fernet kwaye uqalise ukuvelisa isitshixo-eli qhosha liyimfuneko kulungelelwaniso lokubhaliweyo / ukuguqulela.

Yenza isitshixo

Ukuvelisa isitshixo, sibiza generate_key() indlela:

from cryptography.fernet import Fernet def generate_key():
'''
Generates a key and save it into a file
'''
key = Fernet.generate_key()
with open('secret.key', 'wb') as key_file:
key_file.write(key)

Kufuneka sisebenzise le ndlela ingentla kube kanye ukuvelisa isitshixo.


Phawula:Kufuneka ugcine eli qhosha kwindawo ekhuselekileyo. Ukuba ulahlekelwe sisitshixo, awuyi kuba nakho ukubhala ngokufihlakeleyo idatha ebhalwe ngokufihlakeleyo ngesi sitshixo.

Layisha isitshixo

Nje ukuba senze isitshixo, kufuneka silayishe isitshixo kwindlela yethu ukuze sibethele idatha:

def load_key():
'''
Loads the key named `secret.key` from the current directory.
'''
return open('secret.key', 'rb').read()

Bethela umyalezo

Ngoku, sikulungele ukubethela umyalezo. Le yinkqubo yamanyathelo amathathu:

  • 1 - khowuda umyalezo
  • 2 - qala iklasi yeFernet
  • 3 - dlulisa umyalezo obhalwe kwi-_ encrypt() indlela

khowuda umyalezo:

message = 'message I want to encrypt'.encode()

Qalisa iklasi yeFernet:


f = Fernet(key)

Bethela umyalezo:

encrypted_message = f.encrypt(message)

Umzekelo weKhowudi epheleleyo

Apha ngezantsi ngumzekelo opheleleyo wokusebenza ngokufihla umyalezo kwi-python:

from cryptography.fernet import Fernet def generate_key():
'''
Generates a key and save it into a file
'''
key = Fernet.generate_key()
with open('secret.key', 'wb') as key_file:
key_file.write(key) def load_key():
'''
Load the previously generated key
'''
return open('secret.key', 'rb').read() def encrypt_message(message):
'''
Encrypts a message
'''
key = load_key()
encoded_message = message.encode()
f = Fernet(key)
encrypted_message = f.encrypt(encoded_message)
print(encrypted_message) if __name__ == '__main__':
encrypt_message('encrypt this message')

Iziphumo:

b'gAAAAABesCUIAcM8M-_Ik_-I1-JD0AzLZU8A8-AJITYCp9Mc33JaHMnYmRedtwC8LLcYk9zpTqYSaDaqFUgfz-tcHZ2TQjAgKKnIWJ2ae9GDoea6tw8XeJ4='

Cacisa idatha kwiPython

Ukucacisa umyalezo, sibiza nje decrypt() indlela evela Fernet Ithala leencwadi. Khumbula, nathi kufuneka silayishe isitshixo, kuba isitshixo siyafuneka ukuze uguqule umyalezo.


from cryptography.fernet import Fernet def load_key():
'''
Load the previously generated key
'''
return open('secret.key', 'rb').read() def decrypt_message(encrypted_message):
'''
Decrypts an encrypted message
'''
key = load_key()
f = Fernet(key)
decrypted_message = f.decrypt(encrypted_message)
print(decrypted_message.decode()) if __name__ == '__main__':
decrypt_message(b'gAAAAABesCUIAcM8M-_Ik_-I1-JD0AzLZU8A8-AJITYCp9Mc33JaHMnYmRedtwC8LLcYk9zpTqYSaDaqFUgfz-tcHZ2TQjAgKKnIWJ2ae9GDoea6tw8XeJ4=')

Iziphumo:

encrypt this message