Maitiro Ekunyora uye Kuchenesa Dhata muPython uchishandisa Cryptography Raibhurari

Muchidzidzo ichi iwe unodzidza kunyorera nekunyudza dhata, i.e. tambo yemavara uchishandisa raibhurari yekunyorwa muPython.

Encryption ndiyo nzira yekukodha ruzivo nenzira yekuti mapato ane mvumo chete anogona kuiwana. Iyo inotibvumidza isu kuchengetedza zvakachengeteka data izvo isu tisingade chero munhu kuti aone kana kuwana.

Inoenderana:

Mumuenzaniso uyu, tichave tichishandisa symmetric encryption, zvinoreva kuti kiyi imwe chete yataishandisa kunyorera data, inoshandiswa zvakare kududzira.

Iyo cryptography raibhurari yatinoshandisa pano yakavakirwa pamusoro peAES algorithm.

Encrypt Dhata muPython

Kutanga, isu tinofanirwa kuisa iyo cryptography raibhurari:



pip3 install cryptography

Kubva kuraibhurari raibhurari, tinoda kuendesa Fernet uye tanga kugadzira kiyi - iyi kiyi inodikanwa kune symmetric encryption / decryption.

Gadzira Kiyi

Kuti tiite kiyi, tinoshevedza iyo generate_key() nzira:

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)

Isu tinongoda kuita nzira iri pamusoro kamwe chete kuti tibudise kiyi.

Ongorora:Unofanirwa kuchengeta kiyi iyi munzvimbo yakachengeteka. Kana iwe ukarasa kiyi, haugone kudhirowa iyo data yaive yakavharirwa nekiyi iyi.

Takura Kiyi

Kana tangogadzira kiyi, isu tinofanirwa kurodha kiyi munzira yedu kuti tinyore data:

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

Encrypt Meseji

Zvino, isu takagadzirira kunyorera meseji. Iyi nhanho nhanho maitiro:

  • 1 - encode iyo meseji
  • 2 - tanga iyo Fernet kirasi
  • 3 - pfuudza iyo yakanyorwa meseji ku encrypt() nzira

encode iro meseji:

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

tanga kirasi yeFernet:

f = Fernet(key)

encrypt meseji:

encrypted_message = f.encrypt(message)

Yakazara Kodhi Muenzaniso

Pazasi pane yakazara izere inoshanda muenzaniso wekunyorera meseji mu 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')

Kubuda:

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

Decrypt Dhata muPython

Kududzira meseji, isu tinongodaidza iyo _ _ + _ | nzira kubva decrypt() raibhurari. Rangarira, isu tinofanirwa zvakare kurodha kiyi zvakare, nekuti kiyi inodiwa kuti utore meseji.

Fernet

Kubuda:

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=')