Me Pehea te Whakamuna me te Tango Whakakore i te Raraunga ki te maori ma te whakamahi i te Whare Pukapuka Cryptography

I roto i tenei akoranga ka ako koe me pehea te whakamuna me te wetewete i nga raraunga, hei. he aho kuputuhi e whakamahi ana i te whare pukapuka cryptography i Python.

Ko te Whakamuna te mahinga o te whakawaehere korero kia taea ai e nga roopu whai mana anake te uru atu. Ka taea e maatau te tiaki humarie i nga raraunga kaore e hiahiatia e maatau kia kite, kia uru mai ranei tetahi.

E pā ana:

I roto i tenei tauira, ka whakamahia e matou te hangarite hangarite, ko te tikanga ano o te ki i whakamahia e matou ki te whakamuna raraunga, he pai ano mo te wetemunatanga.

Ko te whare pukapuka cryptography e whakamahia ana e maatau ki konei i hangaia i runga o te AES algorithm.

Whakamuna Raraunga i te Python

Tuatahi, me whakauru e tatou te whare pukapuka cryptography:



pip3 install cryptography

Mai i te whare pukapuka cryptography, me kawemai Fernet ka tiimata ki te whakaputa taviri - e hiahiatia ana tenei ki mo te hangarite hangarite / wetemunatanga.

Hanga Kī

Ki te whakaputa i te ki, ka karanga matou i te generate_key() tikanga:

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)

Kia kotahi tonu te whakamahi i te tikanga i runga ake nei kia puta ai he ki.

Panui:Me mau tonu tenei ki ki te waahi ahuru. Mena ka ngaro i a koe te ki, kaore e taea e koe te wetewete i nga raraunga kua whakakapi ki tenei ki.

Utaina te Kī

Ka oti ana i a maatau he matua, me uta e tatou te ki ki ta maatau tikanga kia huna nga raraunga:

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

Whakamunahia he Karere

Na, kua rite taatau ki te whakakapi i tetahi korero. Koinei te mahinga e toru:

  • 1 - whakawaeherehia te korero
  • 2 - arawhiti i te akomanga Fernet
  • 3 - tukuna nga korero whakawaehere ki encrypt() tikanga

whakawaeheretia te korero:

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

arawhiti i te karaehe Fernet:

f = Fernet(key)

whakamuna te kupu:

encrypted_message = f.encrypt(message)

Tauira Waehere Katoa

Kei raro nei tetahi tauira mahi mo te whakamuna i tetahi karere ki te maori:

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

Putanga:

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

Wetemuna Raraunga i te Python

Hei wetemunahia te korero, ka karanga noa matou i te decrypt() tikanga mai i te Fernet whare pukapuka. Kia maumahara, me uta ano hoki e tatou te ki, na te mea e hiahiatia ana te matua kia wetewetehia te korero.

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

Putanga:

encrypt this message