Pythonでの暗号化、復号化のライブラリ
cryptographyを使う
code:python
pip install cryptography
これでライブラリーをインストールできる
ちなみに仮想環境を立ち上げた状態でインストールする必要がある
実際に鍵生成や暗号化、復号化の実装
code:AES(Advaned Encryption Standard).py
class Encryption:
#鍵生成
def __init__(self):
self.key = Fernet.generate_key()
self.cipher_suite = Fernet(self.key)
#引数として与えられた文字列データをバイト列にエンコードし、データを暗号化
def encrypt(self, data: str) -> bytes:
cipher_text = self.cipher_suite.encrypt(data.encode())
return cipher_text
#暗号化されたデータを引数としてうけとり復号化
def decrypt(self, cipher_text: bytes) -> str:
plain_text = self.cipher_suite.decrypt(cipher_text)
return plain_text.decode()