Cara Ngenkripsi lan Ngenkripsi Data ing Python nggunakake Cryptography Library

Ing tutorial iki sampeyan bakal sinau babagan cara ndhelik lan ndhelik data, f.eks. serangkaian teks nggunakake perpustakaan kriptografi ing Python.

Enkripsi minangka proses enkoding informasi kanthi cara supaya mung pihak resmi sing bisa ngakses. Iki ngidini kita nglindhungi data kanthi aman sing ora dikarepake sapa wae sing bisa ndeleng utawa ngakses.

Gegandhengan:


Ing conto iki, kita bakal nggunakake enkripsi simetris, sing tegese tombol sing padha kanggo ndhelik data, uga bisa digunakake kanggo dekripsi.

Perpustakaan kriptografi sing digunakake ing kene dibangun ing ndhuwur algoritma AES.




Enkripsi Data ing Python

Kaping pisanan, kita kudu nginstal perpustakaan kriptografi:



pip3 install cryptography

Saka perpustakaan kriptografi, kita kudu ngimpor Fernet lan wiwiti ngasilake tombol - tombol iki dibutuhake kanggo enkripsi simulasi / dekripsi.

Gawe Kunci

Kanggo ngasilake kunci, kita nelpon generate_key() cara:

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)

Kita mung kudu nglakokake cara ing ndhuwur sapisan kanggo ngasilake kunci.


Cathetan:Sampeyan kudu nyimpen tombol iki ing papan sing aman. Yen sampeyan ilang tombol, sampeyan ora bakal bisa ndhelik data sing dienkripsi karo kunci iki.

Muat Kunci

Sawise nggawe kunci, kita kudu mbukak kunci ing cara kanggo ndhelik data:

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

Enkripsi Pesen

Saiki, kita siyap ndhelik pesen. Iki minangka proses telung langkah:

  • 1 - encode pesen
  • 2 - nganakake kelas Fernet
  • 3 - kirim pesen sing dienkode menyang encrypt() cara

encode pesen:

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

miwiti kelas Fernet:


f = Fernet(key)

ndhelik pesen:

encrypted_message = f.encrypt(message)

Tuladha Kode Lengkap

Ing ngisor iki minangka conto lengkap babagan enkripsi pesen ing 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')

Output:

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

Decrypt Data ing Python

Kanggo ndekripsi pesen, kita mung nelpon decrypt() cara saka Fernet perpustakaan. Elingi, kita uga kudu mbukak kunci uga, amarga kunci dibutuhake kanggo ndhelik pesen.


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

Output:

encrypt this message