Python/Cryptography

出典: フリー教科書『ウィキブックス(Wikibooks)』

Python/Cryptographyの概要[編集]

Cryptographyは、Pythonで利用できるオープンソースの暗号化および暗号学のライブラリです。Cryptographyは、データの暗号化、復号化、署名、ハッシュなどの暗号学的な操作をサポートし、セキュリティ関連のタスクを簡単に実行することができます。Cryptographyは安全性に配慮された設計がなされており、安全な暗号アルゴリズムを提供しています。

主な機能[編集]

1. データの暗号化と復号化: Cryptographyは対称鍵暗号と公開鍵暗号をサポートしており、データの暗号化と復号化を行うことができます。

2. デジタル署名: Cryptographyは公開鍵暗号を使用してデジタル署名を生成し、署名の検証を行う機能を提供します。

3. ハッシュ関数: Cryptographyはハッシュ関数をサポートしており、データのハッシュ値を計算することができます。

4. キーダービベレーション: Cryptographyは安全なランダムキーの生成をサポートし、キーダービベレーションを容易に行うことができます。


Python/Cryptographyのコード例[編集]

コードは未検証なので、検証・編集を歓迎します。

データの暗号化と復号化[編集]

from cryptography.fernet import Fernet

# ランダムな鍵の生成
key = Fernet.generate_key()

# 鍵を使用してFernetオブジェクトを作成
fernet = Fernet(key)

# データの暗号化
data = b"Hello, world!"
encrypted_data = fernet.encrypt(data)

# データの復号化
decrypted_data = fernet.decrypt(encrypted_data)

# 結果の表示
print("Original data:", data)
print("Encrypted data:", encrypted_data)
print("Decrypted data:", decrypted_data)

デジタル署名[編集]

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding

# 鍵の生成
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)

# デジタル署名を作成
message = b"Hello, world!"
signature = private_key.sign(message, padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA256())

# デジタル署名の検証
public_key = private_key.public_key()
public_key.verify(signature, message, padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH), hashes.SHA256())

# 結果の表示
print("Message:", message)
print("Signature:", signature)
print("Signature verified successfully!")

ハッシュ関数[編集]

from cryptography.hazmat.primitives import hashes

# ハッシュ関数を使用してハッシュ値を計算
data = b"Hello, world!"
digest = hashes.Hash(hashes.SHA256())
digest.update(data)
hash_value = digest.finalize()

# 結果の表示
print("Data:", data)
print("Hash value:", hash_value)

ファイルの暗号化と復号化[編集]

from cryptography.fernet import Fernet

# ランダムな鍵の生成
key = Fernet.generate_key()

# 鍵を使用してFernetオブジェクトを作成
fernet = Fernet(key)

# ファイルの暗号化
def encrypt_file(filename, key):
    with open(filename, 'rb') as file:
        data = file.read()
        encrypted_data = fernet.encrypt(data)
    
    with open(filename + '.encrypted', 'wb') as encrypted_file:
        encrypted_file.write(encrypted_data)

# ファイルの復号化
def decrypt_file(filename, key):
    with open(filename, 'rb') as encrypted_file:
        encrypted_data = encrypted_file.read()
        decrypted_data = fernet.decrypt(encrypted_data)
    
    with open(filename[:-10], 'wb') as decrypted_file:
        decrypted_file.write(decrypted_data)

# ファイルの暗号化と復号化の例
file_to_encrypt = 'example.txt'
file_to_decrypt = 'example.txt.encrypted'

encrypt_file(file_to_encrypt, key)
decrypt_file(file_to_decrypt, key)

ステータスバーの追加[編集]

上記のコード例にステータスバーの追加を行います。ステータスバーは、暗号化や復号化の進捗状況を表示するために使用します。以下はファイルの暗号化と復号化のコード例にステータスバーを追加したものです:

from cryptography.fernet import Fernet
import time

# ランダムな鍵の生成
key = Fernet.generate_key()

# 鍵を使用してFernetオブジェクトを作成
fernet = Fernet(key)

# ファイルの暗号化
def encrypt_file(filename, key):
    with open(filename, 'rb') as file:
        data = file.read()
        encrypted_data = fernet.encrypt(data)
    
    with open(filename + '.encrypted', 'wb') as encrypted_file:
        encrypted_file.write(encrypted_data)

# ファイルの復号化
def decrypt_file(filename, key):
    with open(filename, 'rb') as encrypted_file:
        encrypted_data = encrypted_file.read()
        decrypted_data = fernet.decrypt(encrypted_data)
    
    with open(filename[:-10], 'wb') as decrypted_file:
        decrypted_file.write(decrypted_data)

# ステータスバーの表示
def show_status(message):
    print("[{}] {}".format(time.strftime('%Y-%m-%d %H:%M:%S'), message))

# ファイルの暗号化と復号化の例
file_to_encrypt = 'example.txt'
file_to_decrypt = 'example.txt.encrypted'

show_status("Encrypting file: {}".format(file_to_encrypt))
encrypt_file(file_to_encrypt, key)
show_status("File encrypted successfully!")

show_status("Decrypting file: {}".format(file_to_decrypt))
decrypt_file(file_to_decrypt, key)
show_status("File decrypted successfully!")

このように、ステータスバーを使用することで、暗号化や復号化の進捗状況をリアルタイムで表示することができます。

Cryptographyのインストール方法[編集]

Cryptographyはpipコマンドを使用して簡単にインストールすることができます。以下のコマンドを実行してください: pip install cryptography