python cryptographyで暗号・復号の方法を調べたので
タイトル:python cryptog…
カテゴリ:python
投稿日:22/05/17 23:13
更新日:22/05/17 23:13
GOOD
0
お気に入り
```py
from cryptography.fernet import Fernet
def Sample():
# 暗号化
key = Fernet.generate_key()
with open('.\\test.key', "wb") as key_data:
key_data.write(key)
with open('.\\sample.txt',"r") as file:
data = file.read()
print(data)
byte_data = data.encode()
f = Fernet(key)
encrypt_data = f.encrypt(byte_data)
with open('.\\sample.txt', "wb") as file:
file.write(encrypt_data)
print(encrypt_data)
def Sample2():
#復号
with open('.\\test.key', "rb") as key_data:
key = key_data.read()
with open('.\\sample.txt',"rb") as file:
encrypt_data = file.read()
print(encrypt_data)
f = Fernet(key)
decrypt_data = f.decrypt(encrypt_data)
print(decrypt_data.decode('utf-8'))
if __name__ == "__main__":
Sample()
Sample2()
```
・元のテキスト
Sample
test test
<br>
・暗号化
b'gAAAAABig6twLTsU3r3yd3UH_GO2j5sZ9OUGmaHOjZhX6V5BRZwc......